|
1
|
|
|
from typing import Any |
|
2
|
|
|
import requests |
|
3
|
|
|
import gettext |
|
4
|
|
|
|
|
5
|
|
|
from models.category import category_from_dict |
|
6
|
|
|
from models.category_past_races import category_past_races_from_dict |
|
7
|
|
|
from models.leaderboards import leaderboards_from_dict |
|
8
|
|
|
from models.race import race_from_dict, races_from_dict |
|
9
|
|
|
from models.user import user_from_dict |
|
10
|
|
|
from models.user_past_races import user_past_races_from_dict |
|
11
|
|
|
from models.user_search import user_search_from_dict |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def script_path(): |
|
15
|
|
|
pass |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def script_description(): |
|
19
|
|
|
localedir = script_path() + "locales" |
|
20
|
|
|
lang = gettext.translation("racetime-obs", localedir=localedir) |
|
21
|
|
|
_ = lang.gettext |
|
22
|
|
|
return (_( |
|
23
|
|
|
"<p>You've loaded the incorrect script.<br><br>Please remove this file" |
|
24
|
|
|
"and add 'racetime_obs.py' instead</p>" |
|
25
|
|
|
)) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
base_url = "https://racetime.gg/" |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def racetime_get(uri: str, payload={}): |
|
32
|
|
|
headers = { |
|
33
|
|
|
'User-Agent': "oro-obs-bot_alpha" |
|
34
|
|
|
} |
|
35
|
|
|
try: |
|
36
|
|
|
with requests.get(uri, payload, headers=headers) as res: |
|
37
|
|
|
if res.status_code == 200: |
|
38
|
|
|
return res.json() |
|
39
|
|
|
except Any: |
|
40
|
|
|
return None |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
# Get Races |
|
44
|
|
|
# |
|
45
|
|
|
# URL: https://racetime.gg/races/data |
|
46
|
|
|
# Returns a list of all open and ongoing races. |
|
47
|
|
|
def get_races(): |
|
48
|
|
|
return races_from_dict(racetime_get(f'{base_url}races/data')) |
|
49
|
|
|
|
|
50
|
|
|
# Get Category |
|
51
|
|
|
# |
|
52
|
|
|
# URL: https://racetime.gg/<category>/data |
|
53
|
|
|
# Replace with the category slug, e.g. ootr. |
|
54
|
|
|
# This endpoint includes all the basic information about the category shown on |
|
55
|
|
|
# the webpage, except for past races. Current races are given in a summarised |
|
56
|
|
|
# format, full race information must be retrieved individually. |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def get_category(category: str): |
|
60
|
|
|
return category_from_dict(racetime_get(f'{base_url}{category}/data')) |
|
61
|
|
|
|
|
62
|
|
|
# Get Category Past Races |
|
63
|
|
|
# |
|
64
|
|
|
# URL: https://racetime.gg/<category>/races/data |
|
65
|
|
|
# Returns a list of all completed (finished and cancelled) races in a category. |
|
66
|
|
|
# This list is paginated, and sorted by each race's completion time |
|
67
|
|
|
# (the ended_at field), most recent first. 10 races are returned per page. |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def get_category_past_races(category: str): |
|
71
|
|
|
return category_past_races_from_dict( |
|
72
|
|
|
racetime_get(f'{base_url}{category}/races/data') |
|
73
|
|
|
) |
|
74
|
|
|
|
|
75
|
|
|
# Get Leaderboard |
|
76
|
|
|
# |
|
77
|
|
|
# URL: https://racetime.gg/<category>/leaderboards/data |
|
78
|
|
|
# Provides category leaderboard data. |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
def get_leaderboard(category: str): |
|
82
|
|
|
return leaderboards_from_dict(racetime_get( |
|
83
|
|
|
f'{base_url}{category}/leaderboards/data') |
|
84
|
|
|
) |
|
85
|
|
|
|
|
86
|
|
|
# Get Race Detail |
|
87
|
|
|
# |
|
88
|
|
|
# URL: https://racetime.gg/<category>/<race>/data |
|
89
|
|
|
# Replace with the category slug, e.g. For OoTR use ootr, and with the race |
|
90
|
|
|
# room identifier, e.g. social-kirby-4429. Typically you'll determine the race |
|
91
|
|
|
# URL by first retrieving data from one of the other endpoints, which will |
|
92
|
|
|
# point you directly to this URL. This endpoint covers everything you might |
|
93
|
|
|
# want to know about a race. All the data shown on the race page, except for |
|
94
|
|
|
# chat messages, is provided. A full breakdown of entrants is also here, which |
|
95
|
|
|
# is sorted by race status and finish position, as appropriate. |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def get_race(category: str, race: str): |
|
99
|
|
|
return race_from_dict(racetime_get(f'{base_url}{category}/{race}/data')) |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
def get_race_by_name(name: str): |
|
103
|
|
|
if name is None or name == "": |
|
104
|
|
|
return None |
|
105
|
|
|
name = name.replace(base_url, "", 1) |
|
106
|
|
|
return race_from_dict(racetime_get(f'{base_url}{name}/data')) |
|
107
|
|
|
|
|
108
|
|
|
# Get Past User Races |
|
109
|
|
|
# |
|
110
|
|
|
# URL: https://racetime.gg/user/<user>/races/data |
|
111
|
|
|
# Returns a list of all finished (but not cancelled) races that a user has |
|
112
|
|
|
# entered. This list is paginated, and sorted by each race's completion time |
|
113
|
|
|
# (the ended_at field), most recent first. 10 races are returned per page. |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
def get_user_past_races(user: str, show_entrants: bool, page: int): |
|
117
|
|
|
payload = {"show_entrants": show_entrants, "page": page} |
|
118
|
|
|
return user_past_races_from_dict(racetime_get( |
|
119
|
|
|
f'{base_url}user/{user}/races/data', payload) |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
|
|
# User Search |
|
123
|
|
|
# |
|
124
|
|
|
# URL: https://racetime.gg/user/search |
|
125
|
|
|
# Returns an array of matching user data blobs. |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
def search_users(name: str, scrim: str = None): |
|
129
|
|
|
payload = {"name": name, "scrim": scrim} |
|
130
|
|
|
return user_search_from_dict(racetime_get( |
|
131
|
|
|
f'{base_url}/user/search', payload) |
|
132
|
|
|
) |
|
133
|
|
|
|
|
134
|
|
|
# Get User |
|
135
|
|
|
# |
|
136
|
|
|
# URL: https://racetime.gg/user/<user>/data |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
def get_user(name: str): |
|
140
|
|
|
endpoint = f'{base_url}/user/{name}/data' |
|
141
|
|
|
return user_from_dict(racetime_get(endpoint)) |
|
142
|
|
|
|