summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py83
1 files changed, 10 insertions, 73 deletions
diff --git a/main.py b/main.py
index dff07f5..548cb30 100644
--- a/main.py
+++ b/main.py
@@ -1,9 +1,8 @@
1from banapedia.Ban import * 1from banapedia.Ban import *
2import bandict
2from collections import Counter 3from collections import Counter
3import json
4import pygal 4import pygal
5import numpy as np 5import numpy as np
6import urllib.request
7 6
8__author__ = 'pacien' 7__author__ = 'pacien'
9 8
@@ -12,44 +11,15 @@ BAN_MAP_FILE = "output/ban-map.svg"
12BAN_DURATION_MAP_FILE = "output/ban-duration-map.svg" 11BAN_DURATION_MAP_FILE = "output/ban-duration-map.svg"
13HIST_FILE = "output/histogram.svg" 12HIST_FILE = "output/histogram.svg"
14 13
15BAN_FILE = "resources/ban_list.json"
16
17SAMPLES = 30000 14SAMPLES = 30000
18SAMPLES_BY_QUERY = 500
19
20
21def configure_proxy():
22 proxy = urllib.request.ProxyHandler(urllib.request.getproxies())
23 opener = urllib.request.build_opener(proxy)
24 urllib.request.install_opener(opener)
25
26
27def load_from_internet():
28 configure_proxy()
29 return fetch_multipart_ban_dict(SAMPLES, SAMPLES_BY_QUERY)
30
31
32def load_from_local():
33 with open(BAN_FILE, "r") as ban_dict_file:
34 return json.load(ban_dict_file)
35
36
37def write_to_local(ban_dict_list):
38 with open(BAN_FILE, "w") as ban_dict_file:
39 json.dump(ban_dict_list, ban_dict_file, indent="\t")
40
41
42# ban_dict_list = load_from_internet()
43# write_to_local(ban_dict_list)
44
45ban_dict_list = load_from_local()
46 15
47ban_list = map_bans(ban_dict_list) 16BAN_FILE = "resources/ban_list.json"
48 17
18ban_dict_list = bandict.BanList(BAN_FILE)
49 19
50########## HISTOGRAM ########## 20# ======== HISTOGRAM ======= #
51 21
52ban_durations = [ban.get_duration() for ban in ban_list] 22ban_durations = ban_dict_list.get_durations()
53(ban_durations_bars, bins) = np.histogram(ban_durations, bins=[round(365/12*x) for x in range(1, 50+2)]) 23(ban_durations_bars, bins) = np.histogram(ban_durations, bins=[round(365/12*x) for x in range(1, 50+2)])
54 24
55print("[INFO]", "Generating histogram") 25print("[INFO]", "Generating histogram")
@@ -60,13 +30,10 @@ bar_chart.add("Number of active bans", ban_durations_bars)
60bar_chart.render_to_file(HIST_FILE) 30bar_chart.render_to_file(HIST_FILE)
61print("[INFO]", "Histogram generation complete") 31print("[INFO]", "Histogram generation complete")
62 32
63########## NB BAN MAP ########## 33# ======= NB BAN MAP ======= #
64
65def count_by_country(ban_list):
66 country_ban_list = [ban.get_country_code() for ban in ban_list]
67 return Counter(country_ban_list)
68 34
69nb_bans_by_country = count_by_country(ban_list) 35country_ban_list = ban_dict_list.get_countries()
36nb_bans_by_country = Counter(country_ban_list)
70 37
71print("[INFO]", "Generating ban map") 38print("[INFO]", "Generating ban map")
72worldmap_chart = pygal.Worldmap(legend_at_bottom=True) 39worldmap_chart = pygal.Worldmap(legend_at_bottom=True)
@@ -76,35 +43,9 @@ worldmap_chart.render_to_file(BAN_MAP_FILE)
76print("[INFO]", "Ban map generation complete") 43print("[INFO]", "Ban map generation complete")
77 44
78 45
79########## BAN DURATION MAP ########## 46# ======= BAN DURATION MAP ======= #
80
81def group_by_country(ban_list):
82 ban_duration_by_country = {}
83
84 for ban in ban_list:
85 country_code = ban.get_country_code()
86
87 if country_code not in ban_duration_by_country.keys():
88 ban_duration_by_country[country_code] = []
89
90 ban_duration_by_country[country_code].append(ban)
91
92 return ban_duration_by_country
93 47
94 48average_ban_duration_by_country = ban_dict_list.average_ban_by_country()
95def calc_average_ban_by_country(ban_by_country_dict):
96 average_ban_duration_by_country = {}
97
98 for country, bans in ban_by_country_dict.items():
99 average = np.mean([ban.get_duration() for ban in bans])
100 average_ban_duration_by_country[country] = average
101
102 return average_ban_duration_by_country
103
104ban_duration_by_country = group_by_country(ban_list)
105average_ban_duration_by_country = calc_average_ban_by_country(ban_duration_by_country)
106
107average_ban_duration_by_country = {country: duration/30 for country, duration in average_ban_duration_by_country.items()}
108 49
109print("[INFO]", "Generating ban duration map") 50print("[INFO]", "Generating ban duration map")
110worldmap_chart = pygal.Worldmap(legend_at_bottom=True) 51worldmap_chart = pygal.Worldmap(legend_at_bottom=True)
@@ -113,10 +54,6 @@ worldmap_chart.add("Average ban duration (months)", average_ban_duration_by_coun
113worldmap_chart.render_to_file(BAN_DURATION_MAP_FILE) 54worldmap_chart.render_to_file(BAN_DURATION_MAP_FILE)
114print("[INFO]", "Ban duration map generation complete") 55print("[INFO]", "Ban duration map generation complete")
115 56
116print("\nTHIS WAS A TRIUMPH!")
117print("I'M MAKING A NOTE HERE:")
118print("HUGE [SUCCESS]\n")
119
120print("Some additional stats about ban durations:") 57print("Some additional stats about ban durations:")
121print(" Mean: %.2f days" % np.mean(ban_durations)) 58print(" Mean: %.2f days" % np.mean(ban_durations))
122print(" Median: %.2f days" % np.median(ban_durations)) 59print(" Median: %.2f days" % np.median(ban_durations))