diff options
Diffstat (limited to 'wikibania/ban/Ban.py')
-rw-r--r-- | wikibania/ban/Ban.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/wikibania/ban/Ban.py b/wikibania/ban/Ban.py new file mode 100644 index 0000000..e06ca89 --- /dev/null +++ b/wikibania/ban/Ban.py | |||
@@ -0,0 +1,37 @@ | |||
1 | from datetime import datetime | ||
2 | |||
3 | import pygeoip | ||
4 | |||
5 | |||
6 | ISO_TIMESTAMP = "%Y-%m-%dT%H:%M:%SZ" | ||
7 | |||
8 | |||
9 | class Ban: | ||
10 | def __init__(self, geoip_looker, user=None, timestamp=None, expiry=None, timestamp_format=ISO_TIMESTAMP): | ||
11 | self.geoip_looker = geoip_looker | ||
12 | self.timestamp_format = timestamp_format | ||
13 | |||
14 | self.user = user | ||
15 | self.timestamp = timestamp | ||
16 | self.expiry = expiry | ||
17 | |||
18 | def items(self): | ||
19 | return { | ||
20 | "user": self.user, | ||
21 | "timestamp": self.timestamp.strftime(ISO_TIMESTAMP), | ||
22 | "expiry": self.expiry.strftime(ISO_TIMESTAMP), | ||
23 | } | ||
24 | |||
25 | def hydrate(self, ban_dict): | ||
26 | self.user = ban_dict["user"] | ||
27 | self.timestamp = datetime.strptime(ban_dict["timestamp"], ISO_TIMESTAMP) | ||
28 | self.expiry = datetime.strptime(ban_dict["expiry"], ISO_TIMESTAMP) | ||
29 | |||
30 | def calc_duration(self): | ||
31 | return (self.expiry - self.timestamp).days | ||
32 | |||
33 | def lookup_country_code(self): | ||
34 | try: | ||
35 | return self.geoip_looker.country_code_by_addr(self.user).lower() | ||
36 | except pygeoip.GeoIPError: | ||
37 | return "UNKNOWN" | ||