-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (96 loc) · 3.28 KB
/
main.py
File metadata and controls
134 lines (96 loc) · 3.28 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import logging
from datetime import datetime
from typing import List
from telegram import ParseMode, TelegramError
from telegram.ext import Updater, PicklePersistence, CallbackContext, JobQueue
import config
from resources import *
to_check = {
'news': News(),
'notifications': Notifications()
}
logger = logging.getLogger('Main')
logger.setLevel(logging.DEBUG)
def set_latest_items(context: CallbackContext) -> None:
if is_set(context):
return
for name, klass in to_check.items():
klass.log(logging.INFO, 'Setting latest item')
try:
set_latest_url(context, name, klass.get_latest_item())
klass.log(logging.INFO, 'Latest item has been set')
except GUException:
klass.log(logging.ERROR, 'cannot set data')
jq.run_once(set_latest_items, 60)
break
def do_job(context: CallbackContext) -> None:
if not is_set(context):
logger.debug('Waiting for latest items to be set')
return
for name, klass in to_check.items():
try:
latest = klass.get_latest_item()
if latest.url == (latest_url := get_latest_url(context, name)):
continue
items = fetch_items(klass, latest_url)
for item in reversed(items):
send_to_channel(context, item)
set_latest_url(context, name, items[0])
except GUException:
klass.log(logging.ERROR, "Couldn't fetch the data")
def send_to_channel(context: CallbackContext, item: Item):
try:
context.bot.send_photo(
config.CHANNEL,
item.image_url,
build_caption(item),
parse_mode=ParseMode.MARKDOWN
)
except TelegramError as err:
logger.error('Something went wrong. could not send item to the channel', exc_info=err)
def build_caption(item: Item):
caption = f"""
🔹 [{item.title}]({item.url})
📅 {item.date}
"""
return caption
def fetch_items(klass: Base, latest_url: str) -> List[Item]:
fetched_items: List[Item] = []
klass_items = klass.get_all_items()
page = 1
while True:
if len(klass_items) == 0:
page += 1
klass_items = klass.get_all_items(page)
item = klass_items.pop(0)
if item.url == latest_url:
break
fetched_items.append(item)
return fetched_items
def set_latest_url(context: CallbackContext, name: str, item: Item) -> None:
context.bot_data[name] = item.url
def get_latest_url(context: CallbackContext, name: str) -> str:
return context.bot_data.get(name, None)
def is_set(context: CallbackContext) -> bool:
return get_latest_url(context, list(to_check.keys())[0]) is not None
if __name__ == '__main__':
updater: Updater = Updater(
config.TOKEN,
persistence=PicklePersistence(
config.FILENAME,
store_user_data=False,
store_chat_data=False,
)
)
jq: JobQueue = updater.job_queue
jq.run_once(set_latest_items, 5)
now_minute = datetime.now().minute
if (first := (config.INTERVAL - (now_minute % config.INTERVAL))) == config.INTERVAL:
first = 0
jq.run_repeating(
do_job,
config.INTERVAL * 60,
first * 60
)
jq.start()
updater.idle()