2021-12-24 04:03:26 +08:00
|
|
|
|
# python3
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
# @Time : 2021/12/23 23:04
|
|
|
|
|
|
# @Author : yzyyz
|
|
|
|
|
|
# @Email : youzyyz1384@qq.com
|
|
|
|
|
|
# @File : other_than.py
|
|
|
|
|
|
# @Software: PyCharm
|
2021-12-24 10:10:48 +08:00
|
|
|
|
from utils.http_utils import AsyncHttpx
|
2022-02-19 18:20:19 +08:00
|
|
|
|
from nonebot.adapters.onebot.v11 import MessageSegment
|
2021-12-24 10:10:48 +08:00
|
|
|
|
from typing import Optional
|
|
|
|
|
|
from services.log import logger
|
2022-01-05 22:32:59 +08:00
|
|
|
|
from utils.image_utils import text2image
|
|
|
|
|
|
from utils.message_builder import image
|
|
|
|
|
|
from json.decoder import JSONDecodeError
|
2021-12-24 04:03:26 +08:00
|
|
|
|
import re
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
2021-12-24 10:10:48 +08:00
|
|
|
|
__doc__ = """爬虫实现国外疫情数据(找不到好接口)"""
|
2021-12-24 04:03:26 +08:00
|
|
|
|
|
2021-12-24 10:10:48 +08:00
|
|
|
|
|
|
|
|
|
|
def intcomma(value) -> str:
|
2021-12-24 04:03:26 +08:00
|
|
|
|
"""
|
|
|
|
|
|
数字格式化
|
|
|
|
|
|
"""
|
|
|
|
|
|
orig = str(value)
|
2021-12-24 10:10:48 +08:00
|
|
|
|
new = re.sub(r"^(-?\d+)(\d{3})", r"\g<1>,\g<2>", orig)
|
|
|
|
|
|
return new if orig == new else intcomma(new)
|
|
|
|
|
|
|
2021-12-24 04:03:26 +08:00
|
|
|
|
|
2022-01-05 22:32:59 +08:00
|
|
|
|
async def get_other_data(place: str, count: int = 0) -> Optional[MessageSegment]:
|
2021-12-24 04:03:26 +08:00
|
|
|
|
"""
|
|
|
|
|
|
:param place: 地名
|
2022-01-05 22:32:59 +08:00
|
|
|
|
:param count: 递归次数
|
2021-12-24 04:03:26 +08:00
|
|
|
|
:return: 格式化字符串
|
|
|
|
|
|
"""
|
2022-01-05 22:32:59 +08:00
|
|
|
|
if count == 5:
|
|
|
|
|
|
return None
|
2021-12-24 04:03:26 +08:00
|
|
|
|
try:
|
2021-12-24 10:10:48 +08:00
|
|
|
|
html = (
|
|
|
|
|
|
(await AsyncHttpx.get("https://news.ifeng.com/c/special/7uLj4F83Cqm"))
|
|
|
|
|
|
.text.replace("\n", "")
|
|
|
|
|
|
.replace(" ", "")
|
|
|
|
|
|
)
|
2022-01-05 22:32:59 +08:00
|
|
|
|
find_data = re.compile(r"varallData=(.*?);</script>")
|
|
|
|
|
|
sum_ = re.findall(find_data, html)[0]
|
|
|
|
|
|
sum_ = json.loads(sum_)
|
|
|
|
|
|
except JSONDecodeError:
|
|
|
|
|
|
return await get_other_data(place, count + 1)
|
2021-12-24 10:10:48 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"疫情查询发生错误 {type(e)}:{e}")
|
|
|
|
|
|
return None
|
2021-12-24 10:20:57 +08:00
|
|
|
|
try:
|
|
|
|
|
|
other_country = sum_["yiqing_v2"]["dataList"][29]["child"]
|
|
|
|
|
|
for country in other_country:
|
|
|
|
|
|
if place == country["name2"]:
|
2022-01-05 22:32:59 +08:00
|
|
|
|
return image(
|
2022-02-19 18:20:19 +08:00
|
|
|
|
b64=(await text2image(
|
2022-01-05 22:32:59 +08:00
|
|
|
|
f" {place} 疫情数据:\n"
|
|
|
|
|
|
"——————————————\n"
|
|
|
|
|
|
f" 新增病例:<f font_color=red>{intcomma(country['quezhen_add'])}</f>\n"
|
|
|
|
|
|
f" 现有确诊:<f font_color=red>{intcomma(country['quezhen_xianyou'])}</f>\n"
|
|
|
|
|
|
f" 累计确诊:<f font_color=red>{intcomma(country['quezhen'])}</f>\n"
|
|
|
|
|
|
f" 累计治愈:<f font_color=#39de4b>{intcomma(country['zhiyu'])}</f>\n"
|
|
|
|
|
|
f" 死亡:{intcomma(country['siwang'])}\n"
|
|
|
|
|
|
"——————————————"
|
|
|
|
|
|
# f"更新时间:{country['sys_publishDateTime']}"
|
|
|
|
|
|
# 时间无法精确到分钟,网页用了js我暂时找不到
|
|
|
|
|
|
,
|
|
|
|
|
|
font_size=30,
|
|
|
|
|
|
color="#f9f6f2",
|
|
|
|
|
|
padding=15
|
2022-02-19 18:20:19 +08:00
|
|
|
|
)).pic2bs4()
|
2021-12-24 10:20:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
for city in country["child"]:
|
|
|
|
|
|
if place == city["name3"]:
|
2022-01-05 22:32:59 +08:00
|
|
|
|
return image(
|
2022-02-19 18:20:19 +08:00
|
|
|
|
b64=(await text2image(
|
2022-01-05 22:32:59 +08:00
|
|
|
|
f"\n{place} 疫情数据:\n"
|
|
|
|
|
|
"——————————————\n"
|
|
|
|
|
|
f"\t新增病例:<f font_color=red>{intcomma(city['quezhen_add'])}</f>\n"
|
|
|
|
|
|
f"\t累计确诊:<f font_color=red>{intcomma(city['quezhen'])}</f>\n"
|
|
|
|
|
|
f"\t累计治愈:<f font_color=#39de4b>{intcomma(city['zhiyu'])}</f>\n"
|
|
|
|
|
|
f"\t死亡:{intcomma(city['siwang'])}\n"
|
|
|
|
|
|
"——————————————\n",
|
|
|
|
|
|
font_size=30,
|
|
|
|
|
|
color="#f9f6f2",
|
|
|
|
|
|
padding=15
|
2022-02-19 18:20:19 +08:00
|
|
|
|
)).pic2bs4()
|
2021-12-24 10:20:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"疫情查询发生错误 {type(e)}:{e}")
|
2021-12-24 10:10:48 +08:00
|
|
|
|
return None
|