-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate_leaderboard.py
104 lines (82 loc) · 4.02 KB
/
update_leaderboard.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
import logging
import telegram
import flag
from constants import LEADERBOARD_UPDATE_MESSAGE, LEADERBOARD_UPDATES_SUPPORTED_COUNTRIES, USERS_BAN_LIST, \
USERS_ALLOW_LIST
from db import get_track_of_the_day, save_leaderboard, get_leaderboard
from secrets import TELEGRAM_KEY, TELEGRAM_CHAT_MESSAGE_ID
from utils import parse_leaderboard, compare_leaderboards
logging.basicConfig(filename='log.txt', filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
# datefmt='%H:%M:%S',
level=logging.DEBUG)
logging.getLogger('telegram').setLevel(logging.ERROR)
def main():
logging.info("Leaderboard updates script started!")
bot = telegram.Bot(TELEGRAM_KEY)
logging.debug(bot)
try:
saved_track = get_track_of_the_day()
logging.debug('-' * 80)
logging.debug('Track of the day: ' + str(saved_track))
previous_leaderboard = get_leaderboard()
new_leaderboard = parse_leaderboard(saved_track)
save_leaderboard(new_leaderboard)
logging.debug('-' * 80)
logging.debug('old leaderboard:')
logging.debug(previous_leaderboard)
logging.debug('-' * 80)
logging.debug('new_leaderboard:')
logging.debug(new_leaderboard)
logging.debug('-' * 80)
message_parts = []
for diff in compare_leaderboards(previous_leaderboard, new_leaderboard):
logging.debug('diff_item:')
logging.debug(diff)
# filter by country
if diff['record']['country'] in list(LEADERBOARD_UPDATES_SUPPORTED_COUNTRIES.keys()):
text_position = '#' + diff['record']['position']
improved_position = diff.get('improved_position')
if improved_position:
text_position = text_position + '(#' + improved_position + ')'
text_time = diff['record']['time'] + 's'
improved_time = diff.get('improved_time')
if improved_time:
text_time = text_time + '(-' + improved_time + 's)'
text_name = diff['record']['name']
version = diff['record']['version']
if LEADERBOARD_UPDATES_SUPPORTED_COUNTRIES.get(diff['record']['country']):
country_flag = flag.flag(LEADERBOARD_UPDATES_SUPPORTED_COUNTRIES[diff['record']['country']])
else:
country_flag = '🏳️'
message_text = LEADERBOARD_UPDATE_MESSAGE.format(country_flag, text_name, text_time, text_position)
# just to add some visibility
if version != '1.16':
message_text = message_text + ' - v' + version
# logging.debug('message_text: ' + message_text) # might have some non-ascii chars
if text_name.lower() not in USERS_BAN_LIST:
if ('*' in USERS_ALLOW_LIST) or (text_name.lower() in USERS_ALLOW_LIST):
message_parts.append(message_text)
else:
logging.debug('user not in allow list: ' + text_name)
else:
logging.debug('user banned: ' + text_name)
else:
logging.debug('not supported country: ' + diff['record']['country'])
if message_parts:
message = '\n\n'.join(message_parts)
bot.send_message(chat_id=TELEGRAM_CHAT_MESSAGE_ID, text=message, parse_mode=telegram.ParseMode.HTML)
logging.info("Leaderboard updated")
else:
logging.info("No updates!")
except Exception as error:
logging.exception('Uncaught error: ')
logging.debug(error)
import traceback
exc = traceback.format_exc()
logging.error(exc)
bot.send_message(chat_id=TELEGRAM_CHAT_MESSAGE_ID,
text='⚠️ @antonkoba Error in update_leaderboard: ' + str(exc),
parse_mode=telegram.ParseMode.HTML)
if __name__ == "__main__":
main()