This repository was archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosuapi.py
More file actions
80 lines (67 loc) · 2.71 KB
/
Copy pathosuapi.py
File metadata and controls
80 lines (67 loc) · 2.71 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
"""Get useful stuff from the osu!api"""
import base64
import lzma
import time
import json
import requests
import mods
try:
with open('config.json', 'r') as f:
CONFIG = json.load(f)
OSU_API_KEY = CONFIG['osu_api_key']
f.close()
except OSError as err:
print("OS Error {0}".format(err))
def get_users_from_beatmap(beatmap_id):
"""Get users from top 100 scores."""
parameters = {"k": OSU_API_KEY, "b": beatmap_id, "m": 0}
api_request = requests.get("http://osu.ppy.sh/api/get_scores", params=parameters)
json_data = api_request.json()
users = []
for item in json_data:
enabled_mods = mods.get_mods(int(item["enabled_mods"]))
users.append([item["username"], enabled_mods])
return users
def get_replays(beatmap_id):
"""Get the replay data of a user's score on a beatmap."""
replay_data = []
for i, user in enumerate(get_users_from_beatmap(beatmap_id)):
parameters = {"k": OSU_API_KEY, "b": beatmap_id, "m": 0, "u": user[0]}
def download_replay():
"""Downloads and decodes a replay into a usable list"""
req = requests.get("https://osu.ppy.sh/api/get_replay", params=parameters).json()
try:
print("\r({}/50) Downloading {}'s replay... " .format(i+1, user[0]), end="")
replay_data.append([lzma.decompress(
base64.b64decode(req["content"])
).decode("utf-8"),user[1], user[0]])
time.sleep(7)
except KeyError as err:
if req["error"] == "Requesting too fast! Slow your operation, cap'n!":
print("Too fast! Trying again in 15 seconds...")
time.sleep(15)
download_replay()
else:
print(req)
raise err
download_replay()
print("")
return replay_data
def get_beatmap(map_hash):
"""Returns beatmap ID from given beatmap hash"""
parameters = {"k": OSU_API_KEY, "h": map_hash}
requ = requests.get("https://osu.ppy.sh/api/get_beatmaps", params=parameters).json()
return requ[0]["beatmap_id"]
def get_beatmap_info(beatmap_hash):
"""Retrieve general beatmap information."""
parameters = {"k": OSU_API_KEY, "h": beatmap_hash}
b_req = requests.get("https://osu.ppy.sh/api/get_beatmaps", params=parameters).json()
beatmap_data_string = """
------------------------------------------------
| Comparing Replays For Map:
| Artist: {}
| Title: {}
| Beatmap Id: {}
------------------------------------------------
""".format(b_req[0]['artist'], b_req[0]['title'], b_req[0]['beatmap_id'])
print(beatmap_data_string)