mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
refactor code
This commit is contained in:
@@ -10,32 +10,50 @@ from utils.utils import get_local_proxy
|
||||
url = "https://api.yimian.xyz/coro/"
|
||||
|
||||
|
||||
async def get_yiqing_data(province, city_=''):
|
||||
async def get_yiqing_data(province, city_=""):
|
||||
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()
|
||||
with open(TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", 'w') as f:
|
||||
with open(
|
||||
TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", "w"
|
||||
) as f:
|
||||
json.dump(datalist, f)
|
||||
datalist = json.load(open(TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", 'r'))
|
||||
result = ''
|
||||
datalist = json.load(
|
||||
open(TXT_PATH + "yiqing/" + str(datetime.now().date()) + ".json", "r")
|
||||
)
|
||||
result = ""
|
||||
for data in datalist:
|
||||
if data['provinceShortName'] == province:
|
||||
if city_ == '':
|
||||
result = province + "疫情数据:\n现存确诊: " + \
|
||||
str(data['currentConfirmedCount']) + "\n累计确诊: " + \
|
||||
str(data['confirmedCount']) + "\n治愈: " + \
|
||||
str(data['curedCount']) + "\n死亡: " + \
|
||||
str(data['deadCount'])
|
||||
if data["provinceShortName"] == province:
|
||||
if city_ == "":
|
||||
result = (
|
||||
province
|
||||
+ "疫情数据:\n现存确诊: "
|
||||
+ str(data["currentConfirmedCount"])
|
||||
+ "\n累计确诊: "
|
||||
+ str(data["confirmedCount"])
|
||||
+ "\n治愈: "
|
||||
+ str(data["curedCount"])
|
||||
+ "\n死亡: "
|
||||
+ str(data["deadCount"])
|
||||
)
|
||||
break
|
||||
else:
|
||||
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'])
|
||||
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"])
|
||||
)
|
||||
break
|
||||
return result
|
||||
|
||||
@@ -45,6 +63,5 @@ def clear_data():
|
||||
os.remove(TXT_PATH + "yiqing/" + file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_yiqing_data("浙江", city_=''))
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(get_yiqing_data("浙江", city_=""))
|
||||
|
||||
Reference in New Issue
Block a user