feat: 优化返回数据过期时间显示

This commit is contained in:
helloplhm-qwq 2023-12-16 21:27:40 +08:00
parent c01d1ed9fb
commit 44b76b0e2d
No known key found for this signature in database
GPG Key ID: 6BE1B64B905567C7
2 changed files with 16 additions and 10 deletions

View File

@ -25,22 +25,21 @@ logger = log.log('api_handler')
sourceExpirationTime = {
'tx': {
"expire": True,
"time": 15 * 60 * 60, # 15 hours
"time": 80400, # 不知道tx为什么要取一个这么不对劲的数字当过期时长
},
'kg': {
"expire": True,
"time": 15 * 60 * 60, # 15 hours
"time": 24 * 60 * 60, # 24 hours
},
'kw': {
"expire": True,
"time": 30 * 60 # 30 minutes
"time": 60 * 60 # 60 minutes
},
'wy': {
"expire": True,
"time": 10 * 60, # 10 minutes
"time": 20 * 60, # 20 minutes
},
'mg': {
# no expiration
"expire": False,
"time": 0,
}
@ -69,6 +68,11 @@ async def url(source, songId, quality):
'quality': {
'target': quality,
'result': quality,
},
'expire': {
# 在更新缓存的时候把有效期的75%作为链接可用时长,现在加回来
'time': (cache['time'] + (sourceExpirationTime[source]['time'] * 0.25)) if cache['expire'] else None,
'canExpire': cache['expire'],
}
},
}
@ -90,7 +94,7 @@ async def url(source, songId, quality):
expireTime = sourceExpirationTime[source]['time'] + int(time.time())
config.updateCache('urls', f'{source}_{songId}_{quality}', {
"expire": canExpire,
"time": expireTime,
"time": int(expireTime - sourceExpirationTime[source]['time'] * 0.25), # 取有效期的75%作为链接可用时长
"url": result['url'],
})
logger.debug(f'缓存已更新:{source}_{songId}_{quality}, URL{result["url"]}, expire: {expireTime}')
@ -106,7 +110,7 @@ async def url(source, songId, quality):
'result': result['quality'],
},
'expire': {
'time': expireTime,
'time': expireTime if canExpire else None,
'canExpire': canExpire,
},
},

View File

@ -46,8 +46,10 @@ async def info(songid):
'size': utils.sizeFormat(int(req['track_info']['file']['size_new'][0])),
}
genres = []
for g in req['info']['genre']['content']:
genres.append(g['value'])
# fix: KeyError: 'genre'
if (req.get('info') and req['info'].get('genre') and req['info']['genre'].get('content')):
for g in req['info']['genre']['content']:
genres.append(g['value'])
return {
'name': req['track_info']['title'] + ' ' + req['track_info']['subtitle'].strip(),
'name_ori': req['track_info']['title'],
@ -66,7 +68,7 @@ async def info(songid):
'cover': f'https://y.qq.com/music/photo_new/T002R800x800M000{req["track_info"]["album"]["pmid"]}.jpg',
'sizable_cover': 'https://y.qq.com/music/photo_new/T002R{size}x{size}M000' + f'{req["track_info"]["album"]["pmid"]}.jpg',
'publish_date': req['track_info']['time_public'],
'mvid': req['track_info']['mv']['id'],
'mvid': req['track_info']['mv']['vid'],
'genre': genres,
'kmid': req['track_info']['ksong']['mid'],
'kid': req['track_info']['ksong']['id'],