forked from spoekles/aoc-discord-bot-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaocapi.py
95 lines (74 loc) · 2.84 KB
/
aocapi.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
import requests
import os
import json
import time
from models import User, Day
import operator
from dotenv import load_dotenv
class AoCAPI(object):
sortedUsers: list[User]
session_id: str
url: str
lastUpdate: float
def __init__(self):
load_dotenv()
self.sortedUsers = []
self.lastUpdate = 0
session_id = os.getenv('SESSION_ID')
url = os.getenv('LEADERBOARD_URL')
if session_id is None or url is None:
print("ERROR: Session cookie or leaderboard URL is missing")
quit()
self.session_id = session_id
self.url = url
def get_leaderboard(self) -> list[User]:
# Prevent overloading AoC api
if (self.lastUpdate != 0):
if ((time.time() - self.lastUpdate) < 900):
return self.sortedUsers
headers = {
"Cookie": f"session={self.session_id}"
}
response = requests.get(self.url, headers=headers)
# check if server is alive
if (response.status_code != 200):
raise LeaderboardError(
"AoC API responded with non-200 status code")
# checking if cookie hasnt expired
if (response.url != self.url):
# We have been redirected
raise LeaderboardError(
"AoC API responded with redirect. Check session cookie.")
response_data = response.json()
# Build the users list from the retrieved data
users: list[User] = []
for _, player in response_data["members"].items():
days = {}
# Set name to anonymous of not set
if player["name"] == None:
player["name"] = "Anonymous"
for day in range(1, 26):
day_obj: Day
if str(day) in player["completion_day_level"]:
day_stars = player["completion_day_level"][str(day)]
day_obj = Day(
day_stars["1"]["get_star_ts"] if "1" in day_stars else None,
day_stars["2"]["get_star_ts"] if "2" in day_stars else None,
)
else:
day_obj = Day(None, None)
days[day] = day_obj
users.append(User(player["name"], player["local_score"],
player["global_score"], player["stars"], days))
# Sort the users list
users.sort(key=lambda x: x.localScore, reverse=True)
# Set leaderboard positions in User objects
for i in range(len(users)):
users[i].leaderboard_position = i + 1 # 0th index is 1st on the leaderboard
self.sortedUsers = users
self.lastUpdate = time.time()
return self.sortedUsers
class LeaderboardError(Exception):
message: str
def __init__(self, message: str) -> None:
self.message = message