-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
76 lines (65 loc) · 2.36 KB
/
tools.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
72
73
74
75
76
import pymorphy2
import email.utils
from urllib.parse import urlparse
from urllib.parse import parse_qs
from datetime import datetime
# Пункты, которые игнорируем
graphs_ignore = [
"/statistics/info/type_map/",
"/statistics/info/all/",
"/statistics/delay/",
"/statistics/hour/",
"/statistics/day/",
"/info/queue/size",
"/download/steam/",
"mod_not_found_local",
"/condition/mod/",
"/update/steam/",
"/list/tags/"
"/list/mods/",
"/download/",
"start",
"/"
]
async def pars_link(link):
if link.startswith("https://steamcommunity.com/sharedfiles/filedetails/") or link.startswith(
"https://steamcommunity.com/workshop/filedetails/") or link.startswith(
"https://openworkshop.su/mod/"):
parsed = urlparse(link, "highlight=params#url-parsing")
try:
if parsed.path.startswith("/mod/"):
link = parsed.path.removeprefix("/mod/")
else:
captured_value = parse_qs(parsed.query)
link = captured_value['id'][0]
except:
link = False
return link
async def format_seconds(seconds, word: str = 'секунда') -> str:
try:
morph = pymorphy2.MorphAnalyzer()
parsed_word = morph.parse(word)[0]
res = f"{seconds} {parsed_word.make_agree_with_number(seconds).word}"
except:
res = "ERROR"
return res
async def get_name(head: str) -> str:
if head.startswith("attachment; filename="):
return head.split("attachment; filename=")[-1]
else:
return email.utils.unquote(head.split("filename*=utf-8''")[-1])
async def graf(data: list, date_key: str) -> list[dict, list]:
tab = []
output = {}
for i in data:
if i["type"] not in graphs_ignore:
if not output.get(i["type"]):
output[i["type"]] = [[], []]
output[i["type"]][1].append(i["count"])
if date_key == "date_time":
output[i["type"]][0].append(datetime.fromisoformat(i[date_key]).hour)
elif date_key == "date":
output[i["type"]][0].append(datetime.fromisoformat(i[date_key]).toordinal())
if datetime.fromisoformat(i[date_key]) not in tab:
tab.append(datetime.fromisoformat(i[date_key]))
return [output, tab]