miui-auto-tasks/utils/api/sign.py
Night-stars-1 56c27977da
feat: 重构项目 (#138)
* 修复无法获取成长值的问题

更改成长值获取接口

* 修复无法获取成长值的问题

更改成长值获取接口

* 改用正则获取成长值

* 推送配置说明

* 更新推送

* 更新推送

* Update config.yaml

* Update config.yaml

* Update requirements.txt

* Update config.yaml

* format the file

* 提升版本号

* 提升版本号

* 提升版本号

* 修复取消点赞出错

* feat: 重构项目

* chore: add Exception

* add: repo report link

* feat: 添加token的获取

* chore: 修正签到参数

* Delete utils directory

* Delete config.yaml

* Delete miuitask.py

* feat: 重构项目

* Create __init__.py

* feat: 重构项目

* Create 1

* feat: 重构项目

* add: 添加cryptography依赖

* Delete utils/api/1

* chore: 使用yaml储存数据

* chore: 添加token异常提示
chore: 修正部分数据结构

* imp: add system info output

* imp: auto reformat by PyCharm

* imp: 增加鸣谢

* imp: 修改主程序名称

* fix: 添加被删除的文件

* imp: 根据新版本 修改GitHub Action 脚本

* add: 添加tenacity重试库,部分get_token添加重试机制
chore: 将token.py重命名为utils.py
chore: 添加captcha.py,用于用户自行解决验证码

* 增加信息显示

增加显示项目信息及系统信息

* imp: 修复github action 脚本

* imp: auto reformat by PyCharm

---------

Co-authored-by: 0-8-4 <ljd69154@liangjundi.cn>
Co-authored-by: TardisX <ranoklx@gmail.com>
2023-11-14 00:18:59 +11:00

220 lines
6.2 KiB
Python

import time
from typing import Dict, List, Optional, Set, 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) -> 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', '')
showType = True if daily_task['showType'] == 0 else False
task_status.append(DailyTasksResult(name=task_name, showType=showType, desc=task_desc))
return task_status
else:
log.error("获取每日任务状态失败:" + api_data.message)
return []
except Exception as e:
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
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:
log.exception(f"{self.NAME}出错")
return False
class Check_In(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': ""
}
URL_SIGN = 'https://api.vip.miui.com/mtop/planet/vip/user/checkinV2'
class Browse_Post(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 Browse_User_Page(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 Browse_Special_Page(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 Board_Follow(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 Board_UnFollow(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 Thumb_Up(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[Check_In.NAME] = Check_In
BaseSign.AVAILABLE_SIGNS[Browse_Post.NAME] = Browse_Post
BaseSign.AVAILABLE_SIGNS[Browse_User_Page.NAME] = Browse_User_Page
BaseSign.AVAILABLE_SIGNS[Browse_Special_Page.NAME] = Browse_Special_Page
BaseSign.AVAILABLE_SIGNS[Board_Follow.NAME] = Board_Follow
BaseSign.AVAILABLE_SIGNS[Board_UnFollow.NAME] = Board_UnFollow
BaseSign.AVAILABLE_SIGNS[Thumb_Up.NAME] = Thumb_Up