Repository navigation
Expand file tree
/
Copy pathAPI.py
More file actions
99 lines (83 loc) · 2.92 KB
/
Copy pathAPI.py
File metadata and controls
99 lines (83 loc) · 2.92 KB
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
96
97
98
99
import time
import requests
from datetime import datetime, timedelta
from Config import *
class Osu:
TOKEN = ""
REQUEST_TIMEOUT = 30
REQUEST_INTERVAL = 1.0
def __init__(self, token):
self.EXPIRES = datetime.now()
self.TOKEN = None
self._last_request_at = 0.0
self.checkToken()
def checkToken(self):
if self.TOKEN is None:
self.TOKEN = self.getToken()
else:
print("Token is not None")
if self.EXPIRES < datetime.now():
print("Token is expired")
self.TOKEN = self.getToken()
else:
print("Token is not expired")
return self.TOKEN
def _wait_for_rate_limit(self):
elapsed = time.monotonic() - self._last_request_at
remaining = self.REQUEST_INTERVAL - elapsed
if remaining > 0:
time.sleep(remaining)
def _request(self, method, url, **kwargs):
while True:
self._wait_for_rate_limit()
try:
response = requests.request(
method,
url,
timeout=self.REQUEST_TIMEOUT,
**kwargs,
)
finally:
self._last_request_at = time.monotonic()
if response.status_code != 429:
return response
retry_after = response.headers.get("Retry-After", "60")
try:
wait_seconds = max(float(retry_after), self.REQUEST_INTERVAL)
except (TypeError, ValueError):
wait_seconds = 60.0
print(f"Rate limited; retrying in {wait_seconds:g} seconds", flush=True)
time.sleep(wait_seconds)
def getToken(self):
response = self._request(
"POST",
"https://osu.ppy.sh/oauth/token",
data={
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "client_credentials",
"scope": "public",
},
)
response.raise_for_status()
data = response.json()
self.EXPIRES = datetime.now() + timedelta(seconds=data["expires_in"])
return data["access_token"]
def getMpInfo(self, mplink, before=""):
response = self._request(
"GET",
f"https://osu.ppy.sh/api/v2/matches/{mplink}",
params={"before": before},
headers={"Authorization": f"Bearer {self.TOKEN}"},
)
# Preserve the API's JSON error bodies because main.py uses them to
# distinguish missing matches and expired authentication.
return response.json()
def getLobby(self):
response = self._request(
"GET",
"https://osu.ppy.sh/api/v2/matches",
headers={"Authorization": f"Bearer {self.TOKEN}"},
)
response.raise_for_status()
return response.json()