From bdf9099df8c2a4636b0ad0e710b73330877eef37 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 24 Oct 2014 19:59:05 +0200 Subject: Very cleaner, much class, such readable, wow --- wikibania/ban/BanDB.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 wikibania/ban/BanDB.py (limited to 'wikibania/ban/BanDB.py') 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 @@ +import json + +from wikibania.ban.Ban import Ban +from wikibania.wapi.WikipediaQuery import BlockQuery + + +class BanDB: + def __init__(self, geoip_looker): + self.geoip_looker = geoip_looker + self.bans = [] + + def list(self): + return self.bans + + def load(self, ban_list): + for entry in ban_list: + ban = Ban(self.geoip_looker) + ban.hydrate(entry) + self.bans.append(ban) + + def load_file(self, file_name): + with open(file_name, "r") as file: + entries = json.load(file) + self.load(entries) + + def dump(self): + return [ban.items() for ban in self.bans] + + def dump_file(self, file_name): + with open(file_name, "w") as file: + ban_list = self.dump() + json.dump(ban_list, file) + + def fetch(self, nb_samples, query_limit=500, continue_token=None): + fetch = min(nb_samples, query_limit) + + query = BlockQuery( + properties=["user", "timestamp", "expiry"], + show=["temp", "ip"], + limit=fetch, + continue_token=continue_token, + ) + results = query.fetch_result() + + entries = results["query"]["blocks"] + self.load(entries) + + if nb_samples - fetch > 0: + continue_token = results["query-continue"]["blocks"]["bkcontinue"] + self.fetch(nb_samples - fetch, query_limit, continue_token) -- cgit v1.2.3