Passed
Push — master ( b1826e...729ce0 )
by manny
01:49
created

racetime_client.get_race_by_name()   A

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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