-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.py
48 lines (38 loc) · 1.42 KB
/
API.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
import requests
from datetime import datetime, timedelta
from Config import *
class Osu:
TOKEN = ""
def __init__(self, token):
self.EXPIRES = datetime.now()
self.TOKEN = None
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 getToken(self):
r = requests.post(
"https://osu.ppy.sh/oauth/token",
data={
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "client_credentials",
"scope": "public",
}
).json()
self.EXPIRES = datetime.now() + timedelta(seconds=r["expires_in"])
return r["access_token"]
def getMpInfo(self, mplink, before=""):
return requests.get(f"https://osu.ppy.sh/api/v2/matches/{mplink}?before={before}",
headers={"Authorization": f"Bearer {self.TOKEN}"}, data={}).json()
def getLobby(self):
return requests.get(f"https://osu.ppy.sh/api/v2/matches",
headers={"Authorization": f"Bearer {self.TOKEN}"}, data={}).json()