-
Notifications
You must be signed in to change notification settings - Fork 1
/
osuapi.py
99 lines (75 loc) · 2.86 KB
/
osuapi.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
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 os, sys, json, re, tkinter
from ossapi import Ossapi, User, Beatmap, Domain
from tkinter import filedialog
tkinter.Tk().withdraw()
def getBeatmapsPath():
print('Select your beatmaps directory.')
return filedialog.askdirectory()
try:
config = json.load(open('config.json'))
path = config['beatmaps_path']
except KeyError:
print('Select the your osu! beatmaps folder')
beatmaps_path = filedialog.askdirectory()
config['beatmaps_path'] = beatmaps_path
with open('config.json', 'w') as f:
f.write(json.dumps(config))
config = json.load(open('config.json'))
except FileNotFoundError:
with open('config.json', 'x') as f:
contents = {
'version': '1.4',
'beatmaps_path': getBeatmapsPath(),
# 'ID': 16965, # I think this is necessary but also ok to push with
'ID': 0,
'SECRET': ''
}
f.write(json.dumps(contents, indent=4))
config = json.load(open('config.json'))
input(
'If you have an API v2 ID and secret, enter them in the config file.')
sys.exit() # prevents it from continuing and erroring out.
if config['SECRET'] == '' or config['ID'] == 0:
input(
'If you have an API v2 ID and secret, enter them in the config file.')
sys.exit() # prevents it from continuing and erroring out.
#api = Ossapi(config['ID'],
# config['SECRET'],
# redirect_uri='http://localhost:4444/',
# grant='authorization',
# domain=Domain.OSU)
else:
api = Ossapi(config['ID'], config['SECRET'])
# returns ID and mode if available from a beatmap, user, or score URL
def convertURL(url: str):
suffix = url[19:]
params = suffix.split('/')
if params[0] == 'b' or params[0] == 'beatmaps': # beatmap [diff ID]
return [params[1]]
elif params[0] == 'beatmapsets': # beatmapset [diff ID, mode]
return [params[2], params[1].split('#')[1]]
elif params[0] == 'u' or params[0] == 'users': # user [user ID]
return [params[1]]
elif params[0] == 'scores': # score [score ID, mode]
return [params[2], params[1]]
else:
return None
def getScore(url):
# provide this yourself, functionality for lazer login may come tho
api = Ossapi(config['ID'], config['SECRET'])
scoreID = convertURL(url)
return api.score(scoreID[1], scoreID[0])
def getUser(url: str = None, username: str = None) -> User:
api = Ossapi(config['ID'], config['SECRET'])
if username:
return api.user(username)
else:
uid = convertURL(url)[0]
return api.user(uid)
def getBeatmap(url: str = None, beatmapHash: str = None) -> Beatmap:
api = Ossapi(config['ID'], config['SECRET'])
if beatmapHash:
return api.beatmap(checksum=beatmapHash)
else:
bid = convertURL(url)[0]
return api.beatmap(bid)