diff options
author | Kyomotoi <[email protected]> | 2020-06-06 17:37:17 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2020-06-06 17:37:17 +0800 |
commit | b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5 (patch) | |
tree | f41ee7f886cb6aa979fd6de7d29d0ea5521e2df3 /AyaBot/plugins/covid19.py | |
parent | 69e577fa093fdb8fdf3bc8f23b35951d8abb3b2c (diff) | |
download | ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.tar.gz ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.tar.bz2 ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.zip |
some changes for weather
Diffstat (limited to 'AyaBot/plugins/covid19.py')
-rw-r--r-- | AyaBot/plugins/covid19.py | 120 |
1 files changed, 27 insertions, 93 deletions
diff --git a/AyaBot/plugins/covid19.py b/AyaBot/plugins/covid19.py index 1dbed70..ea9a341 100644 --- a/AyaBot/plugins/covid19.py +++ b/AyaBot/plugins/covid19.py @@ -1,10 +1,13 @@ import re import json +import sys +sys.path.append('D:\code\Aya\AyaBot\plugins\Module') import demjson import requests from pprint import pformat, pprint from urllib.parse import urlencode from nonebot import on_command, CommandSession +import google_translate # 国外版 数据更新非常及时 @@ -27,104 +30,23 @@ LIST = """截至: {lastChecked} 最后检查时间: {lastReported}""" -def int_overflow(val): - maxint = 2147483647 - if not -maxint-1 <= val <= maxint: - val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1 - return val - -def ansii(a): - return a.encode('gbk') - - -def kr(a:int, b): - c = 0 - b = ansii(b) - while c < len(b)-2: - d = b[c + 2] - d = d - 87 if ansii("a")[0] <= d else int(chr(d)) - d = a >> d if ansii("+")[0] == b[c + 1] else a << d - d = int_overflow(d) - a = a + d & 4294967295 if ansii("+")[0] == b[c] else a ^ d - c += 3 - return int_overflow(a) - - -def mr(q, TKK): - e = q.encode() - d = str(TKK).split('.') - a = int(d[0]) - b = int(d[0]) - - for f in e: - a += f - a = kr(a, "+-a^+6") - a = kr(a, "+-3^+b+-f") - a &= 0xffffffff # 出错了,转回无符号 - a ^= (int(d[1]) or 0) - if 0 > a: - a = (a & 2147483647) + 2147483648 - a %= 1E6 - a = int(a) - - # c = '&tk=' - # return c + (str(a) + "." + str(a ^ b)) - return (str(a) + "." + str(a ^ b)) - - -def translate(q='hello', source='en', to='zh-CN', tkk=None): - """ - 限制最大5000,按utf-8算,一个汉字算1个,1个英文算一个,超过会失败 - """ - if not tkk: - tkk = '426151.3141811846' - tk = mr(q, tkk) - params = { - 'client': 't', - 'sl': source, - 'tl': to, - 'hl': source, - 'dt': [ - 'at', 'qca', 'rw', 'rm', 'ss', 't' - ], - 'tk': tk, - 'ie': 'UTF-8', - 'oe': 'UTF-8', - 'pc': 1, - 'kc': 1, - 'ssel': 0, - 'otf': 1 - } - data = { - 'q': q - } - headers = { - 'Referer': 'https://translate.google.cn/', - 'Host': 'translate.google.cn', - } - resp = requests.post('https://translate.google.cn/translate_a/single', params=params, data=data, headers=headers) - if resp.status_code == 200: - resp.encoding = 'utf-8' - data = resp.json() - - result = [] - result.append(''.join(map(lambda x:x[0], data[0][:-1]))) - result.append(data[0][-1][-1]) - return result - else: - return None - - @on_command('covid19', aliases=['疫情', '疫情查询', '疫情情况'], only_to_me=False) async def covid19(session: CommandSession): - country = session.get('country', prompt='请用Eng键入需要查询的国家(例:China)') - re_msg = translate(country[:4999], to='en', source='zh-CN') + country = session.get('country', prompt='请键入需要查询的国家(例:中国)') + if country == '美国': + pass + else: + re_msg = google_translate.translate(country[:4999], to='en', source='zh-CN') # if re_msg[0]!='': # await session.send(re_msg[0]) - await session.send('开始搜寻...\n如返回国名为Global的则为提供的国家名有问题,必须为英文全称') + await session.send('开始搜寻...\n如返回Global则为国家名有问题') try: querystring = {"country":"cy"} - querystring["country"] = re_msg[0] + if country == '美国': + re_msg = 'US' + querystring["country"] = re_msg + else: + querystring["country"] = re_msg[0] print(querystring) headers = { 'x-rapidapi-host': "covid-19-coronavirus-statistics.p.rapidapi.com", @@ -145,4 +67,16 @@ async def covid19(session: CommandSession): ) ) except: - await session.send('搜索出问题了呢,重新试试?')
\ No newline at end of file + await session.send('搜索出问题了呢,重新试试?') + [email protected]_parser +async def _(session: CommandSession): + stripped_arg = session.current_arg_text.strip() + if session.is_first_run: + if stripped_arg: + session.state['country'] = stripped_arg + return + + if not stripped_arg: + session.pause('要查询的国家不能为空,请重新输入') + session.state[session.current_key] = stripped_arg |