From df0d15c1b3d0efcb643ba727e5fcd4e7180fa158 Mon Sep 17 00:00:00 2001 From: yzyyz1387 <1796031384@qq.com> Date: Fri, 24 Dec 2021 04:03:26 +0800 Subject: [PATCH 1/7] International information inquiry is supported --- .idea/workspace.xml | 48 ++++++++++++++++++++++++ plugins/yiqing/__init__.py | 12 +++++- plugins/yiqing/other_than.py | 73 ++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 plugins/yiqing/other_than.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..cb86d2ee --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + 1640286031697 + + + + + + \ No newline at end of file diff --git a/plugins/yiqing/__init__.py b/plugins/yiqing/__init__.py index 29674c2e..5b7f6f1e 100755 --- a/plugins/yiqing/__init__.py +++ b/plugins/yiqing/__init__.py @@ -5,7 +5,7 @@ from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent from nonebot.typing import T_State from utils.utils import get_message_text from configs.config import NICKNAME - +from .other_than import get_other_data __zx_plugin_name__ = "疫情查询" __plugin_usage__ = """ @@ -54,4 +54,12 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情失败" ) else: - await yiqing.send(f"{NICKNAME}没有查到{msg}的疫情查询...") + rely=await get_yiqing_data(msg) + if rely: + await yiqing.send(rely) + logger.info( + f"(USER {event.user_id}, GROUP " + f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情失败" + ) + else: + await yiqing.send(f"{NICKNAME}没有查到{msg}的疫情查询...") \ No newline at end of file diff --git a/plugins/yiqing/other_than.py b/plugins/yiqing/other_than.py new file mode 100644 index 00000000..2eec1e97 --- /dev/null +++ b/plugins/yiqing/other_than.py @@ -0,0 +1,73 @@ +# python3 +# -*- coding: utf-8 -*- +# @Time : 2021/12/23 23:04 +# @Author : yzyyz +# @Email : youzyyz1384@qq.com +# @File : other_than.py +# @Software: PyCharm +import httpx +import re +import json +__doc__='''爬虫实现国外疫情数据(找不到好接口)''' + + +def intcomma(value): + """ + 数字格式化 + """ + orig = str(value) + new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) + if orig == new: + return new + else: + return intcomma(new) + +async def get_other_data(place:str): + """ + :param place: 地名 + :return: 格式化字符串 + """ + try: + html = httpx.get('https://news.ifeng.com/c/special/7uLj4F83Cqm').text.replace('\n', '').replace(' ', '') + except: + return + find_data = re.compile(r'varallData=(.*?);') + sum = re.findall(find_data, html)[0] + sum = json.loads(sum) + other_country=sum['yiqing_v2']['dataList'][29]['child'] + for country in other_country: + if place==country['name2']: + return ( + f"{place} 疫情数据:\n" + "——————————————\n" + f"新增病例:{intcomma(country['quezhen_add'])}\n" + f"现有确诊:{intcomma(country['quezhen_xianyou'])}\n" + f"累计确诊:{intcomma(country['quezhen'])}\n" + f"累计治愈:{intcomma(country['zhiyu'])}\n" + f"死亡:{intcomma(country['siwang'])}\n" + "——————————————" + #f"更新时间:{country['sys_publishDateTime']}" + #时间无法精确到分钟,网页用了js我暂时找不到 + ) + else: + for city in country['child']: + if place==city['name3']: + return ( + f"{place} 疫情数据:\n" + "——————————————\n" + f"新增病例:{intcomma(city['quezhen_add'])}\n" + f"累计确诊:{intcomma(city['quezhen'])}\n" + f"累计治愈:{intcomma(city['zhiyu'])}\n" + f"死亡:{intcomma(city['siwang'])}\n" + "——————————————" + + ) + return + + +if __name__ == '__main__': + a=get_other_data('英国') + print(a) + # print(get_other_data('美国')) + # print(get_other_data('印度')) + # print(get_other_data('伦敦')) \ No newline at end of file From 7567794f748cee3cc4a321540e258398e5f3d447 Mon Sep 17 00:00:00 2001 From: yzyyz1387 <1796031384@qq.com> Date: Fri, 24 Dec 2021 04:05:44 +0800 Subject: [PATCH 2/7] International information inquiry is supported --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 4f753bbb..0580f533 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,8 @@ share/python-wheels/ *.egg MANIFEST +.idea/ + # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. From 19dbd3f2b0cdceeb0530bec6eac7bf81f97cb82d Mon Sep 17 00:00:00 2001 From: yzyyz1387 <1796031384@qq.com> Date: Fri, 24 Dec 2021 04:06:56 +0800 Subject: [PATCH 3/7] International information inquiry is supported --- .gitignore | 1 - .idea/workspace.xml | 48 --------------------------------------------- 2 files changed, 49 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 0580f533..f8dad0fc 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,6 @@ share/python-wheels/ *.egg MANIFEST -.idea/ # PyInstaller # Usually these files are written by a python script from a template diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index cb86d2ee..00000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - 1640286031697 - - - - - - \ No newline at end of file From 99637de1dec4bd5edb35579b69cb52575e9a024d Mon Sep 17 00:00:00 2001 From: yzyyz1387 <1796031384@qq.com> Date: Fri, 24 Dec 2021 04:08:07 +0800 Subject: [PATCH 4/7] International information inquiry is supported --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index f8dad0fc..4f753bbb 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,6 @@ share/python-wheels/ *.egg MANIFEST - # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. From 9b4cde819ef64b8e63446c1b8a193902f2278bb8 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Fri, 24 Dec 2021 10:09:53 +0800 Subject: [PATCH 5/7] Update __init__.py --- plugins/yiqing/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/yiqing/__init__.py b/plugins/yiqing/__init__.py index 5b7f6f1e..df306eab 100755 --- a/plugins/yiqing/__init__.py +++ b/plugins/yiqing/__init__.py @@ -12,7 +12,7 @@ __plugin_usage__ = """ usage: 全国疫情查询 指令: - 疫情 中国 + 疫情 中国/美国/英国... 疫情 [省份/城市] * 当省份与城市重名时,可在后添加 "市" 或 "省" * 示例:疫情 吉林 <- [省] @@ -22,7 +22,7 @@ __plugin_des__ = "实时疫情数据查询" __plugin_cmd__ = ["疫情 [省份/城市]", "疫情 中国"] __plugin_type__ = ('一些工具',) __plugin_version__ = 0.1 -__plugin_author__ = "HibiKier" +__plugin_author__ = "HibiKier & yzyyz1387" __plugin_settings__ = { "level": 5, "default_status": True, @@ -54,7 +54,7 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情失败" ) else: - rely=await get_yiqing_data(msg) + rely = await get_other_data(msg) if rely: await yiqing.send(rely) logger.info( @@ -62,4 +62,4 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情失败" ) else: - await yiqing.send(f"{NICKNAME}没有查到{msg}的疫情查询...") \ No newline at end of file + await yiqing.send(f"{NICKNAME}没有查到{msg}的疫情查询...") From 71f860a50090453725241f061d85d068dd4cb250 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Fri, 24 Dec 2021 10:10:48 +0800 Subject: [PATCH 6/7] Update other_than.py --- plugins/yiqing/other_than.py | 59 +++++++++++++++++------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/plugins/yiqing/other_than.py b/plugins/yiqing/other_than.py index 2eec1e97..6d0b5253 100644 --- a/plugins/yiqing/other_than.py +++ b/plugins/yiqing/other_than.py @@ -5,38 +5,44 @@ # @Email : youzyyz1384@qq.com # @File : other_than.py # @Software: PyCharm -import httpx +from utils.http_utils import AsyncHttpx +from typing import Optional +from services.log import logger import re import json -__doc__='''爬虫实现国外疫情数据(找不到好接口)''' + +__doc__ = """爬虫实现国外疫情数据(找不到好接口)""" -def intcomma(value): +def intcomma(value) -> str: """ 数字格式化 """ orig = str(value) - new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) - if orig == new: - return new - else: - return intcomma(new) + new = re.sub(r"^(-?\d+)(\d{3})", r"\g<1>,\g<2>", orig) + return new if orig == new else intcomma(new) -async def get_other_data(place:str): + +async def get_other_data(place: str) -> Optional[str]: """ :param place: 地名 :return: 格式化字符串 """ try: - html = httpx.get('https://news.ifeng.com/c/special/7uLj4F83Cqm').text.replace('\n', '').replace(' ', '') - except: - return - find_data = re.compile(r'varallData=(.*?);') - sum = re.findall(find_data, html)[0] - sum = json.loads(sum) - other_country=sum['yiqing_v2']['dataList'][29]['child'] + html = ( + (await AsyncHttpx.get("https://news.ifeng.com/c/special/7uLj4F83Cqm")) + .text.replace("\n", "") + .replace(" ", "") + ) + except Exception as e: + logger.error(f"疫情查询发生错误 {type(e)}:{e}") + return None + find_data = re.compile(r"varallData=(.*?);") + sum_ = re.findall(find_data, html)[0] + sum_ = json.loads(sum_) + other_country = sum_["yiqing_v2"]["dataList"][29]["child"] for country in other_country: - if place==country['name2']: + if place == country["name2"]: return ( f"{place} 疫情数据:\n" "——————————————\n" @@ -46,12 +52,12 @@ async def get_other_data(place:str): f"累计治愈:{intcomma(country['zhiyu'])}\n" f"死亡:{intcomma(country['siwang'])}\n" "——————————————" - #f"更新时间:{country['sys_publishDateTime']}" - #时间无法精确到分钟,网页用了js我暂时找不到 + # f"更新时间:{country['sys_publishDateTime']}" + # 时间无法精确到分钟,网页用了js我暂时找不到 ) else: - for city in country['child']: - if place==city['name3']: + for city in country["child"]: + if place == city["name3"]: return ( f"{place} 疫情数据:\n" "——————————————\n" @@ -60,14 +66,5 @@ async def get_other_data(place:str): f"累计治愈:{intcomma(city['zhiyu'])}\n" f"死亡:{intcomma(city['siwang'])}\n" "——————————————" - ) - return - - -if __name__ == '__main__': - a=get_other_data('英国') - print(a) - # print(get_other_data('美国')) - # print(get_other_data('印度')) - # print(get_other_data('伦敦')) \ No newline at end of file + return None From 58957cdf44aedf226b4cc23c58c457d7cb8c0e9e Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Fri, 24 Dec 2021 10:20:57 +0800 Subject: [PATCH 7/7] Update other_than.py --- plugins/yiqing/other_than.py | 59 +++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/plugins/yiqing/other_than.py b/plugins/yiqing/other_than.py index 6d0b5253..50538208 100644 --- a/plugins/yiqing/other_than.py +++ b/plugins/yiqing/other_than.py @@ -39,32 +39,35 @@ async def get_other_data(place: str) -> Optional[str]: return None find_data = re.compile(r"varallData=(.*?);") sum_ = re.findall(find_data, html)[0] - sum_ = json.loads(sum_) - other_country = sum_["yiqing_v2"]["dataList"][29]["child"] - for country in other_country: - if place == country["name2"]: - return ( - f"{place} 疫情数据:\n" - "——————————————\n" - f"新增病例:{intcomma(country['quezhen_add'])}\n" - f"现有确诊:{intcomma(country['quezhen_xianyou'])}\n" - f"累计确诊:{intcomma(country['quezhen'])}\n" - f"累计治愈:{intcomma(country['zhiyu'])}\n" - f"死亡:{intcomma(country['siwang'])}\n" - "——————————————" - # f"更新时间:{country['sys_publishDateTime']}" - # 时间无法精确到分钟,网页用了js我暂时找不到 - ) - else: - for city in country["child"]: - if place == city["name3"]: - return ( - f"{place} 疫情数据:\n" - "——————————————\n" - f"新增病例:{intcomma(city['quezhen_add'])}\n" - f"累计确诊:{intcomma(city['quezhen'])}\n" - f"累计治愈:{intcomma(city['zhiyu'])}\n" - f"死亡:{intcomma(city['siwang'])}\n" - "——————————————" - ) + try: + sum_ = json.loads(sum_) + other_country = sum_["yiqing_v2"]["dataList"][29]["child"] + for country in other_country: + if place == country["name2"]: + return ( + f"{place} 疫情数据:\n" + "——————————————\n" + f"新增病例:{intcomma(country['quezhen_add'])}\n" + f"现有确诊:{intcomma(country['quezhen_xianyou'])}\n" + f"累计确诊:{intcomma(country['quezhen'])}\n" + f"累计治愈:{intcomma(country['zhiyu'])}\n" + f"死亡:{intcomma(country['siwang'])}\n" + "——————————————" + # f"更新时间:{country['sys_publishDateTime']}" + # 时间无法精确到分钟,网页用了js我暂时找不到 + ) + else: + for city in country["child"]: + if place == city["name3"]: + return ( + f"{place} 疫情数据:\n" + "——————————————\n" + f"新增病例:{intcomma(city['quezhen_add'])}\n" + f"累计确诊:{intcomma(city['quezhen'])}\n" + f"累计治愈:{intcomma(city['zhiyu'])}\n" + f"死亡:{intcomma(city['siwang'])}\n" + "——————————————" + ) + except Exception as e: + logger.error(f"疫情查询发生错误 {type(e)}:{e}") return None