zhenxun_bot/plugins/yiqing/data_source.py

107 lines
3.5 KiB
Python
Raw Normal View History

2021-10-03 14:24:07 +08:00
from configs.path_config import TEXT_PATH
2022-01-05 22:32:59 +08:00
from typing import List, Union
2021-11-23 21:44:59 +08:00
from utils.http_utils import AsyncHttpx
2022-01-05 22:32:59 +08:00
from utils.image_utils import text2image
from utils.message_builder import image
2022-02-19 18:20:19 +08:00
from nonebot.adapters.onebot.v11 import MessageSegment
2021-08-04 15:19:45 +08:00
import ujson as json
2021-05-20 19:25:51 +08:00
2022-02-19 18:20:19 +08:00
china_city = TEXT_PATH / "china_city.json"
2021-05-20 19:25:51 +08:00
2021-10-03 14:24:07 +08:00
data = {}
2021-05-20 19:25:51 +08:00
2022-05-09 21:42:24 +08:00
url = "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=diseaseh5Shelf"
2021-05-20 19:25:51 +08:00
2022-01-05 22:32:59 +08:00
async def get_yiqing_data(area: str) -> Union[str, MessageSegment]:
2021-11-23 21:44:59 +08:00
"""
查看疫情数据
:param area: 省份/城市
"""
2021-08-04 15:19:45 +08:00
global data
province = None
city = None
province_type = ""
2021-10-03 14:24:07 +08:00
if area == "中国":
2021-08-04 15:19:45 +08:00
province = area
province_type = ""
2021-10-04 22:58:11 +08:00
elif area[-1] == '' or (area in data.keys() and area[-1] != ""):
2021-10-03 14:24:07 +08:00
province = area if area[-1] != "" else area[:-1]
2021-08-11 11:44:39 +08:00
if len(data[province]) == 1:
province_type = ""
city = ""
else:
2021-10-03 14:24:07 +08:00
area = area[:-1] if area[-1] == "" else area
2021-08-04 15:19:45 +08:00
for p in data.keys():
if area in data[p]:
province = p
city = area
2022-05-09 21:42:24 +08:00
epidemic_data = (await AsyncHttpx.get(url)).json()["data"]["diseaseh5Shelf"]
2021-11-23 21:44:59 +08:00
last_update_time = epidemic_data["lastUpdateTime"]
if area == "中国":
data_ = epidemic_data["areaTree"][0]
else:
2022-01-05 22:32:59 +08:00
try:
data_ = [
x
for x in epidemic_data["areaTree"][0]["children"]
if x["name"] == province
][0]
if city:
2021-11-23 21:44:59 +08:00
data_ = [x for x in data_["children"] if x["name"] == city][0]
2022-01-05 22:32:59 +08:00
except IndexError:
return "未查询到..."
2021-11-23 21:44:59 +08:00
confirm = data_["total"]["confirm"] # 累计确诊
heal = data_["total"]["heal"] # 累计治愈
dead = data_["total"]["dead"] # 累计死亡
now_confirm = data_["total"]["nowConfirm"] # 目前确诊
add_confirm = data_["today"]["confirm"] # 新增确诊
2022-01-16 14:52:50 +08:00
grade = ""
_grade_color = ""
2022-05-09 21:42:24 +08:00
# if data_["total"].get("grade"):
# grade = data_["total"]["grade"]
# if "中风险" in grade:
# _grade_color = "#fa9424"
# else:
# _grade_color = "red"
2022-01-16 14:52:50 +08:00
dead_rate = f"{dead / confirm * 100:.2f}" # 死亡率
heal_rate = f"{heal / confirm * 100:.2f}" # 治愈率
2021-10-03 14:24:07 +08:00
x = f"{city}" if city else f"{province}{province_type}"
2022-02-19 18:20:19 +08:00
return image(b64=(await text2image(
f"""
{x} 疫情数据 {f"(<f font_color={_grade_color}>{grade}</f>)" if grade else ""}
2022-01-05 22:32:59 +08:00
目前确诊
确诊人数<f font_color=red>{now_confirm}(+{add_confirm})</f>
2022-02-19 18:20:19 +08:00
-----------------
2022-01-05 22:32:59 +08:00
累计数据
确诊人数<f font_color=red>{confirm}</f>
治愈人数<f font_color=#39de4b>{heal}</f>
死亡人数<f font_color=#191d19>{dead}</f>
治愈率{heal_rate}%
死亡率{dead_rate}%
2022-02-19 18:20:19 +08:00
更新日期{last_update_time}
2022-01-05 22:32:59 +08:00
""", font_size=30, color="#f9f6f2"
2022-02-19 18:20:19 +08:00
)).pic2bs4())
2021-05-20 19:25:51 +08:00
2021-10-04 22:58:11 +08:00
def get_city_and_province_list() -> List[str]:
2021-11-23 21:44:59 +08:00
"""
获取城市省份列表
"""
2021-08-04 15:19:45 +08:00
global data
2021-10-03 14:24:07 +08:00
if not data:
try:
with open(china_city, "r", encoding="utf8") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
2021-11-04 16:11:50 +08:00
city_list = ["中国"]
2021-08-04 15:19:45 +08:00
for p in data.keys():
for c in data[p]:
city_list.append(c)
2021-10-08 16:21:27 +08:00
city_list.append(p)
2021-08-04 15:19:45 +08:00
return city_list