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 | |
parent | 69e577fa093fdb8fdf3bc8f23b35951d8abb3b2c (diff) | |
download | ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.tar.gz ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.tar.bz2 ATRI-b6ee52b1e53cd44f43b588a59d590bb7cf02b6d5.zip |
some changes for weather
23 files changed, 549 insertions, 272 deletions
diff --git a/.idea/Aya.iml b/.idea/Aya.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/.idea/Aya.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> + <component name="TestRunnerService"> + <option name="PROJECT_TEST_RUNNER" value="Unittests" /> + </component> +</module>
\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8656114 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="JavaScriptSettings"> + <option name="languageLevel" value="ES6" /> + </component> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" /> +</project>
\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..eca873e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/Aya.iml" filepath="$PROJECT_DIR$/.idea/Aya.iml" /> + </modules> + </component> +</project>
\ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project>
\ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..eccc8d0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,209 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ChangeListManager"> + <list default="true" id="915041b0-9c7a-46d5-a551-ef5bd5074dc5" name="Default Changelist" comment=""> + <change afterPath="$PROJECT_DIR$/.idea/Aya.iml" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/AyaBot/plugins/Module/google_translate/__init__.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/AyaBot/plugins/covid19.py" beforeDir="false" afterPath="$PROJECT_DIR$/AyaBot/plugins/covid19.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/AyaBot/plugins/translate.py" beforeDir="false" afterPath="$PROJECT_DIR$/AyaBot/plugins/translate.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/AyaBot/plugins/weather.py" beforeDir="false" afterPath="$PROJECT_DIR$/AyaBot/plugins/weather.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/run.py" beforeDir="false" afterPath="$PROJECT_DIR$/run.py" afterDir="false" /> + </list> + <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> + <option name="SHOW_DIALOG" value="false" /> + <option name="HIGHLIGHT_CONFLICTS" value="true" /> + <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> + <option name="LAST_RESOLUTION" value="IGNORE" /> + </component> + <component name="FileEditorManager"> + <leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> + <file pinned="false" current-in-tab="true"> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/weather.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="532"> + <caret line="28" column="19" lean-forward="true" selection-start-line="28" selection-start-column="19" selection-end-line="28" selection-end-column="19" /> + <folding> + <element signature="e#0#9#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/Module/google_translate/__init__.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="2318"> + <caret line="122" column="14" lean-forward="true" selection-start-line="122" selection-start-column="14" selection-end-line="122" selection-end-column="14" /> + <folding> + <element signature="e#0#9#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + </leaf> + </component> + <component name="FileTemplateManagerImpl"> + <option name="RECENT_TEMPLATES"> + <list> + <option value="Python Script" /> + </list> + </option> + </component> + <component name="Git.Settings"> + <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> + </component> + <component name="IdeDocumentHistory"> + <option name="CHANGED_PATHS"> + <list> + <option value="$PROJECT_DIR$/AyaBot/plugins/tools/__init__.py" /> + <option value="$PROJECT_DIR$/AyaBot/plugins/tools/google_translate/__init__.py" /> + <option value="$PROJECT_DIR$/AyaBot/plugins/weather.py" /> + </list> + </option> + </component> + <component name="ProjectFrameBounds"> + <option name="x" value="377" /> + <option name="y" value="45" /> + <option name="width" value="1400" /> + <option name="height" value="1000" /> + </component> + <component name="ProjectView"> + <navigator proportions="" version="1"> + <foldersAlwaysOnTop value="true" /> + </navigator> + <panes> + <pane id="Scope" /> + <pane id="ProjectPane"> + <subPane> + <expand> + <path> + <item name="Aya" type="b2602c69:ProjectViewProjectNode" /> + <item name="Aya" type="462c0819:PsiDirectoryNode" /> + </path> + <path> + <item name="Aya" type="b2602c69:ProjectViewProjectNode" /> + <item name="Aya" type="462c0819:PsiDirectoryNode" /> + <item name="AyaBot" type="462c0819:PsiDirectoryNode" /> + </path> + <path> + <item name="Aya" type="b2602c69:ProjectViewProjectNode" /> + <item name="Aya" type="462c0819:PsiDirectoryNode" /> + <item name="AyaBot" type="462c0819:PsiDirectoryNode" /> + <item name="plugins" type="462c0819:PsiDirectoryNode" /> + </path> + </expand> + <select /> + </subPane> + </pane> + </panes> + </component> + <component name="PropertiesComponent"> + <property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> + <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> + <property name="WebServerToolWindowFactoryState" value="false" /> + <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" /> + <property name="nodejs_npm_path_reset_for_default_project" value="true" /> + </component> + <component name="RunDashboard"> + <option name="ruleStates"> + <list> + <RuleState> + <option name="name" value="ConfigurationTypeDashboardGroupingRule" /> + </RuleState> + <RuleState> + <option name="name" value="StatusDashboardGroupingRule" /> + </RuleState> + </list> + </option> + </component> + <component name="SvnConfiguration"> + <configuration /> + </component> + <component name="TaskManager"> + <task active="true" id="Default" summary="Default task"> + <changelist id="915041b0-9c7a-46d5-a551-ef5bd5074dc5" name="Default Changelist" comment="" /> + <created>1591429432399</created> + <option name="number" value="Default" /> + <option name="presentableId" value="Default" /> + <updated>1591429432399</updated> + <workItem from="1591429437702" duration="111000" /> + <workItem from="1591429651268" duration="1344000" /> + </task> + <servers /> + </component> + <component name="TimeTrackingManager"> + <option name="totallyTimeSpent" value="1455000" /> + </component> + <component name="ToolWindowManager"> + <frame x="377" y="45" width="1400" height="1000" extended-state="0" /> + <layout> + <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25373134" /> + <window_info id="Structure" order="1" side_tool="true" weight="0.25" /> + <window_info id="Favorites" order="2" side_tool="true" /> + <window_info anchor="bottom" id="Message" order="0" /> + <window_info anchor="bottom" id="Find" order="1" /> + <window_info anchor="bottom" id="Run" order="2" /> + <window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> + <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> + <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> + <window_info anchor="bottom" id="TODO" order="6" /> + <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" /> + <window_info anchor="bottom" id="Version Control" order="8" /> + <window_info anchor="bottom" id="Database Changes" order="9" /> + <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" /> + <window_info anchor="bottom" id="Terminal" order="11" /> + <window_info anchor="bottom" id="Python Console" order="12" /> + <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> + <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> + <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> + <window_info anchor="right" id="SciView" order="3" weight="0.32910448" /> + <window_info anchor="right" id="Database" order="4" /> + </layout> + </component> + <component name="TypeScriptGeneratedFilesManager"> + <option name="version" value="1" /> + </component> + <component name="editorHistoryManager"> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/tools/google_translate.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="228"> + <caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" /> + <folding> + <element signature="e#0#9#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/tools/__init__.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="-152"> + <caret line="1" column="15" lean-forward="true" selection-start-line="1" selection-start-column="15" selection-end-line="1" selection-end-column="15" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/Module/google_translate/__init__.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="2318"> + <caret line="122" column="14" lean-forward="true" selection-start-line="122" selection-start-column="14" selection-end-line="122" selection-end-column="14" /> + <folding> + <element signature="e#0#9#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/AyaBot/plugins/weather.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="532"> + <caret line="28" column="19" lean-forward="true" selection-start-line="28" selection-start-column="19" selection-end-line="28" selection-end-column="19" /> + <folding> + <element signature="e#0#9#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </component> +</project>
\ No newline at end of file diff --git a/AyaBot/plugins/Module/google_translate/__init__.py b/AyaBot/plugins/Module/google_translate/__init__.py new file mode 100644 index 0000000..fbb732d --- /dev/null +++ b/AyaBot/plugins/Module/google_translate/__init__.py @@ -0,0 +1,144 @@ +import re +import requests +import demjson +from pprint import pformat, pprint +from urllib.parse import urlencode + + +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 Sr(a, TKK): + a = ''.join(a['a']['b']['q']) + return mr(a, TKK) +d = { + 'a':{ + 'a': ['q'], + 'b': { + 'q': ['me'] + }, + 'c': 1, + 'g': 1 + }, + 'b': 1, + 'c': None, + 'j': False, +} +TKK = '426151.3141811846' +tk = Sr(d, TKK) +print(tk) +""" + +session = requests.session() + + +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 + + +def ref_words(q='hello', source='en', to='zh-CN'): + params = { + 'q': q, + 'client': 'translate-web', + 'ds': 'translate', + 'hl': source, + 'requiredfields': f'tl:{to}', + 'callback': 'window.google.ref_words' + } + url = 'https://clients1.google.com/complete/search?' + headers = { + 'Referer': 'https://translate.google.cn/', + 'Host': 'clients1.google.cn', + } + resp = session.get(url, params=params, headers=headers) + if resp.status_code == 200: + resp.encoding = 'utf-8' + result = re.search(r'window.google.ref_words\((.*)\)', resp.text).group(1) + json_data = demjson.decode(result) + data_list = list(map(lambda x: x[0], json_data[1])) + return data_list + else: + return None
\ No newline at end of file diff --git a/AyaBot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc b/AyaBot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc Binary files differnew file mode 100644 index 0000000..2e141ef --- /dev/null +++ b/AyaBot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc diff --git a/AyaBot/plugins/__pycache__/awsl.cpython-37.pyc b/AyaBot/plugins/__pycache__/awsl.cpython-37.pyc Binary files differdeleted file mode 100644 index 6c47288..0000000 --- a/AyaBot/plugins/__pycache__/awsl.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/bilibili.cpython-37.pyc b/AyaBot/plugins/__pycache__/bilibili.cpython-37.pyc Binary files differdeleted file mode 100644 index 59bdbf0..0000000 --- a/AyaBot/plugins/__pycache__/bilibili.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/bilibilifan.cpython-37.pyc b/AyaBot/plugins/__pycache__/bilibilifan.cpython-37.pyc Binary files differdeleted file mode 100644 index e6f89bd..0000000 --- a/AyaBot/plugins/__pycache__/bilibilifan.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/broadcast.cpython-37.pyc b/AyaBot/plugins/__pycache__/broadcast.cpython-37.pyc Binary files differdeleted file mode 100644 index ed83918..0000000 --- a/AyaBot/plugins/__pycache__/broadcast.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/bug.cpython-37.pyc b/AyaBot/plugins/__pycache__/bug.cpython-37.pyc Binary files differdeleted file mode 100644 index 17caf47..0000000 --- a/AyaBot/plugins/__pycache__/bug.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc b/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc Binary files differindex edecda4..be04c61 100644 --- a/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc +++ b/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc diff --git a/AyaBot/plugins/__pycache__/math.cpython-37.pyc b/AyaBot/plugins/__pycache__/math.cpython-37.pyc Binary files differdeleted file mode 100644 index b29588f..0000000 --- a/AyaBot/plugins/__pycache__/math.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/r6.cpython-37.pyc b/AyaBot/plugins/__pycache__/r6.cpython-37.pyc Binary files differdeleted file mode 100644 index f10eb1b..0000000 --- a/AyaBot/plugins/__pycache__/r6.cpython-37.pyc +++ /dev/null diff --git a/AyaBot/plugins/__pycache__/translate.cpython-37.pyc b/AyaBot/plugins/__pycache__/translate.cpython-37.pyc Binary files differindex 40e59c6..6346914 100644 --- a/AyaBot/plugins/__pycache__/translate.cpython-37.pyc +++ b/AyaBot/plugins/__pycache__/translate.cpython-37.pyc diff --git a/AyaBot/plugins/__pycache__/weather.cpython-37.pyc b/AyaBot/plugins/__pycache__/weather.cpython-37.pyc Binary files differindex db86c0c..733d83e 100644 --- a/AyaBot/plugins/__pycache__/weather.cpython-37.pyc +++ b/AyaBot/plugins/__pycache__/weather.cpython-37.pyc diff --git a/AyaBot/plugins/__pycache__/yiqing.cpython-37.pyc b/AyaBot/plugins/__pycache__/yiqing.cpython-37.pyc Binary files differdeleted file mode 100644 index db96f6f..0000000 --- a/AyaBot/plugins/__pycache__/yiqing.cpython-37.pyc +++ /dev/null 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 diff --git a/AyaBot/plugins/translate.py b/AyaBot/plugins/translate.py index b49aedd..6422380 100644 --- a/AyaBot/plugins/translate.py +++ b/AyaBot/plugins/translate.py @@ -1,155 +1,21 @@ import re -import requests +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 #FROM Joenothing-lst -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 Sr(a, TKK): - a = ''.join(a['a']['b']['q']) - return mr(a, TKK) -d = { - 'a':{ - 'a': ['q'], - 'b': { - 'q': ['me'] - }, - 'c': 1, - 'g': 1 - }, - 'b': 1, - 'c': None, - 'j': False, -} -TKK = '426151.3141811846' -tk = Sr(d, TKK) -print(tk) -""" - - -session = requests.session() - - -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 - - -def ref_words(q='hello', source='en', to='zh-CN'): - params = { - 'q': q, - 'client': 'translate-web', - 'ds': 'translate', - 'hl': source, - 'requiredfields': f'tl:{to}', - 'callback':'window.google.ref_words' - } - url = 'https://clients1.google.com/complete/search?' - headers = { - 'Referer': 'https://translate.google.cn/', - 'Host': 'clients1.google.cn', - } - resp = session.get(url, params=params, headers=headers) - if resp.status_code == 200: - resp.encoding = 'utf-8' - result = re.search(r'window.google.ref_words\((.*)\)', resp.text).group(1) - json_data = demjson.decode(result) - data_list = list(map(lambda x:x[0], json_data[1])) - return data_list - else: - return None - - @on_command('ja_to_zh', aliases=('日语翻译',), only_to_me=False) async def _(session: CommandSession): if ' ' in session.ctx['raw_message']: msg=session.ctx['raw_message'][5:] - re_msg = translate(msg[:4999], to='zh-CN', source='ja') + re_msg = google_translate.translate(msg[:4999], to='zh-CN', source='ja') if re_msg[0]!='' and re_msg[0]!=msg: await session.send(re_msg[0]) @@ -157,7 +23,7 @@ async def _(session: CommandSession): async def _(session: CommandSession): if ' ' in session.ctx['raw_message']: msg=session.ctx['raw_message'][5:] - re_msg = translate(msg[:4999], to='zh-CN', source='en') + re_msg = google_translate.translate(msg[:4999], to='zh-CN', source='en') if re_msg[0]!='': await session.send(re_msg[0]) @@ -165,7 +31,7 @@ async def _(session: CommandSession): async def _(session: CommandSession): if ' ' in session.ctx['raw_message']: msg=session.ctx['raw_message'][5:] - re_msg = translate(msg[:4999], to='ja', source='zh-CN') + re_msg = google_translate.translate(msg[:4999], to='ja', source='zh-CN') if re_msg[0]!='': await session.send(re_msg[0]) @@ -173,6 +39,6 @@ async def _(session: CommandSession): async def _(session: CommandSession): if ' ' in session.ctx['raw_message']: msg=session.ctx['raw_message'][5:] - re_msg = translate(msg[:4999], to='en', source='zh-CN') + re_msg = google_translate.translate(msg[:4999], to='en', source='zh-CN') if re_msg[0]!='': await session.send(re_msg[0])
\ No newline at end of file diff --git a/AyaBot/plugins/weather.py b/AyaBot/plugins/weather.py index f6a78fe..d439eeb 100644 --- a/AyaBot/plugins/weather.py +++ b/AyaBot/plugins/weather.py @@ -1,46 +1,139 @@ +import os +import re +import sys +sys.path.append('D:\code\Aya\AyaBot\plugins\Module') import json import requests from nonebot import on_command, CommandSession +import google_translate +API_URL = 'https://api.weatherbit.io/v2.0/current?city={city}' +API_URL_2 = ',CN&key=1df2eb2951f3470a94cb323bb4647c18' -API_URL = 'https://www.tianqiapi.com/free/day?appid=36628957&appsecret=WKn4dtVg&city=' +LIST = """{city} 情况如下: +截至: {ob_time} +天气: {description} +温度: {temp} +风速: {wind_spd} +风向: {wind_cdir} +云覆盖率(%): {clouds}""" -LIST = """{city} 今日信息如下: -更新时间:{time} -天气情况:{wea} -空气质量:{air} -温度: - 现在温度:{tem} - 最高温度:{temday} - 最低温度:{temnight} -风向:{win} -风力等级:{winspeed} -风速:{winmeter}""" +LIST_ALL = """{city} 详细情况如下: +纬度(°): {lat} +经度(°): {lon} +日出时间(HH:MM): {sunrise} +日落时间(HH:MM): {sunset} +本地时区: {timezone} +源站ID: {station} +上次观察时间(YYYY-MM-DD HH:MM): {ob_time} +当前周期小时(YYYY-MM-DD HH:MM): {datetime} +压力(mb): {pres} +海平面压力(mb): {slp} +风速(m/s): {wind_spd} +风向(°): {wind_dir} +缩写风向: {wind_cdir} +风向全称: {wind_cdir_full} +温度(℃): {temp} +感觉温度(℃): {app_temp} +相对湿度(%): {rh} +露点(℃): {dewpt} +云覆盖率(%): {clouds} +一天的一部分(d/n): {pod} +现在天气: {description} +可见度(km): {vis} +液体当量沉淀速率(mm/hr): {precip} +降雪(mm/h): {snow} +紫外线指数(0-11+): {uv} +空气质量指数[美国-EPA标准0-+500] [Clear Sky]: {aqi} +漫射水平太阳辐照度(W/m^2) [Clear Sky]: {dhi} +普通太阳直射辐射(W/m^2) [Clear Sky]: {dni} +全球水平太阳辐照度(W/m^2): {ghi} +估计的太阳辐射(W/m^2): {solar_rad} +太阳斜角(°): {elev_angle} +太阳时角(°): {h_angle}""" -@on_command('weather', aliases=['天气', '查天气', '天气查询'], only_to_me=False) +@on_command('weather', aliases=['查天气', '天气', '天气情况'], only_to_me=False) async def weather(session: CommandSession): - city = session.get('city', prompt='你想查哪个城市呢?') - try: - res = API_URL + city - res1 = requests.get(res) - res1.encoding = 'utf-8' - html = res1.text - wt = json.loads(html) - await session.send(LIST.format( - city=wt["city"], - time=wt["update_time"], - wea=wt["wea"], - tem=wt["tem"], - temday=wt["tem_day"], - temnight=wt["tem_night"], - win=wt["win"], - winspeed=wt["win_speed"], - winmeter=wt["win_meter"], - air=wt["air"] - ) + city = session.get('city', prompt='请键入你需要查询的城市(例:北京)') + re_msg = google_translate.translate(city[:4999], to='en', source='zh-CN') + URL = API_URL + re_msg[0] + API_URL_2 + # print(URL) + res = requests.get(URL) + res.encoding = 'utf-8' + html = res.text + wt = json.loads(html) + await session.send(LIST.format( + city=wt["data"][0]["city_name"], + ob_time=wt["data"][0]["ob_time"], + description=wt["data"][0]["weather"]["description"], + temp=wt["data"][0]["temp"], + wind_spd=wt["data"][0]["wind_spd"], + wind_cdir=wt["data"][0]["wind_cdir"], + clouds=wt["data"][0]["clouds"], ) - except: - await session.send('获取数据时出问题,请重试') - return
\ No newline at end of file + ) + +@on_command('wtlist', aliases=['天气详细']) +async def _(session: CommandSession): + city = session.get('city', prompt='请键入你需要查询的城市(例:北京)') + session.send('正在搜寻...') + re_msg = google_translate.translate(city[:4999], to='en', source='zh-CN') + URL = API_URL + re_msg[0] + API_URL_2 + # print(URL) + res = requests.get(URL) + res.encoding = 'utf-8' + html = res.text + wt = json.loads(html) + await session.send(LIST_ALL.format( + rh=wt["data"][0]["rh"], + pod=wt["data"][0]["pod"], + lon=wt["data"][0]["lon"], + pres=wt["data"][0]["pres"], + timezone=wt["data"][0]["timezone"], + ob_time=wt["data"][0]["ob_time"], + clouds=wt["data"][0]["clouds"], + solar_rad=wt["data"][0]["solar_rad"], + city=wt["data"][0]["city_name"], + wind_spd=wt["data"][0]["wind_spd"], + last_ob_time=wt["data"][0]["last_ob_time"], + wind_cdir_full=wt["data"][0]["wind_cdir_full"], + wind_cdir=wt["data"][0]["wind_cdir"], + temp=wt["data"][0]["temp"], + slp=wt["data"][0]["slp"], + vis=wt["data"][0]["vis"], + h_angle=wt["data"][0]["h_angle"], + sunset=wt["data"][0]["sunset"], + dni=wt["data"][0]["dni"], + dewpt=wt["data"][0]["dewpt"], + snow=wt["data"][0]["snow"], + uv=wt["data"][0]["uv"], + precip=wt["data"][0]["precip"], + wind_dir=wt["data"][0]["wind_dir"], + sunrise=wt["data"][0]["sunrise"], + ghi=wt["data"][0]["ghi"], + dhi=wt["data"][0]["dhi"], + aqi=wt["data"][0]["aqi"], + lat=wt["data"][0]["lat"], + description=wt["data"][0]["weather"]["description"], + datetime=wt["data"][0]["datetime"], + station=wt["data"][0]["station"], + elev_angle=wt["data"][0]["elev_angle"], + app_temp=wt["data"][0]["app_temp"], + ) + ) + + + [email protected]_parser +async def _(session: CommandSession): + stripped_arg = session.current_arg_text.strip() + if session.is_first_run: + if stripped_arg: + session.state['city'] = stripped_arg + return + + if not stripped_arg: + session.pause('要查询的城市不能为空,请重新输入') + session.state[session.current_key] = stripped_arg
\ No newline at end of file diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc Binary files differdeleted file mode 100644 index 535caa2..0000000 --- a/__pycache__/config.cpython-37.pyc +++ /dev/null @@ -9,5 +9,4 @@ if __name__ == '__main__': nonebot.load_plugins( path.join(path.dirname(__file__), 'AyaBot', 'plugins'), 'AyaBot.plugins') - nonebot.run() - + nonebot.run()
\ No newline at end of file |