mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-05-23 19:17:41 +08:00
feat: 请求库缓存
This commit is contained in:
parent
4e004ff33f
commit
1ef6675a5f
@ -64,7 +64,8 @@ def getKey(hash_):
|
|||||||
return utils.md5(hash_.lower() + tools.pidversec + tools.appid + tools.mid + tools.userid)
|
return utils.md5(hash_.lower() + tools.pidversec + tools.appid + tools.mid + tools.userid)
|
||||||
|
|
||||||
async def url(songId, quality):
|
async def url(songId, quality):
|
||||||
inforeq = Httpx.request("https://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=" + songId)
|
songId = songId.lower()
|
||||||
|
inforeq = Httpx.request("https://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=" + songId, {"cache": 86400 * 15})
|
||||||
body_ = jsobject(inforeq.json())
|
body_ = jsobject(inforeq.json())
|
||||||
thash = body_.extra[tools.qualityHashMap[quality]]
|
thash = body_.extra[tools.qualityHashMap[quality]]
|
||||||
albumid = body_.albumid
|
albumid = body_.albumid
|
||||||
@ -116,7 +117,7 @@ async def url(songId, quality):
|
|||||||
if body.status == 3:
|
if body.status == 3:
|
||||||
raise FailedException('该歌曲在酷狗没有版权,请换源播放')
|
raise FailedException('该歌曲在酷狗没有版权,请换源播放')
|
||||||
elif body.status == 2:
|
elif body.status == 2:
|
||||||
raise FailedException('链接获取失败,请检查账号信息是否过期或本歌曲为数字专辑')
|
raise FailedException('链接获取失败,请检查账号是否有会员或数字专辑是否购买')
|
||||||
elif body.status != 1:
|
elif body.status != 1:
|
||||||
raise FailedException('链接获取失败,可能是数字专辑或者api失效')
|
raise FailedException('链接获取失败,可能是数字专辑或者api失效')
|
||||||
return body.url[0]
|
return body.url[0]
|
||||||
|
@ -43,14 +43,15 @@ tools = jsobject({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def signRequest(data):
|
def signRequest(data, cache = False):
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
s = sign(data)
|
s = sign(data)
|
||||||
headers = {}
|
headers = {}
|
||||||
return Httpx.request('https://u.y.qq.com/cgi-bin/musics.fcg?format=json&sign=' + s, {
|
return Httpx.request('https://u.y.qq.com/cgi-bin/musics.fcg?format=json&sign=' + s, {
|
||||||
'method': 'POST',
|
'method': 'POST',
|
||||||
'body': data,
|
'body': data,
|
||||||
'headers': headers
|
'headers': headers,
|
||||||
|
"cache": (86400 * 30) if cache else "no-cache"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ async def url(songId, quality):
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
infoRequest = signRequest(infoReqBody)
|
infoRequest = signRequest(infoReqBody, True)
|
||||||
infoBody = jsobject(infoRequest.json())
|
infoBody = jsobject(infoRequest.json())
|
||||||
if (infoBody.code != 0 or infoBody.req.code != 0):
|
if (infoBody.code != 0 or infoBody.req.code != 0):
|
||||||
raise FailedException("获取音乐信息失败")
|
raise FailedException("获取音乐信息失败")
|
||||||
|
@ -18,6 +18,10 @@ import ujson as json
|
|||||||
from .log import log
|
from .log import log
|
||||||
import re
|
import re
|
||||||
import binascii
|
import binascii
|
||||||
|
import time
|
||||||
|
import pickle
|
||||||
|
from . import config
|
||||||
|
from . import utils
|
||||||
|
|
||||||
def is_valid_utf8(text):
|
def is_valid_utf8(text):
|
||||||
# 判断是否为有效的utf-8字符串
|
# 判断是否为有效的utf-8字符串
|
||||||
@ -56,6 +60,20 @@ def request(url, options = {}):
|
|||||||
|
|
||||||
@ return: requests.Response类型的响应数据
|
@ return: requests.Response类型的响应数据
|
||||||
'''
|
'''
|
||||||
|
# 缓存读取
|
||||||
|
cache_key = utils.md5(f'{url}{options}')
|
||||||
|
if options.get("cache") and options["cache"] != "no-cache":
|
||||||
|
cache = config.getCache("httpx", cache_key)
|
||||||
|
if cache:
|
||||||
|
logger.debug(f"请求 {url} 有可用缓存")
|
||||||
|
return pickle.loads(utils.from_base64(cache["data"]))
|
||||||
|
if "cache" in list(options.keys()):
|
||||||
|
cache_info = options.get("cache")
|
||||||
|
options.pop("cache")
|
||||||
|
else:
|
||||||
|
cache_info = None
|
||||||
|
|
||||||
|
|
||||||
# 获取请求方法,没有则默认为GET请求
|
# 获取请求方法,没有则默认为GET请求
|
||||||
try:
|
try:
|
||||||
method = options['method']
|
method = options['method']
|
||||||
@ -90,25 +108,19 @@ def request(url, options = {}):
|
|||||||
options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
|
options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||||
# 进行请求
|
# 进行请求
|
||||||
try:
|
try:
|
||||||
|
logger.info("-----start----- " + url)
|
||||||
req = reqattr(url, **options)
|
req = reqattr(url, **options)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'HTTP Request runs into an Error: {traceback.format_exc()}')
|
logger.error(f'HTTP Request runs into an Error: {traceback.format_exc()}')
|
||||||
raise e
|
raise e
|
||||||
# 请求后记录
|
# 请求后记录
|
||||||
logger.debug(f'Request to {url} succeed with code {req.status_code}')
|
logger.debug(f'Request to {url} succeed with code {req.status_code}')
|
||||||
# 记录响应数据
|
# 缓存写入
|
||||||
try:
|
if (cache_info and cache_info != "no-cache"):
|
||||||
logger.debug(json.loads(req.content.decode("utf-8")))
|
cache_data = pickle.dumps(req)
|
||||||
except:
|
expire_time = (cache_info if isinstance(cache_info, int) else 3600) + int(time.time())
|
||||||
try:
|
config.updateCache("httpx", cache_key, {"expire": True, "time": expire_time, "data": utils.to_base64(cache_data)})
|
||||||
logger.debug(json.loads(zlib.decompress(req.content).decode("utf-8")))
|
logger.debug("缓存已更新: " + url)
|
||||||
except zlib.error:
|
|
||||||
if is_valid_utf8(req.text) and is_plain_text(req.text):
|
|
||||||
logger.debug(req.text)
|
|
||||||
else:
|
|
||||||
logger.debug(binascii.hexlify(req.content))
|
|
||||||
except:
|
|
||||||
logger.debug(zlib.decompress(req.content).decode("utf-8") if is_valid_utf8(zlib.decompress(req.content).decode("utf-8")) and is_plain_text(zlib.decompress(req.content).decode("utf-8")) else binascii.hexlify(zlib.decompress(req.content)))
|
|
||||||
# 返回请求
|
# 返回请求
|
||||||
return req
|
return req
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user