🐛 修复自动米游币获取会阻塞的问题

This commit is contained in:
CMHopeSunshine 2022-08-06 21:57:09 +08:00
parent 1fd8ea1234
commit 9d3814c3cb
4 changed files with 243 additions and 264 deletions

View File

@ -48,8 +48,7 @@ uuid = str(uuid.uuid4())
@rm_cloud_ys.handle()
async def _handle(event: Union[GroupMessageEvent, MessageEvent], match: Matcher, args: Message = CommandArg()):
plan_text = args.extract_plain_text()
if plan_text:
if plan_text := args.extract_plain_text():
match.set_arg('choice', plan_text)
@ -60,13 +59,11 @@ async def _(event: Union[GroupMessageEvent, MessageEvent], choice: str = ArgPlai
data = load_json(Path() / 'data' / 'LittlePaimon' / 'CloudGenshin.json')
del data[user_id]
if scheduler.get_job('cloud_genshin_' + user_id):
scheduler.remove_job("cloud_genshin_" + user_id)
if scheduler.get_job(f'cloud_genshin_{user_id}'):
scheduler.remove_job(f"cloud_genshin_{user_id}")
save_json(data, Path() / 'data' / 'LittlePaimon' / 'CloudGenshin.json')
await rm_cloud_ys.finish('token已解绑并取消自动签到~', at_sender=True)
elif choice == '':
await rm_cloud_ys.finish()
else:
await rm_cloud_ys.finish()

View File

@ -535,31 +535,23 @@ async def mys_sign_auto_handler(event: MessageEvent, msg: Message = CommandArg()
await mys_sign_auto.finish('指令错误,在后面加 开启/关闭 来使用哦', at_sender=True)
ud_lmt = FreqLimiter(300)
ud_lmt = FreqLimiter(180)
ud_p_lmt = FreqLimiter(15)
@update_info.handle()
async def _(event: MessageEvent, state: T_State, msg: Message = CommandArg()):
uid = re.search(r'(?P<uid>(1|2|5)\d{8})', msg.extract_plain_text())
if uid:
if uid := re.search(r'(?P<uid>(1|2|5|8)\d{8})', msg.extract_plain_text().strip()):
state['uid'] = uid.group('uid')
else:
user = ''
for msg_seg in msg:
if msg_seg.type == "at":
user = msg_seg.data['qq']
break
if user:
if user := next((msg_seg.data['qq'] for msg_seg in msg if msg_seg.type == "at"), ''):
uid = await get_last_query(str(user))
if uid:
state['uid'] = uid
else:
uid = await get_last_query(str(event.user_id))
if uid:
state['uid'] = uid
if uid:
state['uid'] = uid
if 'uid' in state and not ud_lmt.check(state['uid']):
await update_info.finish(f'每个uid每5分钟才能更新一次信息,请稍等一下吧~(剩余{ud_lmt.left_time(state["uid"])}秒)')
await update_info.finish(f'每个uid每3分钟才能更新一次信息,请稍等一下吧~(剩余{ud_lmt.left_time(state["uid"])}秒)')
if not ud_p_lmt.check(get_message_id(event)):
await update_info.finish(f'每个会话每15秒才能更新一次信息请稍等一下吧~(剩余{ud_lmt.left_time(get_message_id(event))}秒)')
@ -576,7 +568,7 @@ async def _(event: MessageEvent, uid: Message = Arg('uid')):
enka_data = await get_enka_data(uid)
if not enka_data:
await update_info.finish('派蒙没有获取到该uid的信息哦可能是enka接口服务出现问题稍候再试吧~')
ud_lmt.start_cd(uid, 300)
ud_lmt.start_cd(uid, 180)
ud_lmt.start_cd(get_message_id(event), 15)
player_info = PlayerInfo(uid)
player_info.set_player(enka_data['playerInfo'])
@ -593,8 +585,7 @@ async def _(event: MessageEvent, uid: Message = Arg('uid')):
@role_info.handle()
async def _(event: MessageEvent, state: T_State, msg: Message = CommandArg()):
uid = re.search(r'(?P<uid>(1|2|5)\d{8})', msg.extract_plain_text())
if uid:
if uid := re.search(r'(?P<uid>(1|2|5|8)\d{8})', msg.extract_plain_text().strip()):
state['uid'] = uid.group('uid')
await update_last_query(str(event.user_id), uid.group('uid'))
else:
@ -604,12 +595,10 @@ async def _(event: MessageEvent, state: T_State, msg: Message = CommandArg()):
user = msg_seg.data['qq']
if user:
uid = await get_last_query(str(user))
if uid:
state['uid'] = uid
else:
uid = await get_last_query(str(event.user_id))
if uid:
state['uid'] = uid
if uid:
state['uid'] = uid
msg = msg.extract_plain_text().replace(state['uid'] if 'uid' in state else 'ysd', '').strip()
if not msg:
await role_info.finish('请把要查询角色名给派蒙哦~')
@ -712,9 +701,9 @@ async def coin_auto_sign():
for user_id, uid, remind_id in data:
sk = await get_private_stoken(uid, key='uid')
stoken = sk[0][4]
get_coin_task = MihoyoBBSCoin(stoken)
data = await get_coin_task.task_run()
if get_coin_task.is_Right is False:
get_coin_task = MihoyoBBSCoin(stoken, user_id, uid)
data = await get_coin_task.run()
if get_coin_task.state is False:
await delete_coin_auto_sign(user_id, uid)
if remind_id.startswith('q'):
await get_bot().send_private_msg(user_id=remind_id[1:],
@ -811,7 +800,7 @@ async def daily_update():
# @scheduler.scheduled_job('cron', hour=3, misfire_grace_time=10)
async def all_update():
uid_list = await get_all_query()
logger.info('派蒙开始更新用户角色信息,共{}个用户'.format(len(uid_list)))
logger.info(f'派蒙开始更新用户角色信息,共{len(uid_list)}个用户')
failed_time = 0
for uid in uid_list:
try:
@ -829,7 +818,7 @@ async def all_update():
failed_time += 1
if failed_time > 5:
break
return '玩家信息uid更新共{}个,更新完成'.format(len(uid_list))
return f'玩家信息uid更新共{len(uid_list)}个,更新完成'
@get_mys_coin.handle()
@ -847,8 +836,8 @@ async def get_mys_coin_handler(event: MessageEvent, msg: Message = CommandArg())
await get_mys_coin.finish('你的该uid还没绑定cookie哦先用ysb绑定吧')
stoken = sk[0][4]
await get_mys_coin.send('开始执行米游币获取,请稍等哦~')
get_coin_task = MihoyoBBSCoin(stoken)
data = await get_coin_task.task_run()
get_coin_task = MihoyoBBSCoin(stoken, str(event.user_id), uid)
data = await get_coin_task.run()
msg = "米游币获取完成\n" + data
await get_mys_coin.finish(msg)

View File

@ -1,64 +1,68 @@
import asyncio
import random
import string
import time
from httpx import AsyncClient
from nonebot import logger
from littlepaimon_utils import aiorequests
from ..utils.auth_util import random_text, random_hex, get_old_version_ds
# 米游社的API列表
bbs_Cookieurl = 'https://webapi.account.mihoyo.com/Api/cookie_accountinfo_by_loginticket?login_ticket={}'
bbs_Cookieurl2 = 'https://api-takumi.mihoyo.com/auth/api/getMultiTokenByLoginTicket?' \
'login_ticket={}&token_types=3&uid={}'
bbs_Cookieurl2 = 'https://api-takumi.mihoyo.com/auth/api/getMultiTokenByLoginTicket?login_ticket={}&token_types=3&uid={}'
bbs_Taskslist = 'https://bbs-api.mihoyo.com/apihub/sapi/getUserMissionsState' # 获取任务列表
bbs_Signurl = 'https://bbs-api.mihoyo.com/apihub/sapi/signIn?gids={}' # post
bbs_Listurl = 'https://bbs-api.mihoyo.com/post/api/getForumPostList?' \
'forum_id={}&is_good=false&is_hot=false&page_size=20&sort_type=1'
bbs_Listurl = 'https://bbs-api.mihoyo.com/post/api/getForumPostList?forum_id={}&is_good=false&is_hot=false&page_size=20&sort_type=1'
bbs_Detailurl = 'https://bbs-api.mihoyo.com/post/api/getPostFull?post_id={}'
bbs_Shareurl = 'https://bbs-api.mihoyo.com/apihub/api/getShareConf?entity_id={}&entity_type=1'
bbs_Likeurl = 'https://bbs-api.mihoyo.com/apihub/sapi/upvotePost' # post json
bbs_Likeurl = 'https://bbs-api.mihoyo.com/apihub/sapi/upvotePost' # post json
mihoyobbs_List = [{
'id': '1',
'forumId': '1',
'name': '崩坏3',
'url': 'https://bbs.mihoyo.com/bh3/'
}, {
'id': '2',
'forumId': '26',
'name': '原神',
'url': 'https://bbs.mihoyo.com/ys/'
}, {
'id': '3',
'forumId': '30',
'name': '崩坏2',
'url': 'https://bbs.mihoyo.com/bh2/'
}, {
'id': '4',
'forumId': '37',
'name': '未定事件簿',
'url': 'https://bbs.mihoyo.com/wd/'
}, {
'id': '5',
'forumId': '34',
'name': '大别野',
'url': 'https://bbs.mihoyo.com/dby/'
}, {
'id': '6',
'forumId': '52',
'name': '崩坏:星穹铁道',
'url': 'https://bbs.mihoyo.com/sr/'
}]
def random_text(num: int) -> str:
return ''.join(random.sample(string.ascii_lowercase + string.digits, num))
mihoyo_bbs_List = [
{
'id': '1',
'forumId': '1',
'name': '崩坏3',
'url': 'https://bbs.mihoyo.com/bh3/',
},
{
'id': '2',
'forumId': '26',
'name': '原神',
'url': 'https://bbs.mihoyo.com/ys/',
},
{
'id': '3',
'forumId': '30',
'name': '崩坏2',
'url': 'https://bbs.mihoyo.com/bh2/',
},
{
'id': '4',
'forumId': '37',
'name': '未定事件簿',
'url': 'https://bbs.mihoyo.com/wd/',
},
{
'id': '5',
'forumId': '34',
'name': '大别野',
'url': 'https://bbs.mihoyo.com/dby/',
},
{
'id': '6',
'forumId': '52',
'name': '崩坏:星穹铁道',
'url': 'https://bbs.mihoyo.com/sr/',
},
]
class MihoyoBBSCoin:
def __init__(self, cookies):
self.postsList = None
self.headers = {
'DS': old_version_get_ds_token(True),
"""
米游币获取
"""
def __init__(self, cookies: str, user_id: str, uid: str):
self.headers: dict = {
'DS': get_old_version_ds(mhy_bbs=True),
'cookie': cookies,
'x-rpc-client_type': '2',
'x-rpc-app_version': '2.28.1',
@ -71,7 +75,8 @@ class MihoyoBBSCoin:
'Host': 'bbs-api.mihoyo.com',
'User-Agent': 'okhttp/4.8.0'
}
self.Task_do = {
self.postsList: list = []
self.Task_do: dict = {
'bbs_Sign': False,
'bbs_Read_posts': False,
'bbs_Read_posts_num': 3,
@ -79,199 +84,173 @@ class MihoyoBBSCoin:
'bbs_Like_posts_num': 5,
'bbs_Share': False
}
self.mihoyobbs_List_Use = []
self.Today_getcoins = 0
self.Today_have_getcoins = 0 # 这个变量以后可能会用上,先留着了
self.Have_coins = 0
self.is_Right = True # 判断stoken是否失效
self.mihoyo_bbs_List: list = mihoyo_bbs_List
self.available_coins: int = 0
self.received_coins: int = 0 # 这个变量以后可能会用上,先留着了
self.total_coins: int = 0
self.state: bool = True
self.user_id = user_id
self.uid = uid
# 开始运行
async def task_run(self):
await self.load_mihoyo_bbs_list_use()
start = await self.get_tasks_list()
if self.is_Right is False:
return start
self.postsList = await self.get_list()
sign = await self.signing()
read = await self.read_posts()
like = await self.like_posts()
share = await self.share_post()
im = start + '\n' + sign + '\n' + read + '\n' + like + '\n' + share
return im
async def load_mihoyo_bbs_list_use(self):
for i in [2, 5]:
for k in mihoyobbs_List:
if i == int(k['id']):
self.mihoyobbs_List_Use.append(k)
# 获取任务列表,用来判断做了哪些任务
async def get_tasks_list(self):
# logger.info('正在获取任务列表')
async with AsyncClient() as client:
req = await client.get(url=bbs_Taskslist, headers=self.headers)
data = req.json()
if 'err' in data['message'] or data['retcode'] == -100:
self.is_Right = False
# logger.info('stoken或cookie失效')
return '你的Cookies或Stoken已失效。'
# log.error('获取任务列表失败你的cookie可能已过期请重新设置cookie。')
else:
self.Today_getcoins = data['data']['can_get_points']
self.Today_have_getcoins = data['data']['already_received_points']
self.Have_coins = data['data']['total_points']
# 如果当日可获取米游币数量为0直接判断全部任务都完成了
if self.Today_getcoins == 0:
self.Task_do['bbs_Sign'] = True
self.Task_do['bbs_Read_posts'] = True
self.Task_do['bbs_Like_posts'] = True
self.Task_do['bbs_Share'] = True
async def run(self):
"""
执行米游币获取任务
:return: 获取消息
"""
logger.info(f'开始执行{self.user_id}的UID{self.uid}的米游币获取任务')
await self.get_tasks_list()
await self.get_list()
tasks_list = [
self.signing,
self.read_posts,
self.like_posts,
self.share_post
]
result = '米游币获取结果:\n'
for task in tasks_list:
if self.state:
msg = await task()
result += msg + '\n'
else:
# 如果第0个大于或等于62则直接判定任务没做
if data['data']['states'][0]['mission_id'] >= 62:
# logger.info(f'新的一天,今天可以获得{self.Today_getcoins}个米游币')
pass
else:
# logger.info(f'似乎还有任务没完成,今天还能获得{self.Today_getcoins}')
for i in data['data']['states']:
# 58是讨论区签到
if i['mission_id'] == 58:
if i['is_get_award']:
self.Task_do['bbs_Sign'] = True
# 59是看帖子
elif i['mission_id'] == 59:
if i['is_get_award']:
self.Task_do['bbs_Read_posts'] = True
else:
self.Task_do['bbs_Read_posts_num'] -= i['happened_times']
# 60是给帖子点赞
elif i['mission_id'] == 60:
if i['is_get_award']:
self.Task_do['bbs_Like_posts'] = True
else:
self.Task_do['bbs_Like_posts_num'] -= i['happened_times']
# 61是分享帖子
elif i['mission_id'] == 61:
if i['is_get_award']:
self.Task_do['bbs_Share'] = True
# 分享帖子,是最后一个任务,到这里了下面都是一次性任务,直接跳出循环
break
return '今日完成情况!'
return 'Cookie已失效'
return result
# 获取要帖子列表
async def get_list(self) -> list:
temp_list = []
# logger.info('正在获取帖子列表......')
async with AsyncClient() as client:
req = await client.get(url=bbs_Listurl.format(self.mihoyobbs_List_Use[0]['forumId']), headers=self.headers)
async def get_tasks_list(self):
"""
获取任务列表用来判断做了哪些任务
"""
data = await aiorequests.get(url=bbs_Taskslist, headers=self.headers)
data = data.json()
if 'err' in data['message'] or data['retcode'] == -100:
self.state = False
return
self.available_coins = data['data']['can_get_points']
self.received_coins = data['data']['already_received_points']
self.total_coins = data['data']['total_points']
# 如果当日可获取米游币数量为0直接判断全部任务都完成了
if self.available_coins == 0:
self.Task_do['bbs_Sign'] = True
self.Task_do['bbs_Read_posts'] = True
self.Task_do['bbs_Like_posts'] = True
self.Task_do['bbs_Share'] = True
else:
# 如果第0个大于或等于62则直接判定任务没做
if data['data']['states'][0]['mission_id'] < 62:
for i in data['data']['states']:
# 58是讨论区签到
if i['mission_id'] == 58:
if i['is_get_award']:
self.Task_do['bbs_Sign'] = True
# 59是看帖子
elif i['mission_id'] == 59:
if i['is_get_award']:
self.Task_do['bbs_Read_posts'] = True
else:
self.Task_do['bbs_Read_posts_num'] -= i[
'happened_times'
]
# 60是给帖子点赞
elif i['mission_id'] == 60:
if i['is_get_award']:
self.Task_do['bbs_Like_posts'] = True
else:
self.Task_do['bbs_Like_posts_num'] -= i[
'happened_times'
]
# 61是分享帖子
elif i['mission_id'] == 61:
if i['is_get_award']:
self.Task_do['bbs_Share'] = True
# 分享帖子,是最后一个任务,到这里了下面都是一次性任务,直接跳出循环
break
async def get_list(self):
"""
获取进行操作的帖子列表
:return: 帖子id列表
"""
req = await aiorequests.get(
url=bbs_Listurl.format(random.choice([bbs['forumId'] for bbs in self.mihoyo_bbs_List])),
headers=self.headers)
data = req.json()
for n in range(5):
temp_list.append([data['data']['list'][n]['post']['post_id'], data['data']['list'][n]['post']['subject']])
# logger.info('已获取{}个帖子'.format(len(temp_list)))
return temp_list
self.postsList = [[d['post']['post_id'], d['post']['subject']] for d in data['data']['list'][:5]]
# 进行签到操作
async def signing(self):
"""
讨论区签到
"""
if self.Task_do['bbs_Sign']:
# logger.info("讨论区任务已经完成过了~")
return '讨论区任务已经完成过了~'
else:
for i in self.mihoyobbs_List_Use:
async with AsyncClient() as client:
req = await client.post(url=bbs_Signurl.format(i['id']), data={}, headers=self.headers)
data = req.json()
if 'err' not in data['message']:
time.sleep(random.randint(2, 8))
else:
# logger.info("cookie或stoken已失效")
return '你的Cookies已失效。'
return 'SignM:完成!'
return '讨论区签到任务已经完成过了~'
for i in self.mihoyo_bbs_List:
req = await aiorequests.post(url=bbs_Signurl.format(i['id']), data={}, headers=self.headers)
data = req.json()
if 'err' in data['message']:
self.state = False
return
await asyncio.sleep(random.randint(3, 6))
return '讨论区签到:完成!'
# 看帖子
async def read_posts(self):
"""
浏览帖子
"""
if self.Task_do['bbs_Read_posts']:
# logger.info("看帖任务已经完成过了~'")
return '看帖任务已经完成过了~'
else:
num_ok = 0
for i in range(self.Task_do['bbs_Read_posts_num']):
async with AsyncClient() as client:
req = await client.get(url=bbs_Detailurl.format(self.postsList[i][0]), headers=self.headers)
data = req.json()
if data['message'] == 'OK':
num_ok += 1
time.sleep(random.randint(2, 8))
# logger.info('ReadM:成功!Read:{}!'.format(str(num_ok)))
return 'ReadM:成功!Read:{}!'.format(str(num_ok))
num_ok = 0
for i in range(self.Task_do['bbs_Read_posts_num']):
req = await aiorequests.get(url=bbs_Detailurl.format(self.postsList[i][0]), headers=self.headers)
data = req.json()
if data['message'] == 'OK':
num_ok += 1
await asyncio.sleep(random.randint(2, 5))
return f'浏览帖子:完成{str(num_ok)}个!'
# 点赞
async def like_posts(self):
"""
点赞帖子
"""
if self.Task_do['bbs_Like_posts']:
# logger.info('Like任务已经完成过了~')
return 'Like任务已经完成过了~'
else:
num_ok = 0
num_cancel = 0
for i in range(self.Task_do['bbs_Like_posts_num']):
async with AsyncClient() as client:
req = await client.post(url=bbs_Likeurl, headers=self.headers,
json={'post_id': self.postsList[i][0], 'is_cancel': False})
data = req.json()
if data['message'] == 'OK':
num_ok += 1
# 判断取消点赞是否打开
if True:
time.sleep(random.randint(2, 8))
async with AsyncClient() as client:
req = await client.post(url=bbs_Likeurl, headers=self.headers,
json={'post_id': self.postsList[i][0], 'is_cancel': True})
data = req.json()
if data['message'] == 'OK':
num_cancel += 1
time.sleep(random.randint(2, 8))
# logger.info('LikeM:完成!like:{}dislike:{}!'.format(str(num_ok), str(num_cancel)))
return 'LikeM:完成!like:{}dislike:{}!'.format(str(num_ok), str(num_cancel))
# 分享操作
return '点赞任务已经完成过了~'
num_ok = 0
num_cancel = 0
for i in range(self.Task_do['bbs_Like_posts_num']):
req = await aiorequests.post(url=bbs_Likeurl,
headers=self.headers,
json={
'post_id': self.postsList[i][0],
'is_cancel': False,
})
data = req.json()
if data['message'] == 'OK':
num_ok += 1
# 取消点赞
await asyncio.sleep(random.randint(1, 3))
req = await aiorequests.post(url=bbs_Likeurl,
headers=self.headers,
json={
'post_id': self.postsList[i][0],
'is_cancel': True,
})
data = req.json()
if data['message'] == 'OK':
num_cancel += 1
await asyncio.sleep(random.randint(2, 5))
return f'点赞帖子:完成{str(num_ok)}个并{str(num_cancel)}个!'
async def share_post(self):
"""
分享帖子
"""
if self.Task_do['bbs_Share']:
# logger.info('分享任务已经完成过了~')
return '分享任务已经完成过了~'
else:
for _ in range(3):
async with AsyncClient() as client:
req = await client.get(url=bbs_Shareurl.format(self.postsList[0][0]), headers=self.headers)
data = req.json()
if data['message'] == 'OK':
return 'ShareM:完成!'
else:
time.sleep(random.randint(2, 8))
time.sleep(random.randint(2, 8))
# add function
import hashlib
def md5(text):
md5_func = hashlib.md5()
md5_func.update(text.encode())
return md5_func.hexdigest()
def old_version_get_ds_token(mysbbs=False):
if mysbbs:
n = 'dWCcD2FsOUXEstC5f9xubswZxEeoBOTc'
else:
n = 'h8w582wxwgqvahcdkpvdhbh2w9casgfl'
i = str(int(time.time()))
r = ''.join(random.sample(string.ascii_lowercase + string.digits, 6))
c = md5('salt=' + n + '&t=' + i + '&r=' + r)
return i + ',' + r + ',' + c
def random_hex(length):
result = hex(random.randint(0, 16 ** length)).replace('0x', '').upper()
if len(result) < length:
result = '0' * (length - len(result)) + result
return result
for _ in range(3):
req = await aiorequests.get(
url=bbs_Shareurl.format(self.postsList[0][0]),
headers=self.headers)
data = req.json()
if data['message'] == 'OK':
return '分享帖子:完成!'
else:
await asyncio.sleep(random.randint(3, 6))
await asyncio.sleep(random.randint(2, 5))

View File

@ -116,6 +116,15 @@ def random_hex(length):
return result
def random_text(length: int) -> str:
"""
生成指定长度的随机字符串
:param length: 长度
:return: 随机字符串
"""
return ''.join(random.sample(string.ascii_lowercase + string.digits, length))
# 米游社headers的ds_token对应版本2.11.1
def get_ds(q="", b=None) -> str:
if b:
@ -144,12 +153,17 @@ def get_headers(cookie, q='', b=None):
return headers
def get_old_version_ds() -> str:
# s = 'h8w582wxwgqvahcdkpvdhbh2w9casgfl'
s = 'dWCcD2FsOUXEstC5f9xubswZxEeoBOTc'
def get_old_version_ds(mhy_bbs: bool = False) -> str:
"""
生成米游社旧版本headers的ds_token
"""
if mhy_bbs:
s = 'dWCcD2FsOUXEstC5f9xubswZxEeoBOTc'
else:
s = 'h8w582wxwgqvahcdkpvdhbh2w9casgfl'
t = str(int(time()))
r = ''.join(random.sample(string.ascii_lowercase + string.digits, 6))
c = md5("salt=" + s + "&t=" + t + "&r=" + r)
c = md5(f"salt={s}&t={t}&r={r}")
return f"{t},{r},{c}"