forked from benx1n/wows-stats-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwws_shiprank.py
136 lines (130 loc) · 6.01 KB
/
wws_shiprank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import httpx
import traceback
import json
import jinja2
import asyncio
from pathlib import Path
from loguru import logger
from hoshino.typing import MessageSegment
from .data_source import servers,set_shipparams,tiers,number_url_homes,set_ShipRank_Numbers
from .utils import match_keywords,bytes2b64
from .wws_ship import ShipSecletProcess,ShipSlectState
from.publicAPI import get_ship_byName
from bs4 import BeautifulSoup
from .html_render import html_to_pic,text_to_pic
dir_path = Path(__file__).parent
template_path = dir_path / "template"
cfgpath = dir_path / 'config.json'
config = json.load(open(cfgpath, 'r', encoding='utf8'))
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_path), enable_async=True
)
headers = {
'Authorization': config['token'],
'accept':'application/json',
'Content-Type':'application/json',
}
async def get_ShipRank(info,bot,ev):
try:
if len(info) == 2:
param_server,info = await match_keywords(info,servers)
if param_server and not param_server == 'cn':
number_url = number_url_homes[param_server] + "/ship/"
else:
return '请检查服务器是否正确,暂不支持国服'
else:
return '参数似乎出了问题呢'
shipList = await get_ship_byName(str(info[0]))
print(shipList)
if shipList:
if len(shipList) < 2:
select_shipId = shipList[0][0]
number_url += f"{select_shipId},{shipList[0][2]}"
else:
msg = f'存在多条名字相似的船\n请在20秒内选择对应的序号\n=================\n'
flag = 0
for each in shipList:
flag += 1
msg += f"{flag}:{tiers[each[3]-1]} {each[1]}\n"
ShipSecletProcess[ev['user_id']] = ShipSlectState(False, None, shipList)
img = await text_to_pic(text=msg,css_path = template_path/"text-ship.css",width=250)
await bot.send(ev,str(MessageSegment.image(bytes2b64(img))))
a = 0
while a < 40 and not ShipSecletProcess[ev['user_id']].state:
a += 1
await asyncio.sleep(0.5)
if ShipSecletProcess[ev['user_id']].state and ShipSecletProcess[ev['user_id']].SlectIndex <= len(shipList):
select_shipId = int(shipList[ShipSecletProcess[ev['user_id']].SlectIndex-1][0])
number_url += f"{select_shipId},{shipList[ShipSecletProcess[ev['user_id']].SlectIndex-1][2]}"
ShipSecletProcess[ev['user_id']] = ShipSlectState(False, None, None)
print(number_url)
else:
ShipSecletProcess[ev['user_id']] = ShipSlectState(False, None, None)
return '已超时退出'
else:
return '找不到船,请确认船名是否正确,可以使用【wws 查船名】查询船只中英文'
content = await search_ShipRank_Yuyuko(select_shipId,param_server)
if content: #存在缓存,直接出图
return await html_to_pic(content, wait=0, viewport={"width": 1300, "height": 100})
else: #无缓存,去Number爬
content,numbers_data = await search_ShipRank_Numbers(number_url,param_server,select_shipId)
if content:
await post_ShipRank(numbers_data) #上报Yuyuko
return await html_to_pic(content, wait=0, viewport={"width": 1300, "height": 100})
else:
return 'wuwuu好像出了点问题,可能是网络问题,过一会儿还是不行的话请联系麻麻~'
except Exception:
logger.error(traceback.format_exc())
ShipSecletProcess[ev['user_id']] = ShipSlectState(False, None, None)
return 'wuwuu好像出了点问题,过一会儿还是不行的话请联系麻麻~'
async def search_ShipRank_Yuyuko(shipId,server):
try:
content = None
async with httpx.AsyncClient(headers=headers) as client: #查询是否有缓存
url = 'https://api.wows.shinoaki.com/upload/numbers/data/v2/upload/ship/rank'
params = {
"server":server,
"shipId":int(shipId)
}
print(f"{url}\n{params}")
resp = await client.get(url, params=params,timeout=None)
result = resp.json()
if result['code'] == 200 and result['data']:
result_data = {"data":result['data']}
template = env.get_template("ship-rank.html")
content = await template.render_async(result_data)
return content
else:
return None
except Exception:
logger.error(traceback.format_exc())
return None
async def search_ShipRank_Numbers(url,server,shipId):
try:
content = None
print(url)
async with httpx.AsyncClient() as client:
resp = await client.get(url, timeout=None)
soup = BeautifulSoup(resp.content, 'html.parser')
data = soup.select('tr[class="cells-middle"]')
infoList = await set_ShipRank_Numbers(data,server,shipId)
if infoList:
result_data = {"data":infoList}
template = env.get_template("ship-rank.html")
content = await template.render_async(result_data)
return content,infoList
else:
return None,None
except Exception:
logger.error(traceback.format_exc())
return None,None
async def post_ShipRank(data):
try:
async with httpx.AsyncClient(headers=headers) as client:
url = 'https://api.wows.shinoaki.com/upload/numbers/data/v2/upload/ship/rank'
resp = await client.post(url, json = data, timeout=None)
print(resp.request)
result = resp.json()
print(result)
except Exception:
logger.error(traceback.format_exc())