-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (76 loc) · 2.68 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import re
import json
from time import sleep
from telegram.ext import Updater
from telegram import Bot, Chat, InputMediaDocument
from telegram.error import BadRequest, RetryAfter
from cred import BOT_KEY, CHANNEL_NAME, DOWNLOADS
from scraper import scrape
updater = Updater(token=BOT_KEY)
scraped = []
rankings_generated = []
def load_data():
global scraped
global rankings_generated
if DOWNLOADS not in os.listdir():
os.mkdir(DOWNLOADS)
if "data.json" in os.listdir(DOWNLOADS):
with open(f"{DOWNLOADS}/data.json", "r") as fd:
data = json.loads(fd.read())
scraped = data[0]
rankings_generated = data[1]
def save_data():
global scraped
global rankings_generated
if DOWNLOADS not in os.listdir():
os.mkdir(DOWNLOADS)
with open(f"{DOWNLOADS}/data.json", "w") as fd:
fd.write(json.dumps([scraped, rankings_generated]))
def generate_caption(caption):
caption = "📜 " + caption + "\n\n▶️ @studenti_unimi | studentiunimi.it"
exp = re.compile(r"(?:\(CLASSE*\s)(\w+-\w+)", re.IGNORECASE)
matches = exp.findall(caption)
if len(matches) > 0:
match = matches[0]
caption = caption.replace(match, f"#{match.replace('-', '_')}")
return caption
def main():
try:
os.mkdir(DOWNLOADS)
except FileExistsError:
pass
load_data()
bot: Bot = updater.bot
chat: Chat = bot.get_chat(CHANNEL_NAME)
docs, rankings = scrape()
for section in docs:
to_send = []
for pdf in section[0]:
if pdf not in scraped:
to_send.append(pdf)
send_group = []
for pdf in to_send:
with open(f"{DOWNLOADS}/{pdf}", "rb") as fd:
input_document = InputMediaDocument(fd, filename=pdf[33:])
send_group.append(input_document)
scraped.append(pdf)
if rankings[section[1]] not in rankings_generated:
with open(f"{DOWNLOADS}/{rankings[section[1]]}", "rb") as fd:
send_group.append(InputMediaDocument(fd, filename=rankings[section[1]]))
rankings_generated.append(rankings[section[1]])
if len(send_group) == 0:
continue
send_group[len(send_group) - 1].caption = generate_caption(section[1])
print("Sending", section[1])
while True:
try:
chat.send_media_group(send_group, disable_notification=True) # Sometimes it raises a BadRequest not sure why
break
except RetryAfter as e:
sleep(e.retry_after + 2)
except BadRequest:
sleep(10)
save_data()
if __name__ == "__main__":
main()