forked from t0n/fpv-velocidrone-ua-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_leaderboard.py
145 lines (111 loc) · 4.51 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
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
from _decimal import Decimal
import telegram
import requests
import flag
from bs4 import BeautifulSoup
from telegram import ParseMode
from db import get_track_of_the_day, save_leaderboard, get_leaderboard
from secrets import TELEGRAM_KEY, TELEGRAM_CHAT_MESSAGE_ID
LEADERBOARD_UPDATE_MESSAGE = '{} <b>{}</b> - {} / <b>{}</b>' # starts with flag emoji
SUPPORTED_COUNTRIES = {
'Ukraine': 'UA',
'Russian Federation': 'RU', # do we need it?
'Belarus': 'BY',
'Poland': 'PL',
'Hungary': 'HU',
'Czech Republic': 'CZ',
}
def parse_leaderboard(track_info):
print('track_info: ' + str(track_info))
track_leaderboard_url = track_info[3]
print('track_leaderboard_url: ' + str(track_leaderboard_url))
# TODO remove
if track_leaderboard_url.endswith('All'):
track_leaderboard_url = track_leaderboard_url.replace('/All', '/1.16')
response = requests.get(track_leaderboard_url)
soup = BeautifulSoup(response.content, 'html.parser')
records = []
rows = soup.find('tbody').findAll('tr')
for row in rows:
# print('row: ' + str(row))
cells = row.findAll('td')
new_record = {
'position': cells[0].text,
'time': cells[1].text,
'name': cells[2].text.strip(),
'country': cells[3].text.strip(),
'ranking': cells[4].text,
'model': cells[5].text.strip(),
'date': cells[6].text,
'version': cells[7].text if len(cells) > 7 else '1.16', # okay there might be no version here
}
print('new_record: ' + str(new_record))
records.append(new_record)
return records
def compare_leaderboards(old, new):
updates = []
for new_record in new:
old_record_found = False
# Update: sometimes there are 2 records by same player but different versions, so we need to dedup
for old_record in old:
# TODO remove this later
if 'version' not in old_record:
old_record['version'] = -1
if old_record['name'] == new_record['name'] and old_record['version'] == new_record['version']:
old_record_found = True
if Decimal(new_record['time']) < Decimal(old_record['time']):
updates.append({
'record': new_record,
'improved_time': str(Decimal(old_record['time']) - Decimal(new_record['time'])),
'improved_position': old_record['position'],
})
if not old_record_found:
updates.append({
'record': new_record,
})
return updates
def main():
print("Leaderboard updates script started!")
bot = telegram.Bot(TELEGRAM_KEY)
print(bot)
saved_track = get_track_of_the_day()
print('-' * 80)
print('Track of the day: ' + str(saved_track))
previous_leaderboard = get_leaderboard()
new_leaderboard = parse_leaderboard(saved_track)
save_leaderboard(new_leaderboard)
print('-' * 80)
print('old leaderboard:')
print(previous_leaderboard)
print('-' * 80)
print('new_leaderboard:')
print(new_leaderboard)
message_parts = []
for diff in compare_leaderboards(previous_leaderboard, new_leaderboard):
print(diff)
# filter by country
if diff['record']['country'] in list(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']
country_flag = flag.flag(SUPPORTED_COUNTRIES[diff['record']['country']])
message_text = LEADERBOARD_UPDATE_MESSAGE.format(country_flag, text_name, text_time, text_position)
print('message_text: ' + message_text)
message_parts.append(message_text)
else:
print('not supported country: ' + diff['record']['country'])
if message_parts:
message = '\n\n'.join(message_parts)
print('-' * 80)
print('message: ' + str(message))
bot.send_message(chat_id=TELEGRAM_CHAT_MESSAGE_ID, text=message, parse_mode=ParseMode.HTML)
else:
print('No updates!')
if __name__ == "__main__":
main()