Skip to content

Commit 8195191

Browse files
committed
feat: fix cookies checking
Update pyproject.toml
1 parent baf00e9 commit 8195191

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

maimai_py/maimai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,10 @@ async def updates_chain(
12171217
if source_mode == "parallel" or (source_mode == "fallback" and len(source_tasks) == 0):
12181218
source_task = asyncio.create_task(self.scores(ident, sp))
12191219
if source_callback is not None:
1220-
source_task.add_done_callback(lambda t, k=kwargs: source_callback(t.result(), t.exception(), k))
1220+
empty_scores = await MaimaiScores(self).configure([])
1221+
source_task.add_done_callback(
1222+
lambda t, k=kwargs: source_callback(t.result() if not t.exception() else empty_scores, t.exception(), k)
1223+
)
12211224
source_tasks.append(source_task)
12221225
source_gather_results = await asyncio.gather(*source_tasks, return_exceptions=True)
12231226
maimai_scores_list = [result for result in source_gather_results if isinstance(result, MaimaiScores)]

maimai_py/providers/wechat.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class WechatProvider(IScoreProvider, IPlayerProvider, IPlayerIdentifierProvider,
3131
def _hash(self) -> str:
3232
return hashlib.md5(b"wechat").hexdigest()
3333

34+
@staticmethod
35+
def _ensure_cookies(identifier: PlayerIdentifier) -> Cookies:
36+
if isinstance(identifier.credentials, dict):
37+
return Cookies(identifier.credentials)
38+
if isinstance(identifier.credentials, Cookies):
39+
return identifier.credentials
40+
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to fetch data")
41+
3442
@staticmethod
3543
async def _deser_score(score: HTMLScore, songs: "MaimaiSongs") -> Optional[Score]:
3644
if song := await songs.by_title(score.title):
@@ -70,17 +78,13 @@ async def _crawl_scores(self, client: "MaimaiClient", cookies: Cookies, maimai_s
7078

7179
async def get_scores_all(self, identifier: PlayerIdentifier, client: "MaimaiClient") -> list[Score]:
7280
maimai_songs = await client.songs() # Ensure songs are loaded in cache
73-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
74-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to fetch scores")
75-
scores = await self._crawl_scores(client, identifier.credentials, maimai_songs)
81+
scores = await self._crawl_scores(client, self._ensure_cookies(identifier), maimai_songs)
7682
return list(scores)
7783

7884
@retry(stop=stop_after_attempt(3), retry=retry_if_exception_type(RequestError), reraise=True)
7985
async def get_player(self, identifier: PlayerIdentifier, client: "MaimaiClient") -> WechatPlayer:
8086
trophies = await client.items(PlayerTrophy)
81-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
82-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to fetch player information")
83-
resp = await client._client.get("https://maimai.wahlap.com/maimai-mobile/friend/userFriendCode/", cookies=identifier.credentials)
87+
resp = await client._client.get("https://maimai.wahlap.com/maimai-mobile/friend/userFriendCode/", cookies=self._ensure_cookies(identifier))
8488
if resp.status_code == 302:
8589
raise InvalidPlayerIdentifierError("Failed to fetch player information, possibly due to invalid cookies or maintenance.")
8690
player = wmdx_html2player(str(resp.text))
@@ -112,16 +116,15 @@ async def get_identifier(self, code: Union[str, dict[str, str]], client: "Maimai
112116
@retry(stop=stop_after_attempt(3), retry=retry_if_exception_type(RequestError), reraise=True)
113117
async def get_friends(self, identifier: PlayerIdentifier, client: "MaimaiClient") -> list[WechatPlayer]:
114118
trophies = await client.items(PlayerTrophy)
115-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
116-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to fetch friends")
119+
cookies = self._ensure_cookies(identifier)
117120
# Prefetch the first page of friends, in order to get the total number of friends
118-
resp = await client._client.get("https://maimai.wahlap.com/maimai-mobile/friend/", cookies=identifier.credentials)
121+
resp = await client._client.get("https://maimai.wahlap.com/maimai-mobile/friend/", cookies=cookies)
119122
if resp.status_code == 302:
120123
raise InvalidPlayerIdentifierError("Failed to fetch friends, possibly due to invalid cookies or maintenance.")
121124
friend_num, players = wmdx_html2players(str(resp.text))
122125
# Fetch all friends by iterating through pages, 10 friends per page
123126
for page in range(2, (friend_num // 10) + 2):
124-
resp = await client._client.get(f"https://maimai.wahlap.com/maimai-mobile/friend/pages/?idx={page}", cookies=identifier.credentials)
127+
resp = await client._client.get(f"https://maimai.wahlap.com/maimai-mobile/friend/pages/?idx={page}", cookies=cookies)
125128
players.extend(wmdx_html2players(str(resp.text))[1])
126129
return [
127130
WechatPlayer(
@@ -137,32 +140,26 @@ async def get_friends(self, identifier: PlayerIdentifier, client: "MaimaiClient"
137140

138141
@retry(stop=stop_after_attempt(3), retry=retry_if_exception_type(RequestError), reraise=True)
139142
async def set_rival_on(self, identifier: PlayerIdentifier, rival: WechatPlayer, client: "MaimaiClient") -> None:
140-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
141-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to set rival status")
142143
resp = await client._client.post(
143144
"https://maimai.wahlap.com/maimai-mobile/friend/rivalOn/",
144-
cookies=identifier.credentials,
145+
cookies=self._ensure_cookies(identifier),
145146
data={"idx": rival.friend_code, "token": rival.token},
146147
)
147148
resp.raise_for_status()
148149

149150
@retry(stop=stop_after_attempt(3), retry=retry_if_exception_type(RequestError), reraise=True)
150151
async def set_rival_off(self, identifier: PlayerIdentifier, rival: WechatPlayer, client: "MaimaiClient") -> None:
151-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
152-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to set rival status")
153152
resp = await client._client.post(
154153
"https://maimai.wahlap.com/maimai-mobile/friend/rivalOff/",
155-
cookies=identifier.credentials,
154+
cookies=self._ensure_cookies(identifier),
156155
data={"idx": rival.friend_code, "token": rival.token},
157156
)
158157
resp.raise_for_status()
159158

160159
@retry(stop=stop_after_attempt(3), retry=retry_if_exception_type(RequestError), reraise=True)
161160
async def get_records(self, identifier: PlayerIdentifier, client: "MaimaiClient") -> list[Score]:
162161
maimai_songs = await client.songs() # Ensure songs are loaded in cache
163-
if not identifier.credentials or not isinstance(identifier.credentials, Cookies):
164-
raise InvalidPlayerIdentifierError("Wahlap wechat cookies are required to fetch scores")
165-
resp = await client._client.get(f"https://maimai.wahlap.com/maimai-mobile/record/", cookies=identifier.credentials)
162+
resp = await client._client.get(f"https://maimai.wahlap.com/maimai-mobile/record/", cookies=self._ensure_cookies(identifier))
166163
if resp.status_code == 302:
167164
raise InvalidPlayerIdentifierError("Failed to fetch records, possibly due to invalid cookies or maintenance.")
168165
scores = wmdx_html2record(str(resp.text))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "maimai-py"
3-
version = "1.3.7rc5"
3+
version = "1.3.7rc6"
44
description = "The definitive python wrapper for MaimaiCN."
55
authors = ["Usagi no Niku <chenbohan911@163.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)