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

151 lines
5.5 KiB
Python
Raw 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, re, bs4, requests
from os import getcwd, sep
from xpinyin import Pinyin
from json.decoder import JSONDecodeError
working_dir = getcwd()
def nic2name(name):
with open(f"{working_dir}{sep}assets{sep}data{sep}nickname.json", 'r', encoding='utf-8') as f:
all_name = json.load(f)
f.close()
for i in all_name:
for x in i.values():
if name in x:
return x[0]
return name
def get_json(name: str) -> dict:
name = nic2name(name)
res = requests.get(f'https://genshin.minigg.cn/?data={name}')
soup = bs4.BeautifulSoup(res.text, "lxml").body
character_json = json.loads(soup.text)
return character_json
def num_to_char(num):
"""数字转中文."""
num = str(num)
num_dict = {"0": u"", "1": u"", "2": u"", "3": u"", "4": u"", "5": u"", "6": u"", "7": u"", "8": u"",
"9": u""}
list_num = list(num)
shu = []
for i in list_num:
shu.append(num_dict[i])
new_str = "".join(shu)
return new_str
def char_to_char(char):
char_dict = {"": u"0", "": u"1", "": u"2", "": u"3", "": u"4", "": u"5", "": u"6", "": u"7", "": u"8",
"": u"9"}
list_num = list(char)
shu = []
for i in list_num:
shu.append(char_dict[i])
new_str = "".join(shu)
return int(new_str)
def get_character(name: str):
# 角色常见昵称转换为官方角色名
nick_name = nic2name(name)
try:
data0 = get_json(nick_name)
data = data0['角色信息']
if nick_name == '旅行者':
data["简介"] = ''
except JSONDecodeError:
correct_result = auto_correct(nick_name)
if correct_result is None:
return f"派蒙这里没找到 <code>{name}</code> ,可能是派蒙的错,可能是你输入的名字不正确哦。", None
else:
if len(correct_result) > 1:
return f"派蒙这里没找到 <code>{name}</code> ,你是要搜索如下的角色吗?\n{montage_result(correct_result)}", None
elif len(correct_result) < 1:
return f"派蒙这里没找到 <code>{name}</code> ,可能是派蒙的错,可能是你输入的名字不正确哦。", None
else:
return f"派蒙这里没找到 <code>{name}</code> ,你是要搜索 <code>{correct_result[0]}</code> 吗", None
result = f"<b>{nick_name}</b>\n" \
f"<b>命之座:</b>{data['命之座']}\n" \
f"<b>所属:</b>{data['所属']}\n" \
f"<b>武器类型:</b>{data['武器类型']}\n" \
f"<b>生日:</b>{data['生日']}\n"
try:
result += f"<b>神之眼:</b>{data['神之眼']}\n"
except KeyError:
result += f"<b>神之心:</b>{data['神之心']}\n"
result += f"<b>称号:</b>{data['称号']}\n" \
f"<b>简介:</b>{data['简介']}"
url = data0['avatar'].split('?')[0]
return result, url
async def get_mz(name_mz: str) -> str:
name_mz = name_mz.replace(" ", "")
try:
name = re.findall(r'(.*)([零一二三四五六七八九0123456789]{1})命', name_mz)[0][0]
name = nic2name(name)
except IndexError:
name = name_mz
name = nic2name(name)
try:
num = int(re.search(r'\d{1,5}', name_mz).group(0))
except AttributeError:
try:
num = char_to_char(re.findall(r'(.*)([零一二三四五六七八九0123456789]{1})命', name_mz)[0][1])
except IndexError:
num = -1
try:
data0 = get_json(name)
data = data0['命之座']
except:
return f"派蒙这没有 <code>{name}</code> ,可能是官方资料没有该资料,可能是你输入的名字不正确哦。"
result = ''
if num == -1:
n = 1
for key, value in data.items():
result = result + num_to_char(n) + '' + key + ':' + str(value['introduction']) + '\n'
n = n + 1
return f'{name}' + '\n' + result
elif 0 < num < 7:
n = 1
for key, value in data.items():
result = num_to_char(num) + '' + key + ':' + str(value['introduction'])
if n == num:
return f'{name}' + result
n = n + 1
elif num == 0:
return "你搁这原地tp呢"
else:
return f"查询错误!你家 <code>{name}</code> 有 <code>{num}</code> 命??"
def auto_correct(name: str) -> list:
with open(f"{working_dir}{sep}assets{sep}data{sep}character_index.json", "r", encoding="utf-8") as f:
character_index = json.loads(f.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.3:
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