Merge pull request #99 from yzyyz1387/fix_zx

支持国际疫情数据查询
This commit is contained in:
HibiKier 2021-12-24 10:22:45 +08:00 committed by GitHub
commit 52ba2cf89a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 4 deletions

View File

@ -5,14 +5,14 @@ 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__ = """
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,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_other_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}的疫情查询...")

View File

@ -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
from utils.http_utils import AsyncHttpx
from typing import Optional
from services.log import logger
import re
import json
__doc__ = """爬虫实现国外疫情数据(找不到好接口)"""
def intcomma(value) -> str:
"""
数字格式化
"""
orig = str(value)
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) -> Optional[str]:
"""
:param place: 地名
:return: 格式化字符串
"""
try:
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=(.*?);</script>")
sum_ = re.findall(find_data, html)[0]
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