替换aiohttp为hettpx,用于异步获取公告列表

This commit is contained in:
nicklly 2022-05-28 08:41:12 +08:00
parent 876fda38ad
commit 29d8d9337f

View File

@ -2,6 +2,7 @@ import os
import json import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from .aiorequests import get
import aiohttp import aiohttp
import asyncio import asyncio
import math import math
@ -76,9 +77,8 @@ def cache(ttl=timedelta(hours=1), arg_key=None):
@cache(ttl=timedelta(hours=3), arg_key='url') @cache(ttl=timedelta(hours=3), arg_key='url')
async def query_data(url): async def query_data(url):
try: try:
async with aiohttp.ClientSession() as session: resp = await get(url=url)
async with session.get(url) as resp: return resp.json()
return await resp.json()
except: except:
pass pass
return None return None
@ -87,7 +87,8 @@ async def query_data(url):
async def load_event_cn(): async def load_event_cn():
result = await query_data(url=list_api) result = await query_data(url=list_api)
detail_result = await query_data(url=detail_api) detail_result = await query_data(url=detail_api)
if result and 'retcode' in result and result['retcode'] == 0 and detail_result and 'retcode' in detail_result and detail_result['retcode'] == 0: if result and 'retcode' in result and result['retcode'] == 0 and detail_result and 'retcode' in detail_result and \
detail_result['retcode'] == 0:
event_data['cn'] = [] event_data['cn'] = []
event_detail = {} event_detail = {}
for detail in detail_result['data']['list']: for detail in detail_result['data']['list']:
@ -127,7 +128,8 @@ async def load_event_cn():
datelist = searchObj.groups() # ('2021', '9', '17') datelist = searchObj.groups() # ('2021', '9', '17')
if datelist and len(datelist) >= 6: if datelist and len(datelist) >= 6:
ctime = datetime.strptime( ctime = datetime.strptime(
f'{datelist[0]}-{datelist[1]}-{datelist[2]} {datelist[3]}:{datelist[4]}:{datelist[5]}', r"%Y-%m-%d %H:%M:%S") f'{datelist[0]}-{datelist[1]}-{datelist[2]} {datelist[3]}:{datelist[4]}:{datelist[5]}',
r"%Y-%m-%d %H:%M:%S")
if start_time < ctime < end_time: if start_time < ctime < end_time:
start_time = ctime start_time = ctime
except Exception as e: except Exception as e:
@ -170,7 +172,7 @@ async def load_event_cn():
'forever': False, 'forever': False,
'type': 3 'type': 3
}) })
i = i+1 i = i + 1
return 0 return 0
return 1 return 1
@ -222,7 +224,7 @@ async def get_events(server, offset, days):
events.append(event) events.append(event)
# 按type从大到小 按剩余天数从小到大 # 按type从大到小 按剩余天数从小到大
events.sort(key=lambda item: item["type"] events.sort(key=lambda item: item["type"]
* 100 - item['left_days'], reverse=True) * 100 - item['left_days'], reverse=True)
return events return events
@ -230,5 +232,6 @@ if __name__ == '__main__':
async def main(): async def main():
await load_event_cn() await load_event_cn()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(main()) loop.run_until_complete(main())