diff --git a/apis/kg/__init__.py b/apis/kg/__init__.py index 7b3944a..04d6ed6 100644 --- a/apis/kg/__init__.py +++ b/apis/kg/__init__.py @@ -64,7 +64,8 @@ def getKey(hash_): return utils.md5(hash_.lower() + tools.pidversec + tools.appid + tools.mid + tools.userid) 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()) thash = body_.extra[tools.qualityHashMap[quality]] albumid = body_.albumid @@ -116,7 +117,7 @@ async def url(songId, quality): if body.status == 3: raise FailedException('该歌曲在酷狗没有版权,请换源播放') elif body.status == 2: - raise FailedException('链接获取失败,请检查账号信息是否过期或本歌曲为数字专辑') + raise FailedException('链接获取失败,请检查账号是否有会员或数字专辑是否购买') elif body.status != 1: raise FailedException('链接获取失败,可能是数字专辑或者api失效') return body.url[0] diff --git a/apis/tx/__init__.py b/apis/tx/__init__.py index 224b182..18d2d46 100644 --- a/apis/tx/__init__.py +++ b/apis/tx/__init__.py @@ -43,14 +43,15 @@ tools = jsobject({ }) -def signRequest(data): +def signRequest(data, cache = False): data = json.dumps(data) s = sign(data) headers = {} return Httpx.request('https://u.y.qq.com/cgi-bin/musics.fcg?format=json&sign=' + s, { 'method': 'POST', '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()) if (infoBody.code != 0 or infoBody.req.code != 0): raise FailedException("获取音乐信息失败") diff --git a/common/Httpx.py b/common/Httpx.py index 3a56104..7b82f00 100644 --- a/common/Httpx.py +++ b/common/Httpx.py @@ -18,6 +18,10 @@ import ujson as json from .log import log import re import binascii +import time +import pickle +from . import config +from . import utils def is_valid_utf8(text): # 判断是否为有效的utf-8字符串 @@ -56,6 +60,20 @@ def request(url, options = {}): @ 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请求 try: method = options['method'] @@ -90,25 +108,19 @@ def request(url, options = {}): options['headers']['Content-Type'] = 'application/x-www-form-urlencoded' # 进行请求 try: + logger.info("-----start----- " + url) req = reqattr(url, **options) except Exception as e: logger.error(f'HTTP Request runs into an Error: {traceback.format_exc()}') raise e # 请求后记录 logger.debug(f'Request to {url} succeed with code {req.status_code}') - # 记录响应数据 - try: - logger.debug(json.loads(req.content.decode("utf-8"))) - except: - try: - logger.debug(json.loads(zlib.decompress(req.content).decode("utf-8"))) - 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))) + # 缓存写入 + if (cache_info and cache_info != "no-cache"): + cache_data = pickle.dumps(req) + expire_time = (cache_info if isinstance(cache_info, int) else 3600) + int(time.time()) + config.updateCache("httpx", cache_key, {"expire": True, "time": expire_time, "data": utils.to_base64(cache_data)}) + logger.debug("缓存已更新: " + url) # 返回请求 return req