-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.py
executable file
ยท153 lines (120 loc) ยท 4.18 KB
/
cron.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
from pathlib import Path
from random import choice
import asyncio
import datetime
import json
import os
import sys
from dotenv import load_dotenv
from telegram import Bot, constants
from telegram.helpers import escape_markdown
from logger import logger
import web_scrape
from metadata import EMOJI_HEARTS, POLL_SENTENCES
load_dotenv()
if "TELEGRAM_CHANNEL_ID" not in os.environ or "TELEGRAM_TOKEN" not in os.environ:
logger.info(
"Both 'TELEGRAM_CHANNEL_ID' and 'TELEGRAM_TOKEN' env. variables must be set."
)
sys.exit(1)
TELEGRAM_CHANNEL_ID = os.environ.get("TELEGRAM_CHANNEL_ID")
TELEGRAM_TOKEN = os.environ.get("TELEGRAM_TOKEN")
absolute_path = Path(__file__).resolve().parent
def random_choice(rand):
return choice(rand)
def check_games_today(games):
# Set today to datetime.date(YEAR, M, D) when debugging specific date
today = datetime.date.today()
if not isinstance(games, str):
for _, value in games.items():
if today == value[0].date():
logger.info("Yesh mishak!")
yield value
return False
def escape_markdown_v2(text):
return escape_markdown(text, version=2)
def create_message(*args):
# pylint: disable=unused-variable,too-many-locals
for item in args[0]:
(
scraped_date_time,
league,
home_team,
home_team_en,
home_team_url,
game_hour,
guest_team,
guest_team_en,
guest_team_url,
game_time_delta,
road_block_time,
specs_word,
specs_number,
poll,
notes,
custom_sepcs_number,
custom_road_block_time,
) = item
yield f"""
ืืฉืืง โฝ *ืืืื* ืืฉืขื *{game_hour}*
*{league}*: [{escape_markdown_v2(home_team)}]({home_team_url}) \\|\\| [{escape_markdown_v2(guest_team)}]({guest_team_url})
ืฆืคื ืืกืืืช ืืืืฉืื: *{custom_road_block_time}*
ืฆืคื ืืืืืื ืืฉืืขืจ: *{specs_word}* {custom_sepcs_number}
""", (
scraped_date_time,
home_team,
guest_team,
poll,
notes,
)
async def send(msg, token=TELEGRAM_TOKEN, chat_id=TELEGRAM_CHANNEL_ID):
async with Bot(token) as bot:
iterator = next(msg)
send_message = list(iterator[:-1])
iterated_data = iterator[-1]
scraped_date_time = iterated_data[0]
home_team = iterated_data[1]
guest_team = iterated_data[2]
poll = iterated_data[3]
notes = iterated_data[4]
if notes:
send_message.append(f"๐ฃ: {notes}\n\n")
send_message.append(
f"_ืืฉืืจืืช ืืืื ื {random_choice(EMOJI_HEARTS)} ืืชืืฉืื ืืืคื_"
)
send_message.append("\n\n")
send_message.append(
"[https://t\\.me/sammy\\_ofer\\_notification\\_channel](https://t.me/sammy_ofer_notification_channel)"
)
web_scrape.GenerateTeamsPNG(home_team, guest_team).banner()
await bot.send_photo(
chat_id,
photo=absolute_path / "banner.png",
caption="".join(send_message),
parse_mode=constants.ParseMode.MARKDOWN_V2,
)
logger.info("Telegram message sent!")
if poll == "on":
await bot.sendPoll(
chat_id,
random_choice(POLL_SENTENCES),
json.dumps([home_team, guest_team]),
disable_notification=True,
protect_content=True,
close_date=datetime.datetime.timestamp(scraped_date_time),
)
logger.info("Telegram poll sent!")
if __name__ == "__main__":
web = web_scrape.WebScrape()
scrape = web.scrape()
scraped_games = web.decoratored_games(
scrape
) # also fetches teams logos and generates static page
generated_data = check_games_today(scraped_games)
detected_games_today = list(generated_data)
message = create_message(detected_games_today)
if not detected_games_today:
logger.info("There is only one thing we say to death - Not today!")
sys.exit(0)
asyncio.run(send(message))