@@ -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 ))
0 commit comments