-
Notifications
You must be signed in to change notification settings - Fork 13
/
advent_of_code.py
65 lines (53 loc) · 1.88 KB
/
advent_of_code.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
import collections
import datetime
import time
from typing import Any
import requests
import config
def check_leaderboards(bot):
last_check = getattr(config.state, 'advent_of_code_last_check', int(time.time()))
today = datetime.date.today()
year = today.year
if today.month != 12:
year -= 1
rs = requests.Session()
for leaderboard in config.bot.advent_of_code:
url = 'https://adventofcode.com/%d/leaderboard/private/view/%d.json' % (
year, leaderboard['leaderboard'])
r = rs.get(url, headers={'Cookie': 'session=' + leaderboard['session']})
r.raise_for_status()
new_completions = collections.defaultdict(list)
for member in r.json()['members'].values():
if member['name'] is None: # anonymous user
continue
last_star_ts = member['last_star_ts']
if last_star_ts == '0' or last_star_ts < last_check: # yes, it's a string sometimes
continue
for day, parts in sorted_dict(member['completion_day_level']):
day_completions = []
for part, part_info in sorted_dict(parts):
if part_info['get_star_ts'] < last_check:
continue
if part == '1':
star = '✩'
else:
star = '⭐'
day_completions.append(star)
if len(day_completions) > 0:
new_completions[member['name']].append(
'day %s %s' % (day, ''.join(day_completions)))
if new_completions:
output = '\n'.join('%s got %s' % (name, ', '.join(completions))
for name, completions in new_completions.items())
bot.send_message(leaderboard['channel'], 'advent of code', {'description': output})
config.state.advent_of_code_last_check = int(time.time())
config.state.save()
def sorted_dict(d: dict[str, Any]):
# the API returns a dict with string days/parts
# {'10': {'2': {...}}} represents day 10, part 2
return sorted(d.items(), key=lambda pair: int(pair[0]))
def main():
import bot
check_leaderboards(bot.Bot({}))
if __name__ == '__main__':
main()