-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
71 lines (60 loc) · 2.47 KB
/
Database.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pyairtable import Table, Base
import random
class Database:
def __init__(self, api_key: str, base_id: str):
self.__api_key = api_key
self.__base_id = base_id
self.__base = Base(api_key, base_id)
def statistic(self) -> dict:
"""
:return: dict of statistic data
"""
all_prisoners = sorted(self.__base.all("prisoners"),
key=lambda x: x["fields"]["friendsCount"])
return {"prisoners_count": len(all_prisoners),
"friends_count": len(self.__base.all("users")),
"tasks": len(self.__base.all("tasks")),
"less_friends": all_prisoners[:3]}
def get_random_prisoner(self) -> dict:
data = self.__base.all("prisoners")
return data[random.randint(0, len(data))]
def get_prisoners_by_city(self) -> dict:
data = self.__base.all("prisoners")
city_base = self.__base.all("city")
cities = {i["fields"]["city"][0] if "city" in i["fields"] else "None" for i in data}
id_to_name = dict()
for i in cities:
for j in city_base:
if j["id"] == i:
id_to_name[i] = j["fields"]["name"]
break
city_prisoners = dict()
city_prisoners["None"] = list()
for i in data:
if "city" in i["fields"]:
if id_to_name[i["fields"]["city"][0]] in city_prisoners:
city_prisoners[id_to_name[i["fields"]["city"][0]]].append(i)
else:
city_prisoners[id_to_name[i["fields"]["city"][0]]] = [i, ]
else:
city_prisoners["None"].append(i)
return city_prisoners
def find_prisoner(self, string):
data = self.__base.all("prisoners")
answer = []
for i in data:
if "name" in i["fields"] and string.lower() in i["fields"]["name"].lower():
answer.append(i)
if "prison" in i["fields"] and string.lower() in i["fields"]["prison"][0].lower():
answer.append(i)
return answer
def get_page(self, nick):
data = self.__base.all("prisoners")
for i in data:
if "shortName" in i["fields"] and nick in i["fields"]["shortName"]:
return i
if __name__ == "__main__":
import toml
config = toml.load("secrets.toml")
db = Database(config["api_key"], config["base_id"])
print(db.get_page('splonis')['fields']['name'])