-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (125 loc) · 4.82 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# TODO: Ensure all other modes work
# TODO: Add a "fast" mode that removes the api call for mapper, also maybe the beatmap
import os
import osu
import csv
import time
import json
from dotenv import load_dotenv
score_dictionary = {}
available_modes = ["osu", "taiko", "fruits", "mania"]
threads = []
load_dotenv()
client_id = os.getenv('client_id')
client_secret = os.getenv('client_secret')
redirect_url = os.getenv('redirect_url')
with open("settings.json", "r") as f:
settings = json.load(f)
def calculate_time(length):
rounded_length = round(length)
minutes = rounded_length // 60
if rounded_length < 60:
if rounded_length < 10:
return f"0:0{rounded_length}"
return f"0:{rounded_length}"
else:
seconds = rounded_length - (minutes * 60)
if seconds < 10:
return f"{minutes}:0{seconds}"
return f"{minutes}:{seconds}"
def return_mods(mods):
string = ""
for x in mods:
string += x.mod.value
return string
client = osu.Client.from_client_credentials(client_id, client_secret, redirect_url, limit_per_minute=120, request_wait_time=0.2)
user_id = settings["user_id"]
mode = settings["mode"]
if mode not in available_modes:
print("This mode is not valid.")
exit()
elif mode == "":
mode = "osu"
try:
user = client.get_user(user_id)
print(f"Getting {user.username}'s top plays")
except:
print("This user doesn't exist")
exit()
scores = client.get_user_scores(user_id, "best", mode=mode, limit=100)
start_time = time.time()
def generate_top_play(x):
print(f"Getting top play: {x+1}")
try:
score = scores[x]
except:
return
beatmap = client.get_beatmap(score.beatmap_id)
leaderboard_spot = client.get_user_beatmap_score(score.beatmap_id, user_id, mode=mode)
try:
mapper = client.get_user(beatmap.user_id)
except:
mapper = None
dict = {
"score_number": x+1,
"user_id": user_id,
"user_country_code": user.country_code,
"beatmap_id": score.beatmap_id,
"beatmapset_id": beatmap.beatmapset_id,
"last_updated": beatmap.beatmapset.last_updated.strftime("%Y-%m-%d %H:%M:%S"),
"ranked_date": beatmap.beatmapset.ranked_date.strftime("%Y-%m-%d %H:%M:%S"),
"artist": beatmap.beatmapset.artist,
"title": beatmap.beatmapset.title,
"difficulty": beatmap.version,
"bpm": beatmap.bpm,
"od": beatmap.accuracy,
"ar": beatmap.ar,
"cs": beatmap.cs,
"hp": beatmap.drain,
"length": calculate_time(beatmap.total_length),
"drain_length": calculate_time(beatmap.hit_length),
"beatmap_mode": beatmap.mode.value,
"mapper": mapper.username if mapper != None else "Restricted",
"mapper_user_id": mapper.id if mapper != None else "Restricted",
"mapper_country_code": mapper.country_code if mapper != None else "Restricted",
"score_id": score.id,
"time": score.ended_at.strftime("%Y-%m-%d %H:%M:%S"),
"score": score.total_score,
"max_combo": score.max_combo,
"rank": score.rank.value,
"leaderboard_position": f"#{leaderboard_spot.position}",
"count_50": score.statistics.meh if score.statistics.meh else "0",
"count_100": score.statistics.ok if score.statistics.ok else "0",
"count_300": score.statistics.great if score.statistics.great else "0",
"count_miss": score.statistics.miss if score.statistics.miss else "0",
"accuracy": score.accuracy,
"mods": return_mods(score.mods) if score.mods else "None",
"pp": score.pp,
"replay": score.replay
}
score_dictionary[x+1] = dict
for i, x in enumerate(range(0, int(settings["limit"]))):
generate_top_play(x)
end_time = time.time()
print(f"The script took {end_time - start_time} seconds to run")
sorted_keys = sorted(score_dictionary.keys())
sorted_score_dictionary = {key: score_dictionary[key] for key in sorted_keys}
if settings["output"] == "csv":
with open(f"{user_id}_top_plays.csv", mode="w") as csv_file:
fieldnames = list(sorted_score_dictionary[1].keys())
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for x in sorted_score_dictionary:
writer.writerow(sorted_score_dictionary[x])
elif settings["output"] == "json":
with open(f"{user_id}_top_plays.json", "w") as json_file:
f = json.dumps(sorted_score_dictionary)
json_file.write(f)
else:
print("Output setting invalid, defaulting to csv.")
with open(f"{user_id}_top_plays.csv", mode="w") as csv_file:
fieldnames = list(sorted_score_dictionary[1].keys())
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for x in sorted_score_dictionary:
writer.writerow(sorted_score_dictionary[x])