zhenxun_bot/plugins/draw_card/update_game_requests_info.py

157 lines
5.8 KiB
Python
Raw Normal View History

2022-02-19 18:20:19 +08:00
from .config import DRAW_DATA_PATH, draw_config
2021-06-04 18:01:33 +08:00
from asyncio.exceptions import TimeoutError
from .util import download_img
from bs4 import BeautifulSoup
from .util import remove_prohibited_str
2021-11-23 21:44:59 +08:00
from utils.http_utils import AsyncHttpx
2022-02-19 18:20:19 +08:00
from nonebot.log import logger
2021-06-04 18:01:33 +08:00
import asyncio
try:
import ujson as json
except ModuleNotFoundError:
import json
2022-02-19 18:20:19 +08:00
headers = {'User-Agent': '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"'}
2021-06-04 18:01:33 +08:00
async def update_requests_info(game_name: str):
2022-02-19 18:20:19 +08:00
info_path = DRAW_DATA_PATH / f"{game_name}.json"
2021-06-04 18:01:33 +08:00
try:
2022-02-19 18:20:19 +08:00
with info_path.open('r', encoding='utf8') as f:
2021-06-04 18:01:33 +08:00
data = json.load(f)
except (ValueError, FileNotFoundError):
data = {}
try:
2022-02-19 18:20:19 +08:00
if game_name in ['fgo', 'fgo_card']:
if game_name == 'fgo':
url = 'http://fgo.vgtime.com/servant/ajax?card=&wd=&ids=&sort=12777&o=desc&pn='
2021-11-23 21:44:59 +08:00
else:
2022-02-19 18:20:19 +08:00
url = 'http://fgo.vgtime.com/equipment/ajax?wd=&ids=&sort=12958&o=desc&pn='
2021-11-23 21:44:59 +08:00
for i in range(9999):
2022-02-19 18:20:19 +08:00
response = await AsyncHttpx.get(f'{url}{i}', timeout=7)
fgo_data = json.loads(response.text)
if int(fgo_data['nums']) == 0:
2021-11-23 21:44:59 +08:00
break
2022-02-19 18:20:19 +08:00
for x in fgo_data['data']:
x['name'] = remove_prohibited_str(x['name'])
key = x['name']
2021-11-23 21:44:59 +08:00
data = add_to_data(data, x, game_name)
2022-02-19 18:20:19 +08:00
await download_img(data[key]['头像'], game_name, key)
logger.info(f'{key} is update...')
if game_name == 'onmyoji':
url = 'https://yys.res.netease.com/pc/zt/20161108171335/js/app/all_shishen.json?v74='
response = await AsyncHttpx.get(url, timeout=7)
onmyoji_data = response.json()
2021-11-23 21:44:59 +08:00
for x in onmyoji_data:
2022-02-19 18:20:19 +08:00
x['name'] = remove_prohibited_str(x['name'])
key = x['name']
2021-11-23 21:44:59 +08:00
data = add_to_data(data, x, game_name)
2022-02-19 18:20:19 +08:00
logger.info(f'{key} is update...')
2021-11-23 21:44:59 +08:00
data = await _last_check(data, game_name)
2022-02-19 18:20:19 +08:00
except TimeoutError:
logger.warning(f'更新 {game_name} 超时...')
2021-06-04 18:01:33 +08:00
return {}, 999
2021-11-29 21:29:48 +08:00
except Exception as e:
2022-02-19 18:20:19 +08:00
logger.warning(f'更新 {game_name} 失败 {type(e)}{e}...')
return {}, 999
with info_path.open('w', encoding='utf8') as wf:
2021-06-04 18:01:33 +08:00
json.dump(data, wf, ensure_ascii=False, indent=4)
return data, 200
# 添加到字典
def add_to_data(data: dict, x: dict, game_name: str) -> dict:
member_dict = {}
2022-02-19 18:20:19 +08:00
if game_name == 'fgo':
2021-06-04 18:01:33 +08:00
member_dict = {
2022-02-19 18:20:19 +08:00
'id': x['id'],
'card_id': x['charid'],
'头像': x['icon'],
'名称': x['name'],
'职阶': x['classes'],
'星级': x['star'],
'hp': x['lvmax4hp'],
'atk': x['lvmax4atk'],
'card_quick': x['cardquick'],
'card_arts': x['cardarts'],
'card_buster': x['cardbuster'],
'宝具': x['tprop'],
2021-06-04 18:01:33 +08:00
}
2022-02-19 18:20:19 +08:00
if game_name == 'fgo_card':
2021-06-04 18:01:33 +08:00
member_dict = {
2022-02-19 18:20:19 +08:00
'id': x['id'],
'card_id': x['equipid'],
'头像': x['icon'],
'名称': x['name'],
'星级': x['star'],
'hp': x['lvmax_hp'],
'atk': x['lvmax_atk'],
'skill_e': x['skill_e'].split('<br />')[: -1],
2021-06-04 18:01:33 +08:00
}
2022-02-19 18:20:19 +08:00
if game_name == 'onmyoji':
2021-06-04 18:01:33 +08:00
member_dict = {
2022-02-19 18:20:19 +08:00
'id': x['id'],
'名称': x['name'],
'星级': x['level'],
2021-06-04 18:01:33 +08:00
}
2022-02-19 18:20:19 +08:00
data[member_dict['名称']] = member_dict
2021-06-04 18:01:33 +08:00
return data
# 获取额外数据
2021-11-23 21:44:59 +08:00
async def _last_check(data: dict, game_name: str) -> dict:
2022-02-19 18:20:19 +08:00
if game_name == 'fgo':
url = 'http://fgo.vgtime.com/servant/'
2021-06-04 18:01:33 +08:00
tasks = []
2022-02-19 18:20:19 +08:00
semaphore = asyncio.Semaphore(draw_config.SEMAPHORE)
2021-06-04 18:01:33 +08:00
for key in data.keys():
2022-02-19 18:20:19 +08:00
tasks.append(asyncio.ensure_future(
_async_update_fgo_extra_info(url, key, data[key]['id'], semaphore)))
result = await asyncio.gather(*tasks)
for x in result:
2021-06-04 18:01:33 +08:00
for key in x.keys():
2022-02-19 18:20:19 +08:00
data[key]['入手方式'] = x[key]['入手方式']
if game_name == 'onmyoji':
url = 'https://yys.163.com/shishen/{}.html'
2021-06-04 18:01:33 +08:00
for key in data.keys():
2022-02-19 18:20:19 +08:00
response = await AsyncHttpx.get(f'{url.format(data[key]["id"])}', timeout=7)
soup = BeautifulSoup(response.text, 'lxml')
data[key]['头像'] = "https:" + soup.find('div', {'class': 'pic_wrap'}).find('img')['src']
await download_img(data[key]['头像'], game_name, key)
2021-06-04 18:01:33 +08:00
return data
2021-11-23 21:44:59 +08:00
async def _async_update_fgo_extra_info(url: str, key: str, _id: str, semaphore):
2021-06-04 18:01:33 +08:00
# 防止访问超时
2021-06-17 09:43:03 +08:00
async with semaphore:
for i in range(10):
try:
2022-02-19 18:20:19 +08:00
response = await AsyncHttpx.get(f'{url}{_id}', timeout=7)
soup = BeautifulSoup(response.text, 'lxml')
obtain = soup.find('table', {'class': 'uk-table uk-codex-table'}).find_all('td')[-1].text
if obtain.find('限时活动免费获取 活动结束后无法获得') != -1:
obtain = ['活动获取']
elif obtain.find('非限时UP无法获得') != -1:
obtain = ['限时召唤']
2021-11-23 21:44:59 +08:00
else:
2022-02-19 18:20:19 +08:00
if obtain.find('&') != -1:
obtain = obtain.strip().split('&')
2021-06-04 18:01:33 +08:00
else:
2022-02-19 18:20:19 +08:00
obtain = obtain.strip().split(' ')
logger.info(f'Fgo获取额外信息 {key}....{obtain}')
2021-11-23 21:44:59 +08:00
x = {key: {}}
2022-02-19 18:20:19 +08:00
x[key]['入手方式'] = obtain
2021-11-23 21:44:59 +08:00
return x
2022-02-19 18:20:19 +08:00
except TimeoutError:
logger.warning(f'访问{url}{_id}{i}次 超时...已再次访问')
2021-11-29 21:29:48 +08:00
except Exception as e:
2022-02-19 18:20:19 +08:00
logger.warning(f'访问{url}{_id}{i}次 发生错误:{e}...已再次访问')
2021-06-04 18:01:33 +08:00
return {}
2022-02-19 18:20:19 +08:00