zhenxun_bot/plugins/yiqing/data_source.py

95 lines
3.0 KiB
Python
Raw Normal View History

2021-10-03 14:24:07 +08:00
from configs.path_config import TEXT_PATH
2021-08-04 15:19:45 +08:00
from typing import List
from pathlib import Path
2021-11-23 21:44:59 +08:00
from utils.http_utils import AsyncHttpx
2021-08-04 15:19:45 +08:00
import ujson as json
2021-05-20 19:25:51 +08:00
2021-10-03 14:24:07 +08:00
china_city = Path(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
2021-08-04 15:19:45 +08:00
url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
2021-05-20 19:25:51 +08:00
2021-08-04 15:19:45 +08:00
async def get_yiqing_data(area: str):
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
2021-11-23 21:44:59 +08:00
epidemic_data = json.loads((await AsyncHttpx.get(url)).json()["data"])
last_update_time = epidemic_data["lastUpdateTime"]
if area == "中国":
data_ = epidemic_data["areaTree"][0]
else:
data_ = [
x
for x in epidemic_data["areaTree"][0]["children"]
if x["name"] == province
][0]
if city:
try:
data_ = [x for x in data_["children"] if x["name"] == city][0]
except IndexError:
return "未查询到..."
confirm = data_["total"]["confirm"] # 累计确诊
heal = data_["total"]["heal"] # 累计治愈
dead = data_["total"]["dead"] # 累计死亡
dead_rate = data_["total"]["deadRate"] # 死亡率
heal_rate = data_["total"]["healRate"] # 治愈率
now_confirm = data_["total"]["nowConfirm"] # 目前确诊
suspect = data_["total"]["suspect"] # 疑似
add_confirm = data_["today"]["confirm"] # 新增确诊
2021-10-03 14:24:07 +08:00
x = f"{city}" if city else f"{province}{province_type}"
2021-08-04 15:19:45 +08:00
return (
f"{x} 疫情数据:\n"
f"\t目前确诊:\n"
f"\t\t确诊人数:{now_confirm}(+{add_confirm})\n"
f"\t\t疑似人数:{suspect}\n"
f"==================\n"
f"\t累计数据:\n"
f"\t\t确诊人数:{confirm}\n"
f"\t\t治愈人数:{heal}\n"
f"\t\t死亡人数:{dead}\n"
f"\t治愈率:{heal_rate}%\n"
f"\t死亡率:{dead_rate}%\n"
f"更新日期:{last_update_time}"
)
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