-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsbot_refac.py
executable file
·521 lines (416 loc) · 18.3 KB
/
hsbot_refac.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import asyncio
import os
# import PIL.Image as Image
from urllib.request import urlretrieve
import requests
import json
from bs4 import BeautifulSoup
import cloudscraper
import os
import random
import collections
from amiyabot import AmiyaBot, Message, Chain
import aiohttp
# init the hs bot
# read the appid and token from local bot.json
f = open('bot.json', 'r')
bot_content = json.loads(f.read())
appid, token = bot_content["appid"], bot_content["token"]
bot = AmiyaBot(appid=appid, token=token)
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'}
region_dict = {
'US': '美',
'EU': '欧',
'AP': '亚'
}
leaderboardId_dict = {
'WLD': '狂野',
'STD': '标准',
'arena': '竞技场'
}
leaderboard_dict = {
'wild': '狂野',
'standard': '标准'
}
async def reqRank(data: Message, region: str, leaderboardId: str, keyword: str):
target = data.text[data.text.find(keyword):]
id = target[5:]
html = f"https://www.hsguru.com/leaderboard?leaderboardId={leaderboardId}®ion={region}&search=" + id
response = requests.get(html, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.content, 'html.parser')
body = soup.body
ht = body.text.split()
if ht[0] == "Bad":
return Chain(data).text(f'hsguru挂了,暂时查不了,有其他问题请反馈Sola')
if leaderboardId == "STD":
start_index = ht.index("Tour", 0, len(ht))
else:
start_index = ht.index("Battletag", 0, len(ht))
res_ht = ht[start_index+1:]
res_text = ""
for item in res_ht:
if not str(item).isalnum():
res_ht.remove(item)
if "history" in item:
res_ht.remove(item)
for i in range(0, len(res_ht)-1, 2):
pair = "rank: " + res_ht[i] + ", " + "name: " + res_ht[i+1] + "\n"
res_text += pair
return Chain(data).text(f'{region_dict[region]}服{leaderboardId_dict[leaderboardId]}, \n{res_text}')
@bot.on_message(keywords='查美服狂野')
async def reqUSRankWLD(data: Message):
return await reqRank(data, 'US', 'WLD', "查美服狂野")
@bot.on_message(keywords='查美服标准')
async def reqUSRankSTD(data: Message):
return await reqRank(data, 'US', 'STD', "查美服标准")
@bot.on_message(keywords='查美服幻变')
async def reqUSRankSTD(data: Message):
return await reqRank(data, 'US', 'twist', "查美服幻变")
@bot.on_message(keywords='查欧服狂野')
async def reqEURankWLD(data: Message):
return await reqRank(data, 'EU', 'WLD', "查欧服狂野")
@bot.on_message(keywords='查欧服标准')
async def reqEURankSTD(data: Message):
return await reqRank(data, 'EU', 'STD', "查欧服标准")
@bot.on_message(keywords='查亚服狂野')
async def reqAPRankWLD(data: Message):
return await reqRank(data, 'AP', 'WLD', "查亚服狂野")
@bot.on_message(keywords='查亚服标准')
async def reqAPRankSTD(data: Message):
return await reqRank(data, 'AP', 'STD', "查亚服标准")
async def fetch_page(session, url):
try:
async with session.get(url) as response:
info = await response.text()
json_dict = json.loads(info)
rank_name = json_dict["leaderboard"]["rows"]
page_results = []
for item in rank_name:
rank, name = item['rank'], item['accountid']
page_results.append((rank, name))
return page_results
except (aiohttp.ClientError, json.JSONDecodeError, KeyError):
try:
scraper = cloudscraper.create_scraper(browser={
'browser': 'firefox',
'platform': 'windows',
'mobile': True
})
info = scraper.get(url)
json_dict = json.loads(info.text)
rank_name = json_dict["leaderboard"]["rows"]
page_results = []
for item in rank_name:
rank, name = item['rank'], item['accountid']
page_results.append((rank, name))
return page_results
except Exception:
return None
async def reqRankLev(data: Message, region: str, leaderboardId: str):
all_results = []
async with aiohttp.ClientSession() as session:
tasks = []
for i in range(1, 5):
html = f"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region={region}&leaderboardId={leaderboardId}&page={i}"
tasks.append(fetch_page(session, html))
results = await asyncio.gather(*tasks)
for page_results in results:
if page_results is None:
return Chain(data).text('获取数据时出现错误,网络波动将在下个版本修复,有问题反馈Sola')
all_results.extend(page_results)
# Sort all results by rank
all_results.sort(key=lambda x: x[0])
# Convert sorted results to string
res = "\n".join(f"{rank}: {name}" for rank, name in all_results)
return Chain(data).text(f'{region_dict[region]}服{leaderboard_dict[leaderboardId]}月榜, \n{res}')
@bot.on_message(keywords='打印亚服狂野月榜')
async def reqAPRankeleven(data: Message):
return await reqRankLev(data, 'AP', 'wild')
@bot.on_message(keywords='打印欧服狂野月榜')
async def reqEURankeleven(data: Message):
return await reqRankLev(data, 'EU', 'wild')
@bot.on_message(keywords='打印美服狂野月榜')
async def reqUSRankeleven(data: Message):
return await reqRankLev(data, 'US', 'wild')
@bot.on_message(keywords='打印美服标准月榜')
async def reqUSRankeleven(data: Message):
return await reqRankLev(data, 'US', 'standard')
@bot.on_message(keywords='打印欧服标准月榜')
async def reqUSRankeleven(data: Message):
return await reqRankLev(data, 'EU', 'standard')
@bot.on_message(keywords='打印亚服标准月榜')
async def reqUSRankeleven(data: Message):
return await reqRankLev(data, 'AP', 'standard')
async def fetch_num(session, url):
try:
async with session.get(url) as response:
info = await response.text()
json_dict = json.loads(info)
return json_dict["leaderboard"]["pagination"]["totalSize"]
except (aiohttp.ClientError, json.JSONDecodeError, KeyError):
try:
scraper = cloudscraper.create_scraper(browser={
'browser': 'firefox',
'platform': 'windows',
'mobile': True
})
info = scraper.get(url)
json_dict = json.loads(info.text)
return json_dict["leaderboard"]["pagination"]["totalSize"]
except Exception:
return -1
@bot.on_message(keywords='查三服狂野人数')
async def reqNumLeaderBoard(data: Message):
urls = [
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=US&leaderboardId=wild&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=EU&leaderboardId=wild&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=AP&leaderboardId=wild&page=1"
]
async with aiohttp.ClientSession() as session:
tasks = [fetch_num(session, url) for url in urls]
USWILD, EUWILD, APWILD = await asyncio.gather(*tasks)
if -1 in (USWILD, EUWILD, APWILD):
return Chain(data).text('获取数据时出现错误,网络波动将在下个版本修复,有问题反馈Sola')
else:
res_text = "美服:" + str(USWILD) + "\n欧服:" + str(EUWILD) + "\n亚服:" + str(APWILD)
return Chain(data).text(f'三服狂野人数, \n{res_text}')
@bot.on_message(keywords='查三服标准人数')
async def reqNumLeaderBoard(data: Message):
urls = [
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=US&leaderboardId=standard&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=EU&leaderboardId=standard&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=AP&leaderboardId=standard&page=1"
]
async with aiohttp.ClientSession() as session:
tasks = [fetch_num(session, url) for url in urls]
USWILD, EUWILD, APWILD = await asyncio.gather(*tasks)
if -1 in (USWILD, EUWILD, APWILD):
return Chain(data).text('获取数据时出现错误,网络波动将在下个版本修复,有问题反馈Sola')
else:
res_text = "美服:" + str(USWILD) + "\n欧服:" + str(EUWILD) + "\n亚服:" + str(APWILD)
return Chain(data).text(f'三服标准人数, \n{res_text}')
async def reqRankArena(data: Message, region: str, leaderboardId: str, keyword: str):
target = data.text[data.text.find(keyword):]
id = target[len(keyword):]
html = f"https://www.hsguru.com/leaderboard?leaderboardId={leaderboardId}®ion={region}&search=" + id
response = requests.get(html, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.content, 'html.parser')
body = soup.body
ht = body.text.split()
start_index = ht.index("Rating", 0, len(ht))
res_ht = ht[start_index+1:]
res_text = ""
for item in res_ht:
if not str(item).isalnum():
res_ht.remove(item)
for i in range(0, len(res_ht)-1, 2):
pair = "rank: " + res_ht[i] + ", " + "name: " + res_ht[i+1] + "\n"
res_text += pair
return Chain(data).text(f'{region_dict[region]}服{leaderboardId_dict[leaderboardId]}, \n{res_text}')
@bot.on_message(keywords='查美服竞技场')
async def reqUSRankarena(data: Message):
return await reqRankArena(data, 'US', 'arena', "查美服竞技场")
@bot.on_message(keywords='查欧服竞技场')
async def reqEURankarena(data: Message):
return await reqRankArena(data, 'EU', 'arena', "查欧服竞技场")
@bot.on_message(keywords='查亚服竞技场')
async def reqAPRankarena(data: Message):
return await reqRankArena(data, 'AP', 'arena', "查亚服竞技场")
@bot.on_message(keywords='猫猫虫')
async def reqNumLeaderBoard(data: Message):
# 查询本地图片,随机选择一个
local_mmc = "./capoo_origin_gif"
mmc_ = [os.path.join(local_mmc, f) for f in os.listdir(local_mmc)]
random_mmc = random.sample(mmc_, 1)
return Chain(data).image(random_mmc)
# @bot.on_message(keywords='代码')
# async def reqCode(data: Message):
target = data.text_origin[data.text_origin.find("代码"):]
deck_code = target[2:].strip()
# deck_code = "AAEBAZICBrS7AsX9AomLBM+sBP3EBZ/zBQzpAYoOoM0CntICv/ICj/YCr4AErsAEhO8E2/oF/Y0GqZ4GAAED6awC/cQFtLsC/cQFuNkE/cQFAAA="
url = "https://api.blizzard.com/hearthstone/deck?code=" + deck_code + "&locale=zh_CN"
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
'Authorization': 'Bearer KRWpkSRImus70XdDJxtFmU78r8l2hXSIpR'
}
response = requests.request("GET", url, headers=headers, data={})
json_dict = json.loads(response.text)
deck_format = json_dict["format"]
deck_class = json_dict["class"]["name"]
card_list = json_dict["cards"]
if "sideboardCards" in json_dict :
ntr = json_dict["sideboardCards"][0]["cardsInSideboard"]
mana_name = collections.defaultdict(list)
name_pic = collections.defaultdict(list)
mana_name_ntr = collections.defaultdict(list)
name_pic_ntr = collections.defaultdict(list)
for card in card_list:
card_mana = card["manaCost"]
card_name = card["name"]
card_img = card["image"]
mana_name[card_mana].append(card_name)
name_pic[card_name].append(card_img)
if "sideboardCards" in json_dict :
for card in ntr:
card_mana = card["manaCost"]
card_name = card["name"]
card_img = card["image"]
mana_name_ntr[card_mana].append(card_name)
name_pic_ntr[card_name].append(card_img)
mana_name = sorted(mana_name.items(), key=lambda d: d[0])
mana_name_ntr = sorted(mana_name_ntr.items(), key=lambda d: d[0])
# card without pic, only name, support ntr
# card with pic
# save the card pic in ./card_source
card_list = os.listdir("./card_source")
for name, url in name_pic.items():
curname = name + ".png"
if curname not in card_list:
location = "./card_source/" + curname
urlretrieve(url[0], location)
# generate image
row, col = 7, 7
imgh, imgw = 404, 558
res_img = Image.new('RGB', (col * imgh, row * imgw))
location = []
for mana, namelist in mana_name:
for name in namelist:
location.append("./card_source/" + name + ".png")
# res_img.paste(Image.open(location))
cnt = 0
for y in range(1, row + 1):
for x in range(1, col + 1):
if cnt >= len(location):
break
# print(location[cnt])
res_img.paste(Image.open(location[cnt]), ((x - 1) * imgh, (y - 1) * imgw))
cnt += 1
else:
continue
def transparent_back(img):
img = img.convert('RGBA')
L, H = img.size
color_0 = img.getpixel((0,0))
for h in range(H):
for l in range(L):
dot = (l,h)
color_1 = img.getpixel(dot)
if color_1 == color_0:
color_1 = color_1[:-1] + (0,)
img.putpixel(dot,color_1)
return img
# ntr
if "sideboardCards" in json_dict :
for name, url in name_pic_ntr.items():
curname = name + ".png"
if curname not in card_list:
location = "./card_source/" + curname
urlretrieve(url[0], location)
location = []
for mana, namelist in mana_name_ntr:
for name in namelist:
location.append("./card_source/" + name + ".png")
cnt = 0
# last row
y = 7
for x in range(1, col + 1):
if cnt >= len(location):
break
res_img.paste(Image.open(location[cnt]), ((x - 1) * imgh, (y - 1) * imgw))
cnt += 1
res_img = transparent_back(res_img)
res_img.save("./deck_gen/2.png")
os.system('pngquant /home/hsbot/deck_gen/2.png --force --ext res.png')
return Chain(data).image("./deck_gen/2res.png")
@bot.on_message(keywords='查主播')
async def bli(data: Message):
res_tag = {"0": "未开播", "1": "直播中", "2": "轮播中"}
result = ""
# 天捷
result += liveReq(res_tag, 30033896, "天捷")
# tsu
result += liveReq(res_tag, 261955, "tsu")
# soda
result += liveReq(res_tag, 14138500, "soda")
# fox11
result += liveReq(res_tag, 3817924, "fox11")
# mimei
result += liveReq(res_tag, 30072416, "米霉")
# changsha
result += liveReq(res_tag, 539119, "长沙")
# 甜宝
result += liveReq(res_tag, 24017611, "甜宝")
# 小指
result += liveReq(res_tag, 24405493, "小指")
# 神抽狗
result += liveReq(res_tag, 22637234, "神抽狗")
# lingmeng
result += liveReq(res_tag, 26919877, "lingmeng")
# nanami
result += liveReq(res_tag, 26101101, "nanami")
# taffy
result += liveReq(res_tag, 22603245, "taffy")
# azusa
result += liveReq(res_tag, 80397, "阿梓")
# 東雪蓮
result += liveReq(res_tag, 22816111, "東雪蓮")
# 010
result += liveReq(res_tag, 21452505, "010")
# 眞白かのん
result += liveReq(res_tag, 21402309, "眞白かのん")
# 星瞳
result += liveReq(res_tag, 22886883, "星瞳")
# 雫るる
result += liveReq(res_tag, 21013446, "雫るる")
# 猫雷
result += liveReq(res_tag, 22676119, "猫雷")
return Chain(data).text(result)
def liveReq(res_tag, id, name):
url = "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + str(id)
response = requests.request("GET", url, headers=headers, data={})
json_dict = json.loads(response.text)
result = name + ": " + res_tag[str(json_dict["data"]["live_status"])] + "\n"
if json_dict["data"]["live_status"] == 1:
result += "房间号:" + str(id) + "\n"
return result
# 新增审核
@bot.on_message(keywords='传说人数')
async def reqNumLeaderBoard(data: Message):
urls = [
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=US&leaderboardId=wild&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=EU&leaderboardId=wild&page=1",
"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=AP&leaderboardId=wild&page=1"
]
async with aiohttp.ClientSession() as session:
tasks = [fetch_num(session, url) for url in urls]
USWILD, EUWILD, APWILD = await asyncio.gather(*tasks)
if -1 in (USWILD, EUWILD, APWILD):
return Chain(data).text('获取数据时出现错误,网络波动将在下个版本修复,有问题反馈Sola')
else:
res_text = "快捷查询只能查狂野,标准需要使用教程中的命令。" + "\n"
res_text += "美服:" + str(USWILD) + "\n欧服:" + str(EUWILD) + "\n亚服:" + str(APWILD)
return Chain(data).text(f'三服狂野人数, \n{res_text}')
@bot.on_message(keywords='月榜查询')
async def reqRankEleven(data: Message):
all_results = []
async with aiohttp.ClientSession() as session:
tasks = []
for i in range(1, 5):
html = f"https://hearthstone.blizzard.com/en-us/api/community/leaderboardsData?region=US&leaderboardId=wild&page={i}"
tasks.append(fetch_page(session, html))
results = await asyncio.gather(*tasks)
for page_results in results:
if page_results is None:
return Chain(data).text('获取数据时出现错误')
all_results.extend(page_results)
# Sort all results by rank
all_results.sort(key=lambda x: x[0])
# Convert sorted results to string
res = "\n".join(f"{rank}: {name}" for rank, name in all_results)
return Chain(data).text(f'快捷查询只能查狂野,其他服务器需要使用教程中的命令, \n{res}')
def start_bot():
asyncio.run(bot.start())
if __name__ == "__main__":
start_bot()