3
0
Telegram_PaimonBot/defs/weapons.py
Xtao_dada 5eeea30fdc
v0.1.0beta (#1)
*  支持生成原神黄历
*  支持每日副本查询
*  支持角色资料、命座查询
*  支持武器查询
2021-07-09 23:31:30 +08:00

71 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import difflib, json, requests
from os import getcwd, sep
from bs4 import BeautifulSoup
from xpinyin import Pinyin
from json.decoder import JSONDecodeError
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/89.0.4389.82 Safari/537.36"}
working_dir = getcwd()
with open(f"{working_dir}{sep}assets{sep}data{sep}weapon.json", 'r', encoding='utf-8') as f:
weapon_all = json.load(f)
weapon_type = {1: "单手剑", 2: "双手剑", 3: "", 4: "法器", 5: "长枪"}
def get_url(name: str):
re = requests.get(url=f'https://genshin.minigg.cn/?data={name}', headers=headers)
soup = BeautifulSoup(re.text, "lxml").body
user_dict = json.loads(soup.text)
return user_dict['icon']
async def get_weapon(name: str):
name = str(name)
for i in weapon_all:
if name in i['name']:
try:
url = get_url(i['name'][0])
except JSONDecodeError:
url = None
text = f"<b>{i['name'][0]}</b> {'' * i['star']}\n" \
f"<b>类型:</b>{weapon_type[i['type']]}\n" \
f"<b>1级基础攻击力</b>{i['basic_attack']}\n" \
f"<b>满级基础攻击力:</b>{i['max_attack']}\n" \
f"<b>满级副属性:</b>{i['max_attribute']}\n" \
f"<b>技能:</b>{i['skill']}"
return text, url
correct_result = auto_correct(name)
if len(correct_result) > 1:
return f"派蒙这里没找到武器 <code>{name}</code> ,你是要搜索如下的武器吗?\n{montage_result(correct_result)}", None
elif len(correct_result) < 1:
return "没有找到该武器,派蒙也米有办法!是不是名字错了?", None
else:
return f"派蒙这里没找到武器 <code>{name}</code> ,你是要搜索 <code>{correct_result[0]}</code> 吗?", None
def auto_correct(name: str) -> list:
with open(f"{working_dir}{sep}assets{sep}data{sep}weapon_index.json", "r", encoding="utf-8") as weapon_index_file:
character_index = json.loads(weapon_index_file.read())
input_pin_yin_list = Pinyin().get_pinyin(name).split("-")
result_cache = []
result = []
for index_name in character_index:
true_name = list(index_name.keys())[0]
for input_pin_yin in input_pin_yin_list:
for true_pin_yin in index_name[true_name]:
if difflib.SequenceMatcher(None, true_pin_yin, input_pin_yin).quick_ratio() >= 1:
result_cache.append(true_name)
if difflib.SequenceMatcher(None, true_name, name).quick_ratio() >= 0.8:
result_cache.append(true_name)
for result_repeat in result_cache:
if result_cache.count(result_repeat) > 1 and result_repeat not in result:
result.append(result_repeat)
return result
def montage_result(correct_result: list) -> str:
cause = correct_result[0]
for i in range(1, len(correct_result)):
cause = cause + "\n" + correct_result[i]
return cause