33import time
44
55from typing import Dict , List , Optional , Type , Union , Any
6+ from tenacity import RetryError , Retrying , stop_after_attempt
67
78from ..data_model import ApiResultHandler , DailyTasksResult , SignResultHandler
89from ..request import get , post
910from ..logger import log
11+ from ..utils import is_incorrect_return
1012
1113
1214class BaseSign :
@@ -37,28 +39,32 @@ def __init__(self, cookie: Dict, token: Optional[str] = None):
3739 async def check_daily_tasks (self , nolog : bool = False ) -> Union [List [DailyTasksResult ], List [None ]]:
3840 """获取每日任务状态"""
3941 try :
40- response = await get ('https://api.vip.miui.com/mtop/planet/vip/member/getCheckinPageCakeList' ,
41- cookies = self .cookie )
42- log .debug (response .text )
43- result = response .json ()
44- api_data = ApiResultHandler (result )
45- if api_data .success :
46- task_status = []
47- tasks : List [Dict [str , List [Dict [str , Any ]]]] = list (filter (
48- lambda x : x ['head' ]['title' ] in ["每日任务" , "其他任务" ], api_data .data ))
49- for task in tasks :
50- for daily_task in task ['data' ]:
51- task_name = daily_task ['title' ]
52- task_desc = daily_task .get ('desc' , '' )
53- show_type = True if daily_task ['showType' ] == 0 else False # pylint: disable=simplifiable-if-expression
54- task_status .append (DailyTasksResult (name = task_name , showType = show_type , desc = task_desc ))
55- return task_status
42+ for attempt in Retrying (stop = stop_after_attempt (3 )):
43+ with attempt :
44+ response = await get ('https://api.vip.miui.com/mtop/planet/vip/member/getCheckinPageCakeList' ,
45+ cookies = self .cookie )
46+ log .debug (response .text )
47+ result = response .json ()
48+ api_data = ApiResultHandler (result )
49+ if api_data .success :
50+ task_status = []
51+ tasks : List [Dict [str , List [Dict [str , Any ]]]] = list (filter (
52+ lambda x : x ['head' ]['title' ] in ["每日任务" , "其他任务" ], api_data .data ))
53+ for task in tasks :
54+ for daily_task in task ['data' ]:
55+ task_name = daily_task ['title' ]
56+ task_desc = daily_task .get ('desc' , '' )
57+ show_type = True if daily_task ['showType' ] == 0 else False # pylint: disable=simplifiable-if-expression
58+ task_status .append (DailyTasksResult (name = task_name , showType = show_type , desc = task_desc ))
59+ return task_status
60+ else :
61+ if not nolog :
62+ log .error (f"获取每日任务状态失败:{ api_data .message } " )
63+ return []
64+ except RetryError as error :
65+ if is_incorrect_return (error ):
66+ log .exception (f"每日任务 - 服务器没有正确返回 { response .text } " )
5667 else :
57- if not nolog :
58- log .error (f"获取每日任务状态失败:{ api_data .message } " )
59- return []
60- except Exception : # pylint: disable=broad-exception-caught
61- if not nolog :
6268 log .exception ("获取每日任务异常" )
6369 return []
6470
@@ -67,40 +73,44 @@ async def sign(self) -> bool:
6773 每日任务处理器
6874 """
6975 try :
70- params = self .PARAMS .copy ()
71- params ['miui_vip_ph' ] = self .cookie ['miui_vip_ph' ] if 'miui_vip_ph' in self .cookie else params
72- params ['token' ] = self .token if 'token' in params else params
73- data = self .DATA .copy ()
74- data ['miui_vip_ph' ] = self .cookie ['miui_vip_ph' ] if 'miui_vip_ph' in self .cookie else data
75- if 'token' in data :
76- if self .token :
77- data ['token' ] = self .token
78- else :
79- log .info (f"未获取到token, 跳过{ self .NAME } " )
80- return False
81- response = await post (self .URL_SIGN ,
82- params = params , data = data ,
83- cookies = self .cookie , headers = self .headers )
84- log .debug (response .text )
85- result = response .json ()
86- api_data = SignResultHandler (result )
87- if api_data :
88- if api_data .growth :
89- log .success (f"{ self .NAME } 结果: 成长值+{ api_data .growth } " )
90- else :
91- log .success (f"{ self .NAME } 结果: { api_data .message } " )
92- return True
93- elif api_data .ck_invalid :
94- log .error (f"{ self .NAME } 失败: Cookie无效" )
95- return False
76+ for attempt in Retrying (stop = stop_after_attempt (3 )):
77+ with attempt :
78+ params = self .PARAMS .copy ()
79+ params ['miui_vip_ph' ] = self .cookie ['miui_vip_ph' ] if 'miui_vip_ph' in self .cookie else params
80+ params ['token' ] = self .token if 'token' in params else params
81+ data = self .DATA .copy ()
82+ data ['miui_vip_ph' ] = self .cookie ['miui_vip_ph' ] if 'miui_vip_ph' in self .cookie else data
83+ if 'token' in data :
84+ if self .token :
85+ data ['token' ] = self .token
86+ else :
87+ log .info (f"未获取到token, 跳过{ self .NAME } " )
88+ return False
89+ response = await post (self .URL_SIGN ,
90+ params = params , data = data ,
91+ cookies = self .cookie , headers = self .headers )
92+ log .debug (response .text )
93+ result = response .json ()
94+ api_data = SignResultHandler (result )
95+ if api_data :
96+ if api_data .growth :
97+ log .success (f"{ self .NAME } 结果: 成长值+{ api_data .growth } " )
98+ else :
99+ log .success (f"{ self .NAME } 结果: { api_data .message } " )
100+ return True
101+ elif api_data .ck_invalid :
102+ log .error (f"{ self .NAME } 失败: Cookie无效" )
103+ return False
104+ else :
105+ log .error (f"{ self .NAME } 失败:{ api_data .message } " )
106+ return False
107+ except RetryError as error :
108+ if is_incorrect_return (error ):
109+ log .exception (f"{ self .NAME } - 服务器没有正确返回 { response .text } " )
96110 else :
97- log .error (f"{ self .NAME } 失败:{ api_data .message } " )
98- return False
99- except Exception : # pylint: disable=broad-exception-caught
100- log .exception (f"{ self .NAME } 出错" )
111+ log .exception ("{self.NAME}出错" )
101112 return False
102113
103-
104114class CheckIn (BaseSign ):
105115 """
106116 每日签到
0 commit comments