import difflib, json from os import getcwd, sep from xpinyin import Pinyin from json.decoder import JSONDecodeError from ci import client 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: "长枪"} async def get_url(name: str): res = await client.get(url=f'https://info.minigg.cn/weapons?query={name}', headers=headers) if "errcode" in res.text: raise JSONDecodeError("", "", 0) py_dict = res.json() return py_dict async def get_weapon(name: str): for i in weapon_all: if name in i['name']: try: url = (await get_url(i['name'][0]))["images"]["icon"] except (JSONDecodeError, KeyError): url = None text = f"{i['name'][0]} {'★' * i['star']}\n" \ f"类型:{weapon_type[i['type']]}\n" \ f"1级基础攻击力:{i['basic_attack']}\n" \ f"满级基础攻击力:{i['max_attack']}\n" \ f"满级副属性:{i['max_attribute']}\n" \ f"技能:{i['skill']}" return text, url try: data = await get_url(str(name)) text = f"{data['name']} {'★' * int(data['rarity'])}\n" \ f"类型:{data['weapontype']}\n" \ f"1级基础攻击力:{data['baseatk']}\n" \ f"副属性:{data['substat']}\n" \ f"描述:{data['description']}" try: url = data["images"]["image"] except KeyError: url = None return text, url except (JSONDecodeError, KeyError): pass correct_result = auto_correct(name) if len(correct_result) > 1: return f"派蒙这里没找到武器 {name} ,你是要搜索如下的武器吗?\n{montage_result(correct_result)}", None elif len(correct_result) < 1: return "没有找到该武器,派蒙也米有办法!是不是名字错了?", None else: return f"派蒙这里没找到武器 {name} ,你是要搜索 {correct_result[0]} 吗?", 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