summaryrefslogtreecommitdiff
path: root/main.py
blob: d6ebe3d96a641d7b0edd6c72bdd70d979923a240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from collections import Counter
import webbrowser

import pygal
import numpy as np
import pygeoip

from wikibania.ban.BanDB import BanDB
from wikibania.ban.BanDBWrapper import BanDBWrapper
import sysproxy


# PARAMS

GEOIP_DB = "/usr/share/GeoIP/GeoIP.dat"

BAN_MAP_FILE = "output/ban-map.svg"
BAN_DURATION_MAP_FILE = "output/ban-duration-map.svg"
HIST_FILE = "output/histogram.svg"
STATS_FILE = "output/stats.txt"

BAN_DB_FILE = "resources/ban_list.json"

FETCH_SAMPLES = 2000
FETCH_DB = False
DUMP_DB = False
LOAD_DB = True

OPEN_FILES = False


# SETUP

sysproxy.configure_system_proxy()
geoip_looker = pygeoip.GeoIP(GEOIP_DB)

ban_db = BanDB(geoip_looker)

if FETCH_DB:
    ban_db.fetch(FETCH_SAMPLES)
if LOAD_DB:
    ban_db.load_file(BAN_DB_FILE)
if DUMP_DB:
    ban_db.dump_file(BAN_DB_FILE)

ban_db_wrapper = BanDBWrapper(ban_db)


# HISTOGRAM

ban_durations = ban_db_wrapper.get_all_durations()
(ban_durations_bars, bins) = np.histogram(ban_durations, bins=range(1, 50 + 2))

bar_chart = pygal.Bar(legend_at_bottom=True)
bar_chart.title = "Active Wikipedia bans by duration (%d samples)" % len(ban_db.list())
bar_chart.x_labels = map(str, range(1, len(ban_durations_bars) + 1))
bar_chart.add("Number of active bans", ban_durations_bars)
bar_chart.render_to_file(HIST_FILE)

if OPEN_FILES:
    webbrowser.open(HIST_FILE, 2)


# BAN DURATION STATS

with open(STATS_FILE, "w") as stats_file:
    stats_file.write("Ban duration:\n")
    stats_file.write("Mean: %.2f days\n" % np.mean(ban_durations))
    stats_file.write("Median: %.2f days\n" % np.median(ban_durations))
    stats_file.write("Deviation: %.2f\n" % np.std(ban_durations))
    stats_file.write("Variance: %.2f\n" % np.var(ban_durations))

if OPEN_FILES:
    webbrowser.open(STATS_FILE, 2)


# NB BAN MAP

country_ban_list = ban_db_wrapper.get_all_countries()
nb_bans_by_country = Counter(country_ban_list)

worldmap_chart = pygal.Worldmap(legend_at_bottom=True)
worldmap_chart.title = "World active Wikipedia bans by country (%d samples)" % len(ban_db.list())
worldmap_chart.add("Active bans", nb_bans_by_country)
worldmap_chart.render_to_file(BAN_MAP_FILE)

if OPEN_FILES:
    webbrowser.open(BAN_MAP_FILE, 2)


# BAN DURATION MAP

average_ban_duration_by_country = ban_db_wrapper.calc_average_duration_by_country()

worldmap_chart = pygal.Worldmap(legend_at_bottom=True)
worldmap_chart.title = "Average Wikipedia ban duration by country (%d samples)" % len(ban_db.list())
worldmap_chart.add("Average ban duration (months)", average_ban_duration_by_country)
worldmap_chart.render_to_file(BAN_DURATION_MAP_FILE)

if OPEN_FILES:
    webbrowser.open(BAN_DURATION_MAP_FILE, 2)