-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
48 lines (39 loc) · 1.44 KB
/
user.py
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
import requests
from settings import (
FORBIDDEN_COUNTRIES,
GEOIP_API_URL,
SKIP_MOBILE_CLIENTS_CHECK,
logger,
)
def get_geo_by_ip(ip_address: str) -> dict | None:
try:
response = requests.get(f"{GEOIP_API_URL}{ip_address}")
except requests.exceptions.RequestException as e:
logger.error(f"Error while getting geo by IP. Error: {e}")
return None
return response.json()
class User:
def __init__(self, login, users_to_remind):
self.id = login["user_id"]
self.username = login["username"]
self.agent = login["user_agent"]
self.ip = login["ip"]
self.geo = get_geo_by_ip(self.ip)
self.country = self.geo.get("country", "Unknown")
self.countryCode = self.geo.get("countryCode", "Unknown")
self.needs_reminder = self.is_need_to_remind(users_to_remind)
@property
def is_not_mobile_client(self) -> bool:
if SKIP_MOBILE_CLIENTS_CHECK:
return True
return "iPhone" not in self.agent or "Android" not in self.agent
def is_need_to_remind(self, users_to_remind: list) -> bool:
return (
self.countryCode in FORBIDDEN_COUNTRIES
and self.id not in users_to_remind
and self.is_not_mobile_client
)
def __str__(self):
return f"User {self.username} with {self.id} is from forbidden country {self.country}"
def __repr__(self):
return self.__str__()