-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
223 lines (189 loc) · 7.39 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from osuapi.osuapi import OsuApi
from pypresence import Presence
from config import config
from json import loads
from time import sleep
from time import time
import subprocess
import websocket
import requests
APPLICATION_ID = 1001141712623763477
OSU_LOGO = "osulogo"
OSU_MODES = ["osu", "osutaiko", "osucatch", "osumania"]
rpc = Presence(APPLICATION_ID)
rpc.connect()
api = OsuApi()
start_time = time()
def get_user(name, mode):
u = api.search_user(name)
if u is None:
return None
return api.get_user(u.id, mode)
def user_stats(user):
score = user.stats.ranked_score
if score > 1_000_000_000:
score = f"{score / 1_000_000_000:.1f}b"
elif score > 1_000_000:
score = f"{score / 1_000_000:.1f}m"
else:
score = f"{score:,}"
return f"{user.info.name} (#{user.stats.global_rank:,}) ▸ {score} score"
def map_name(map, mapper = False, diff = True):
name = f"{map['artist']} - {map['title']}"
if mapper:
name += f" ({map['mapper']})"
if diff:
name += f" [{map['difficulty']}]"
return name
def map_stats(stats):
return f"{stats['fullSR']}★ ▸ View Beatmap"
def map_url(data):
bm = data["menu"]["bm"]
gm = ["osu", "taiko", "fruits", "mania"][data["menu"]["gameMode"]]
url = f"https://osu.ppy.sh/beatmapsets/{bm['set']}#{gm}/{bm['id']}"
req = requests.get(url)
return url if req.status_code == 200 else None
def main():
next_time = time()
activity = {}
user = None
current_mode = -1
def get_activity(data):
nonlocal user, current_mode
state = data["menu"]["state"]
map = data["menu"]["bm"]
stats = map["stats"]
play = data["gameplay"]
activity = {
"buttons": [],
"assets": {
"large_image": OSU_LOGO,
"large_text": map_name(map["metadata"]),
"small_image": OSU_MODES[data['menu']['gameMode']],
"small_text": f"Playing osu!{['', 'taiko', 'catch', 'mania'][data['menu']['gameMode']]}",
}
}
mode = data["menu"]["gameMode"]
modsStr = data['menu']['mods']['str']
if mode == 0 and "RX" in modsStr:
mode = 4
elif mode == 1 and "RX" in modsStr:
mode = 5
elif mode == 2 and "RX" in modsStr:
mode = 6
if user is None or current_mode != mode:
user = get_user(config["username"], mode)
current_mode = mode
if user is not None:
activity["assets"].update({
"large_image": f"https://a.scosu.net/{user.info.id}"
})
activity["buttons"].append({
"label": user_stats(user),
"url": f"https://scosu.net/u/{user.info.id}"
})
else:
not_found = True
match state:
case 0 | 3 | 10 | 11 | 19:
activity.update({
"details": "Main menu",
"state": map_name(map["metadata"], diff = False)
})
case 1:
activity.update({
"details": "Editing a beatmap",
"state": map_name(map["metadata"], diff = False)
})
case 4 | 5 | 13:
activity.update({
"details": f"Song select (+{modsStr})",
"state": map_name(map["metadata"])
})
url = map_url(data)
if url is not None:
activity["buttons"].append({
"label": map_stats(stats),
"url": url
})
case 12:
activity.update({
"details": "In multiplayer",
"state": map_name(map["metadata"])
})
case 2:
if play["name"] == config["username"]:
activity.update({
"details": f"Playing (+{modsStr}) ▸ {play['accuracy']:.2f}% ({play['hits']['grade']['current']})",
"state": f"{play['score']:,} ▸ {play['combo']['current']:,}x ▸ {play['hits']['0']:,} misses"
})
else:
activity.update({
"details": f"Watching {play['name']} (+{modsStr}) ▸ {play['accuracy']:.2f}% ({play['hits']['grade']['current']})",
"state": f"{play['score']:,} ▸ {play['combo']['current']:,}x ▸ {play['hits']['0']:,} misses"
})
url = map_url(data)
if url is not None:
activity["buttons"].append({
"label": map_stats(stats),
"url": url
})
case 7 | 14 | 17 | 18:
if play["name"] == config["username"]:
activity.update({
"details": f"Result screen (+{modsStr}) ▸ {play['accuracy']:.2f}% ({play['hits']['grade']['current']})",
"state": f"{play['score']:,} ▸ {play['combo']['max']:,}x ▸ {play['hits']['0']:,} misses"
})
else:
activity.update({
"details": f"Result screen of {play['name']} (+{modsStr}) ▸ {play['accuracy']:.2f}% ({play['hits']['grade']['current']})",
"state": f"{play['score']:,} ▸ {play['combo']['max']:,}x ▸ {play['hits']['0']:,} misses"
})
url = map_url(data)
if url is not None:
activity["buttons"].append({
"label": map_stats(stats),
"url": url
})
case 15:
activity.update({
"details": "Browsing osu!direct",
"state": map_name(map["metadata"])
})
case _:
activity.update({
"details": "???",
"state": map_name(map["metadata"])
})
return activity
def on_message(_ws, message):
nonlocal next_time, activity
if time() < next_time:
return
next_time = time() + config["cooldown"]
data = loads(message)
new_activity = get_activity(data)
if new_activity != activity:
rpc.update(
state = new_activity.get("state", ""),
details = new_activity.get("details", ""),
large_image = new_activity["assets"]["large_image"],
large_text = new_activity["assets"]["large_text"],
small_image = new_activity["assets"]["small_image"],
small_text = new_activity["assets"]["small_text"],
buttons = new_activity.get("buttons", []),
start = start_time
)
activity = new_activity
if config["gosumemory_path"] != "":
subprocess.Popen(config["gosumemory_path"])
sleep(5)
ws = websocket.WebSocketApp(
config["ws_url"],
on_message = on_message,
on_error = lambda _ws, exc: print(type(exc).__name__, exc),
on_open = lambda _: print("Connected to websocket!")
)
ws.run_forever()
if __name__ == "__main__":
main()