Passed
Push — master ( e44069...5b9119 )
by manny
01:44
created

racetime_client.racetime_get()   A

Complexity

Conditions 4

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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