新增抽卡记录导入导出到小助手 (#515)

*  新增抽卡记录导入导出到小助手

* fixname

* aiofiles打开文件
This commit is contained in:
wuchangjun233 2023-04-22 00:08:26 +08:00 committed by GitHub
parent 1f48c58f30
commit b3c495da87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 0 deletions

View File

@ -9,11 +9,19 @@ from ..utils.error_reply import UID_HINT
from .get_gachalogs import save_gachalogs
from .draw_gachalogs import draw_gachalogs_img
from .export_and_import import export_gachalogs, import_gachalogs
from .lelaer_tools import (
get_gachaurl,
get_lelaer_gachalog,
export_gachalog_to_lelaer,
)
sv_gacha_log = SV('抽卡记录')
sv_refresh_gacha_log = SV('刷新抽卡记录')
sv_export_gacha_log = SV('导出抽卡记录')
sv_import_gacha_log = SV('导入抽卡记录', area='DIRECT')
sv_import_lelaer_gachalog = SV('从小助手导入抽卡记录')
sv_export_lelaer_gachalog = SV('导出抽卡记录到小助手')
sv_export_gachalogurl = SV('导出抽卡记录链接')
@sv_import_gacha_log.on_file('json')
@ -69,3 +77,36 @@ async def send_export_gacha_info(bot: Bot, ev: Event):
return await bot.send(MessageSegment.file(file_path, file_name))
else:
return await bot.send('导出抽卡记录失败...')
@sv_import_lelaer_gachalog.on_fullmatch(('从小助手导入抽卡记录'))
async def import_lelaer_gachalog(bot: Bot, ev: Event):
await bot.logger.info('开始执行[从小助手导入抽卡记录]')
sqla = get_sqla(ev.bot_id)
uid = await sqla.get_bind_uid(ev.user_id)
if uid is None:
return await bot.send(UID_HINT)
im = await get_lelaer_gachalog(uid)
await bot.send(im)
@sv_export_lelaer_gachalog.on_fullmatch(('导出抽卡记录到小助手'))
async def export_to_lelaer_gachalog(bot: Bot, ev: Event):
await bot.logger.info('开始执行[导出抽卡记录到小助手]')
sqla = get_sqla(ev.bot_id)
uid = await sqla.get_bind_uid(ev.user_id)
if uid is None:
return await bot.send(UID_HINT)
im = await export_gachalog_to_lelaer(uid)
await bot.send(im)
@sv_export_gachalogurl.on_fullmatch(('导出抽卡记录链接'))
async def export_gachalogurl(bot: Bot, ev: Event):
await bot.logger.info('开始执行[导出抽卡记录链接]')
sqla = get_sqla(ev.bot_id)
uid = await sqla.get_bind_uid(ev.user_id)
if uid is None:
return await bot.send(UID_HINT)
im = await get_gachaurl(uid)
await bot.send(im)

View File

@ -19,6 +19,10 @@ INT_TO_TYPE = {
async def import_gachalogs(history_url: str, type: str, uid: str) -> str:
if type == 'url':
history_data: dict = json.loads(get(history_url).text)
elif type == 'json':
history_data: dict = json.loads(history_url)
if history_data.get('code') == 300:
return "[提瓦特小助手]抽卡记录不存在"
else:
data_bytes = base64.b64decode(history_url)
try:

View File

@ -0,0 +1,67 @@
import time
from urllib.parse import quote
import aiofiles
from httpx import post
from gsuid_core.logger import logger
from ..utils.mys_api import mys_api
from ..utils.error_reply import get_error
from ..gsuid_utils.api.mys.request import RECOGNIZE_SERVER
from .export_and_import import export_gachalogs, import_gachalogs
async def get_gachaurl(uid: str):
server_id = RECOGNIZE_SERVER.get(str(uid)[0])
authkey_rawdata = await mys_api.get_authkey_by_cookie(uid)
if isinstance(authkey_rawdata, int):
return get_error(authkey_rawdata)
authkey = authkey_rawdata['authkey']
now = time.time()
url = (
f"https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog?"
f"authkey_ver=1&sign_type=2&auth_appid=webview_gacha&init_type=301&"
f"gacha_id=fecafa7b6560db5f3182222395d88aaa6aaac1bc"
f"&timestamp={str(int(now))}"
f"&lang=zh-cn&device_type=mobile&plat_type=ios&region={server_id}"
f"&authkey={quote(authkey,'utf-8')}"
f"&game_biz=hk4e_cn&gacha_type=301&page=1&size=5&end_id=0"
)
logger.info(url)
return url
async def get_lelaer_gachalog(uid: str):
gachalog_url = await get_gachaurl(uid)
data = {'uid': uid, 'gachaurl': gachalog_url, 'lang': 'zh-Hans'}
history_data = post(
'https://www.lelaer.com/outputGacha.php',
data=data,
verify=False,
timeout=30,
).text
logger.info(history_data)
return await import_gachalogs(history_data, 'json', uid)
async def export_gachalog_to_lelaer(uid: str):
gachalog_url = await get_gachaurl(uid)
export = await export_gachalogs(uid)
if export['retcode'] == 'ok':
file_path = export['url']
else:
return '导出抽卡记录失败...'
async with aiofiles.open(file_path, 'r', encoding='utf-8') as f:
file_data = {'upload': await f.read()}
data = {'gachaurl': gachalog_url, 'importType': 'uigf'}
history_data = post(
'https://www.lelaer.com/uigf.php',
files=file_data,
data=data,
verify=False,
timeout=30,
).status_code
if history_data == 200:
return '[提瓦特小助手]抽卡记录上传成功,请前往小程序查看'
else:
return '[提瓦特小助手]上传失败'