-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
82 lines (63 loc) · 2.38 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
import configparser
from time import sleep
import os
from datetime import datetime
from multiprocessing import Process
from utils import get_last_SQL_update, get_player_name
from calculations import calculate_complete_data, get_current_track_data
from plots import plot_total_standings, timedelta_to_string, print_current_ladder, print_total_ladder
from telegram_bot import TelegramBot
import messages
def main():
config = configparser.ConfigParser()
config.read("config.ini")
DIR = config.get("LOCAL_STORAGE", "dir")
if not os.path.exists(DIR):
os.makedirs(DIR)
chatbot = TelegramBot()
chatbot.start_bot()
procs = [Process(target=p, args=[chatbot]) for p in [weekly_results_process, live_updates_process]]
[p.start() for p in procs]
try:
while not sleep(1):
pass
except KeyboardInterrupt:
[p.kill() for p in procs]
chatbot.stop_bot()
raise SystemExit
def weekly_results_process(chatbot):
current_week = get_week_number()
while not sleep(1):
if not current_week == get_week_number():
current_week = get_week_number()
chatbot.send_groupchat_message("Rien ne va plus!")
chatbot.send_results_to_groupchat()
def get_week_number():
return datetime.now().isocalendar()[1]
def live_updates_process(chatbot):
while True:
last_SQL_update = get_last_SQL_update()
data = calculate_complete_data()
renew_plot(data)
renew_ladder(data)
# wait until there are new entries to the database
while last_SQL_update == get_last_SQL_update():
sleep(1) # check every second
# create and send messages based on changes
for message in compare_data_and_create_info_messages(data):
chatbot.send_groupchat_message(message, parse_mode="MARKDOWN")
def renew_plot(data):
year, week = data.dropna()["Date"].max().isocalendar()[0:2]
plot_total_standings(data, backup_name=f"standings_y{year}_w{week}")
def renew_ladder(data):
print_current_ladder(data, style="html")
print_total_ladder(data, style="html")
def compare_data_and_create_info_messages(old_data):
new_data = calculate_complete_data()
message_list = []
message_list.extend(
messages.info_about_current_weeks_ladder_changes(old_data, new_data)
)
return message_list
if __name__ == "__main__":
main()