zhenxun_bot/plugins/yiqing/data_source.py

68 lines
2.4 KiB
Python
Raw Normal View History

2021-05-20 19:25:51 +08:00
from datetime import datetime
import aiohttp
2021-06-30 19:50:55 +08:00
from utils.user_agent import get_user_agent
2021-05-20 19:25:51 +08:00
import json
import os
from configs.path_config import TXT_PATH
2021-06-30 19:50:55 +08:00
from utils.utils import get_local_proxy
2021-05-20 19:25:51 +08:00
url = "https://api.yimian.xyz/coro/"
2021-07-30 21:21:51 +08:00
async def get_yiqing_data(province, city_=""):
2021-05-20 19:25:51 +08:00
if not os.path.exists(TXT_PATH + "yiqing/"):
os.mkdir(TXT_PATH + "yiqing/")
if not os.path.exists(TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json"):
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
async with session.get(url, proxy=get_local_proxy(), timeout=7) as response:
datalist = await response.json()
2021-07-30 21:21:51 +08:00
with open(
TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", "w"
) as f:
2021-05-20 19:25:51 +08:00
json.dump(datalist, f)
2021-07-30 21:21:51 +08:00
datalist = json.load(
open(TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", "r")
)
result = ""
2021-05-20 19:25:51 +08:00
for data in datalist:
2021-07-30 21:21:51 +08:00
if data["provinceShortName"] == province:
if city_ == "":
result = (
province
+ "疫情数据:\n现存确诊: "
+ str(data["currentConfirmedCount"])
+ "\n累计确诊: "
+ str(data["confirmedCount"])
+ "\n治愈: "
+ str(data["curedCount"])
+ "\n死亡: "
+ str(data["deadCount"])
)
2021-05-20 19:25:51 +08:00
break
else:
2021-07-30 21:21:51 +08:00
for city in data["cities"]:
if city["cityName"] == city_:
result = (
city_
+ "疫情数据:\n现存确诊: "
+ str(city["currentConfirmedCount"])
+ "\n累计确诊: "
+ str(city["confirmedCount"])
+ "\n治愈: "
+ str(city["curedCount"])
+ "\n死亡: "
+ str(city["deadCount"])
)
2021-05-20 19:25:51 +08:00
break
return result
def clear_data():
for file in os.listdir(TXT_PATH + "yiqing/"):
os.remove(TXT_PATH + "yiqing/" + file)
2021-07-30 21:21:51 +08:00
if __name__ == "__main__":
print(get_yiqing_data("浙江", city_=""))