refactor code

This commit is contained in:
hibiki
2021-07-30 21:21:51 +08:00
parent 2ad891aa1e
commit cc24822dca
165 changed files with 7815 additions and 8174 deletions
+36 -19
View File
@@ -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_=""))