fix: 修复KG源API

This commit is contained in:
helloplhm-qwq 2023-11-19 09:14:07 +08:00 committed by “Folltoshe”
parent 33a7e8e8b6
commit e28474c490
5 changed files with 21 additions and 80 deletions

View File

@ -49,6 +49,8 @@ sourceExpirationTime = {
async def SongURL(source, songId, quality): async def SongURL(source, songId, quality):
if (source == "kg"):
songId = songId.lower()
try: try:
c = config.getCache('urls', f'{source}_{songId}_{quality}') c = config.getCache('urls', f'{source}_{songId}_{quality}')
if c: if c:

View File

@ -73,9 +73,10 @@ async def url(songId, quality):
if (not thash): if (not thash):
raise FailedException('获取歌曲信息失败') raise FailedException('获取歌曲信息失败')
if (not albumid): if (not albumid):
albumid = 0 albumid = ""
if (not albumaudioid): if (not albumaudioid):
albumaudioid = 0 albumaudioid = ""
thash = thash.lower()
params = { params = {
'album_id': albumid, 'album_id': albumid,
'userid': tools.userid, 'userid': tools.userid,
@ -103,6 +104,8 @@ async def url(songId, quality):
if (tools.version == 'v5'): if (tools.version == 'v5'):
params['quality'] = tools.qualityMap[quality] params['quality'] = tools.qualityMap[quality]
# print(params.quality) # print(params.quality)
if (tools.version == "v4"):
params['version'] = tools.clientver
headers = jsobject({ headers = jsobject({
'User-Agent': 'Android712-AndroidPhone-8983-18-0-NetMusic-wifi', 'User-Agent': 'Android712-AndroidPhone-8983-18-0-NetMusic-wifi',
'KG-THash': '3e5ec6b', 'KG-THash': '3e5ec6b',

View File

@ -34,6 +34,14 @@ tools = jsobject({
"e": '.flac', "e": '.flac',
'h': 'RS01', 'h': 'RS01',
}, },
"dolby": {
"e": ".flac",
"h": "Q000",
},
"master": {
"e": ".flac",
"h": "AI00",
}
}, },
"key": config.read_config("module.tx.user.qqmusic_key"), "key": config.read_config("module.tx.user.qqmusic_key"),
"loginuin": config.read_config("module.tx.user.uin"), "loginuin": config.read_config("module.tx.user.uin"),

View File

@ -15,11 +15,11 @@ import random
import traceback import traceback
import zlib import zlib
import ujson as json import ujson as json
from .log import log
import re import re
import binascii import binascii
import time import time
import pickle import pickle
from . import log
from . import config from . import config
from . import utils from . import utils
from . import variable from . import variable
@ -47,7 +47,7 @@ def convert_dict_to_form_string(dic):
ua_list = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.39||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1788.0||Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1788.0 uacq||Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5666.197 Safari/537.36||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 uacq||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'.split('||') ua_list = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.39||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1788.0||Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1788.0 uacq||Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5666.197 Safari/537.36||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 uacq||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'.split('||')
# 日志记录器 # 日志记录器
logger = log('http_utils') logger = log.log('http_utils')
def request(url, options = {}): def request(url, options = {}):
''' '''
@ -115,10 +115,11 @@ def request(url, options = {}):
logger.info("-----start----- " + url) 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: {log.highlight_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}')
logger.debug(req.text)
# 缓存写入 # 缓存写入
if (cache_info and cache_info != "no-cache"): if (cache_info and cache_info != "no-cache"):
cache_data = pickle.dumps(req) cache_data = pickle.dumps(req)
@ -128,79 +129,6 @@ def request(url, options = {}):
# 返回请求 # 返回请求
return req return req
"""
async def asyncrequest(url, options={}):
'''
- Asynchronous HTTP request function used for sending network requests
- url: URL address to be requested (required)
- options: Configuration parameters for the request (optional, defaults to GET request)
- method: Request method
- headers: Request headers
- body: Request body (can also use the native 'data' parameter of the 'requests' library)
- form: Submitted form data
@ return: aiohttp.ClientResponse type response data
'''
# Get the request method, defaulting to GET if not provided
try:
method = options['method']
options.pop('method')
except KeyError:
method = 'GET'
# Get the User-Agent, choose randomly from ua_list if not present
try:
d_lower = {k.lower(): v for k, v in options['headers'].items()}
useragent = d_lower['user-agent']
except KeyError:
try:
options['headers']['User-Agent'] = random.choice(ua_list)
except:
options['headers'] = {}
options['headers']['User-Agent'] = random.choice(ua_list)
# Get the request function
try:
reqattr = getattr(aiohttp.ClientSession(), method.lower())
except AttributeError:
raise AttributeError('Unsupported method: ' + method)
# Log before the request
logger.debug(f'HTTP Request: {url}\noptions: {options}')
# Convert body/form parameter to native 'data' parameter and add 'Content-Type' header for form requests
if method in ['POST', 'PUT']:
if options.get('body'):
options['data'] = options['body']
options.pop('body')
if options.get('form'):
options['data'] = convert_dict_to_form_string(options['form'])
options.pop('form')
options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
# Send the request
try:
async with reqattr(url, **options) as req:
res = await req.read()
except Exception as e:
logger.error(f'HTTP Request runs into an Error: {traceback.format_exc()}')
raise e
# Log after the request
logger.debug(f'Request to {url} succeed with code {req.status}')
# Log the response data
try:
logger.debug(json.loads(res.decode("utf-8")))
except:
try:
logger.debug(json.loads(zlib.decompress(res).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(res))
except:
logger.debug(
zlib.decompress(res).decode("utf-8") if is_valid_utf8(zlib.decompress(res).decode("utf-8")) and is_plain_text(
zlib.decompress(res).decode("utf-8")) else binascii.hexlify(zlib.decompress(res)))
# Return the response
return req
"""
def checkcn(): def checkcn():
req = request("https://mips.kugou.com/check/iscn?&format=json") req = request("https://mips.kugou.com/check/iscn?&format=json")

View File

@ -11,8 +11,8 @@
import os import os
debug_mode = False debug_mode = True
log_length_limit = 400 log_length_limit = 100000
running = True running = True
config = {} config = {}
workdir = os.getcwd() workdir = os.getcwd()