diff options
Diffstat (limited to 'wikibania/ban/BanDB.py')
-rw-r--r-- | wikibania/ban/BanDB.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/wikibania/ban/BanDB.py b/wikibania/ban/BanDB.py new file mode 100644 index 0000000..e83aa3c --- /dev/null +++ b/wikibania/ban/BanDB.py | |||
@@ -0,0 +1,50 @@ | |||
1 | import json | ||
2 | |||
3 | from wikibania.ban.Ban import Ban | ||
4 | from wikibania.wapi.WikipediaQuery import BlockQuery | ||
5 | |||
6 | |||
7 | class BanDB: | ||
8 | def __init__(self, geoip_looker): | ||
9 | self.geoip_looker = geoip_looker | ||
10 | self.bans = [] | ||
11 | |||
12 | def list(self): | ||
13 | return self.bans | ||
14 | |||
15 | def load(self, ban_list): | ||
16 | for entry in ban_list: | ||
17 | ban = Ban(self.geoip_looker) | ||
18 | ban.hydrate(entry) | ||
19 | self.bans.append(ban) | ||
20 | |||
21 | def load_file(self, file_name): | ||
22 | with open(file_name, "r") as file: | ||
23 | entries = json.load(file) | ||
24 | self.load(entries) | ||
25 | |||
26 | def dump(self): | ||
27 | return [ban.items() for ban in self.bans] | ||
28 | |||
29 | def dump_file(self, file_name): | ||
30 | with open(file_name, "w") as file: | ||
31 | ban_list = self.dump() | ||
32 | json.dump(ban_list, file) | ||
33 | |||
34 | def fetch(self, nb_samples, query_limit=500, continue_token=None): | ||
35 | fetch = min(nb_samples, query_limit) | ||
36 | |||
37 | query = BlockQuery( | ||
38 | properties=["user", "timestamp", "expiry"], | ||
39 | show=["temp", "ip"], | ||
40 | limit=fetch, | ||
41 | continue_token=continue_token, | ||
42 | ) | ||
43 | results = query.fetch_result() | ||
44 | |||
45 | entries = results["query"]["blocks"] | ||
46 | self.load(entries) | ||
47 | |||
48 | if nb_samples - fetch > 0: | ||
49 | continue_token = results["query-continue"]["blocks"]["bkcontinue"] | ||
50 | self.fetch(nb_samples - fetch, query_limit, continue_token) | ||