miui-auto-tasks/utils/api/sign.py

231 lines
6.7 KiB
Python
Raw Normal View History

"""签到实例"""
import time
from typing import Dict, List, Optional, Type, Union
from ..data_model import ApiResultHandler, DailyTasksResult, SignResultHandler
from ..request import get, post
from ..logger import log
class BaseSign:
"""
签到基类
"""
NAME = ""
"""任务名字"""
PARAMS = {}
"""签到参数"""
DATA = {}
"""签到数据"""
URL_SIGN = ""
"""签到地址"""
AVAILABLE_SIGNS: Dict[str, Type["BaseSign"]] = {}
"""可用的子类"""
def __init__(self, cookie: Dict, token: Optional[str] = None):
self.cookie = cookie
self.token = token
self.headers = {
}
async def check_daily_tasks(self, nolog: bool=False) -> Union[List[DailyTasksResult], List[None]]:
"""获取每日任务状态"""
try:
response = await get('https://api.vip.miui.com/mtop/planet/vip/member/getCheckinPageCakeList',
cookies=self.cookie)
log.debug(response.text)
result = response.json()
api_data = ApiResultHandler(result)
if api_data.success:
task_status = []
task = next(filter(lambda x: x['head']['title'] == "每日任务", api_data.data))
for daily_task in task['data']:
task_name = daily_task['title']
task_desc = daily_task.get('desc', '')
show_type = True if daily_task['showType'] == 0 else False # pylint: disable=simplifiable-if-expression
task_status.append(DailyTasksResult(name=task_name, showType=show_type, desc=task_desc))
return task_status
else:
if not nolog:
log.error(f"获取每日任务状态失败:{api_data.message}")
return []
except Exception: # pylint: disable=broad-exception-caught
if not nolog:
log.exception("获取每日任务异常")
return []
async def sign(self) -> bool:
"""
每日任务处理器
"""
try:
params = self.PARAMS.copy()
params['miui_vip_ph'] = self.cookie['miui_vip_ph'] if 'miui_vip_ph' in self.cookie else params
params['token'] = self.token if 'token' in params else params
data = self.DATA.copy()
data['miui_vip_ph'] = self.cookie['miui_vip_ph'] if 'miui_vip_ph' in self.cookie else data
if 'token' in data:
if self.token:
data['token'] = self.token
else:
log.info(f"未获取到token, 跳过{self.NAME}")
return False
response = await post(self.URL_SIGN,
params=params, data=data,
cookies=self.cookie, headers=self.headers)
log.debug(response.text)
result = response.json()
api_data = SignResultHandler(result)
if api_data:
log.success(f"{self.NAME}结果: 成长值+" + api_data.growth)
return True
elif api_data.ck_invalid:
log.error(f"{self.NAME}失败: Cookie无效")
return False
else:
log.error(f"{self.NAME}失败:" + api_data.message)
return False
except Exception: # pylint: disable=broad-exception-caught
log.exception(f"{self.NAME}出错")
return False
class CheckIn(BaseSign):
"""
每日签到
"""
NAME = "每日签到"
PARAMS = {
'ref': 'vipAccountShortcut',
'pathname': '/mio/checkIn',
'version': 'dev.231026',
'miui_vip_ph': "{miui_vip_ph}"
}
DATA = {
'miui_vip_ph': "{miui_vip_ph}",
'token': "{token}"
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/user/checkinV2'
class BrowsePost(BaseSign):
"""
浏览帖子超过10秒
"""
NAME = "浏览帖子超过10秒"
PARAMS = {
'ref': 'vipAccountShortcut',
'pathname': '/mio/detail',
'version': 'dev.231026',
'miui_vip_ph': "{miui_vip_ph}"
}
DATA = {
'action': 'BROWSE_POST_10S',
'miui_vip_ph': "{miui_vip_ph}"
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/member/addCommunityGrowUpPointByActionV2'
class BrowseUserPage(BaseSign):
"""
浏览个人主页10s
"""
NAME = "浏览个人主页10s"
PARAMS = {
'ref': 'vipAccountShortcut',
'pathname': '/mio/detail',
'version': 'dev.231026',
'miui_vip_ph': "{miui_vip_ph}"
}
DATA = {
'action': 'BROWSE_SPECIAL_PAGES_USER_HOME',
'miui_vip_ph': "{miui_vip_ph}"
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/member/addCommunityGrowUpPointByActionV2'
class BrowseSpecialPage(BaseSign):
"""
浏览指定专题页
"""
NAME = "浏览指定专题页"
PARAMS = {
'ref': 'vipAccountShortcut',
'pathname': '/mio/detail',
'version': 'dev.231026',
'miui_vip_ph': "{miui_vip_ph}"
}
DATA = {
'action': 'BROWSE_SPECIAL_PAGES_SPECIAL_PAGE',
'miui_vip_ph': "{miui_vip_ph}"
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/member/addCommunityGrowUpPointByActionV2'
class BoardFollow(BaseSign):
"""
加入小米圈子
"""
NAME = "加入小米圈子"
PARAMS = {
'pathname': '/mio/allboard',
'version': 'dev.20051',
'boardId': '558495',
'miui_vip_ph': "{miui_vip_ph}"
}
URL_SIGN = 'https://api.vip.miui.com/api/community/board/follow'
class BoardUnFollow(BaseSign):
"""
退出小米圈子
"""
NAME = "退出小米圈子"
PARAMS = {
'pathname': '/mio/allboard',
'version': 'dev.20051',
'boardId': '558495',
'miui_vip_ph': "{miui_vip_ph}"
}
URL_SIGN = 'https://api.vip.miui.com/api/community/board/unfollow'
class ThumbUp(BaseSign):
"""
点赞他人帖子
"""
NAME = "点赞他人帖子"
DATA = {
'postId': '36625780',
'sign': '36625780',
'timestamp': int(round(time.time() * 1000))
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/content/announceThumbUp'
# 注册签到任务
BaseSign.AVAILABLE_SIGNS[CheckIn.NAME] = CheckIn
BaseSign.AVAILABLE_SIGNS[BrowsePost.NAME] = BrowsePost
BaseSign.AVAILABLE_SIGNS[BrowseUserPage.NAME] = BrowseUserPage
BaseSign.AVAILABLE_SIGNS[BrowseSpecialPage.NAME] = BrowseSpecialPage
BaseSign.AVAILABLE_SIGNS[BoardFollow.NAME] = BoardFollow
BaseSign.AVAILABLE_SIGNS[BoardUnFollow.NAME] = BoardUnFollow
BaseSign.AVAILABLE_SIGNS[ThumbUp.NAME] = ThumbUp