-
Notifications
You must be signed in to change notification settings - Fork 0
/
osu.py
167 lines (129 loc) · 4.58 KB
/
osu.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
# osu!game functions
import config
import diskcache
import httpapi
osuctx = diskcache.Cache("osuctx")
osuuser = diskcache.Cache("osuuser")
# store player data
cabbageApi = httpapi.HttpGetAPI("https://www.mothership.top/api/v1")
cabbageBindInformationApi = cabbageApi.endpoint("/user/qq")
cabbageRecentImageApi = cabbageApi.endpoint("/")
osuApi = httpapi.HttpGetAPI("https://osu.ppy.sh/api/")
osuGetUserApi = osuApi.endpoint("get_user")
# APIs
class GameMode:
def __init__(self, id):
self.id = id
def __repl__(self):
if self == STD:
return "osu!"
elif self == TAIKO:
return "osu!Taiko"
elif self == CTB:
return "osu!CatchTheBeat"
elif self == MANIA:
return "osu!mania"
else:
return "Unknown GameMode(id={})".format(self.id)
@staticmethod
def getMode(m):
m = str(m).lower()
if m in modemap:
return modemap[m]
STD = GameMode(0)
TAIKO = GameMode(1)
CTB = GameMode(2)
MANIA = GameMode(3)
modemap = {
"s": STD, "0": STD, "std": STD, "osu!standard": STD, "osu!": STD,
"t": TAIKO, "1": TAIKO, "taiko": TAIKO, "osu!taiko": TAIKO,
"c": CTB, "2": CTB, "ctb": CTB, "catchthebeat": CTB, "osu!ctb": CTB, "osu!catchthebeat": CTB,
"m": MANIA, "3": MANIA, "mania": MANIA, "osu!mania": MANIA
}
class OsuUser:
osu_data = {}
def __refreshCache(self, key):
osuuser.set(key, self, expire=86400)
# set expire time to 1 day
@property
def osuid(self):
return -1 if len(self.osu_data) == 0 else int(list(self.osu_data.values())[0]["user_id"])
def __init__(self, uid = None, username = None, init = True):
if not uid and not username:
raise ValueError("Uid or username must be specific.")
self.uid = uid
self.username = username
self.__refreshCache(self.osuid)
async def updateUserData(self):
for i in range(0, 4):
await self.getOsuUserData(self.uid if self.uid else self.username, GameMode.getMode(i), "id" if self.uid else "string")
async def getOsuUserData(self, user, mode = STD, type = "id"):
rep = (await osuGetUserApi.call({
"k": config.osu_api_key,
"u": user,
"m": mode.id,
"type": type
}))
rep = rep.json()
if len(rep) == 0:
return False, -1
else:
self.osu_data[mode.id] = rep[0]
self.__refreshCache(self.osuid)
return True, 0
@staticmethod
def getUser(uid):
return osuuser[uid] if uid in osuuser else OsuUser(uid)
class OsuContext:
qq = -1
cabbage_data = None
osu_user = None
def __refreshCache(self, key):
osuctx.set(key, self, expire=86400)
# set expire time to 1 day
def __init__(self, qq):
self.qq = qq
self.__refreshCache(self.qq)
def updateUser(self):
self.osu_user = OsuUser.getUser(self.osuid)
@staticmethod
def getContext(qq):
return osuctx[qq] if qq in osuctx else OsuContext(qq)
@property
def osuid(self):
return -1 if not self.cabbage_data else self.cabbage_data["data"]["userId"]
@property
def osuname(self):
return "" if not self.cabbage_data else self.cabbage_data["data"]["currentUname"]
async def getCabbageBindInformation(self):
if not self.cabbage_data:
rep = (await cabbageBindInformationApi.endpoint("/{}".format(self.qq)).call()).json()
if rep['code'] != 0:
return False, rep['code']
self.cabbage_data = rep
self.__refreshCache(self.qq)
return True, 0
def drawrecent(uid):
pass
async def recent(cls, phrase, message):
uid = 0
mode = STD
if phrase.get("~query", None):
# get uid from username
pass
if phrase.get("~mode", None):
mode = GameMode.getMode(phrase.get("~mode"))
ctx = OsuContext.getContext(int(message["qq"]))
await ctx.getCabbageBindInformation()
print(osuctx.get(int(message["qq"])).cabbage_data)
async def osudebug(cls, phrase, message):
ctx = OsuContext.getContext(int(message["qq"]))
await ctx.getCabbageBindInformation()
ctx.updateUser()
await ctx.osu_user.updateUserData()
result = "Username: {name}\nUID: {id}\nCabbage Data: {cabbage}\n osu![/get_user] Data: {osuu}"
await cls.sendMessage(message['type'], message['group'], message['qq'], result.format(\
name = ctx.osuname,\
id = ctx.osuid,\
cabbage = ctx.cabbage_data,\
osuu = ctx.osu_user.osu_data))