mirror of
https://github.com/xhongc/music-tag-web.git
synced 2026-04-25 03:00:54 +08:00
feature:新增酷我音乐源
This commit is contained in:
77
applications/task/services/kuwo.py
Normal file
77
applications/task/services/kuwo.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import random
|
||||
import requests
|
||||
import hashlib
|
||||
from typing import Optional
|
||||
import datetime
|
||||
|
||||
default_headers = {
|
||||
'User-Agent': "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",
|
||||
'Referer': 'http://www.kuwo.cn/'
|
||||
}
|
||||
|
||||
|
||||
def generate_kw_token(length=32):
|
||||
charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
return ''.join(random.choices(charset, k=length))
|
||||
|
||||
|
||||
class KuwoClient:
|
||||
def __init__(self):
|
||||
self.token = generate_kw_token()
|
||||
self.cross = hashlib.md5(self.token.encode('utf-8')).hexdigest()
|
||||
|
||||
def _api_request(self, url, params):
|
||||
headers = default_headers.copy()
|
||||
headers['Cross'] = self.cross
|
||||
headers['Cookie'] = f'Hm_token={self.token}'
|
||||
return requests.get(url, params=params, headers=headers, timeout=5.0).json()
|
||||
|
||||
def fetch_lyric(self, song_id):
|
||||
url = f'http://kuwo.cn/newh5/singles/songinfoandlrc?musicId={song_id}'
|
||||
params = {
|
||||
'mid': song_id,
|
||||
'type': 'music',
|
||||
'httpsStatus': 1,
|
||||
'plat': 'web_www'
|
||||
}
|
||||
resp = self._api_request(url, params)
|
||||
lrclist = resp.get("data", {}).get("lrclist", [])
|
||||
lyric = ""
|
||||
try:
|
||||
for line in lrclist:
|
||||
seconds = int(float(line.get("time", "0")))
|
||||
m, s = divmod(seconds, 60)
|
||||
h, m = divmod(m, 60)
|
||||
time_format = "%d:%02d:%02d" % (h, m, s)
|
||||
content = line.get("lineLyric", "")
|
||||
lyric += f"[{time_format}]{content}\n"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return lyric
|
||||
|
||||
def fetch_id3_by_title(self, title):
|
||||
url = 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord'
|
||||
params = {
|
||||
'key': title,
|
||||
'pn': 1,
|
||||
'rn': 10,
|
||||
'httpsStatus': 1
|
||||
}
|
||||
resp = self._api_request(url, params)
|
||||
songs = resp.get('data', {}).get('list', None)
|
||||
for song in songs:
|
||||
song["id"] = song['rid']
|
||||
song["name"] = song['name']
|
||||
song["artist"] = song['artist']
|
||||
song["artist_id"] = song['artistid']
|
||||
song["album"] = song['album']
|
||||
song["album_id"] = song['albumid']
|
||||
song["album_img"] = song['albumpic']
|
||||
song["year"] = ""
|
||||
return songs
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
kw = KuwoClient()
|
||||
# print(kw.fetch_id3_by_title("我想"))
|
||||
print(kw.fetch_lyric("22822909"))
|
||||
@@ -2,6 +2,7 @@ import requests
|
||||
import base64
|
||||
|
||||
from applications.task.services.kugou import KugouClient
|
||||
from applications.task.services.kuwo import KuwoClient
|
||||
from applications.task.services.qm import QQMusicApi
|
||||
from applications.task.utils import timestamp_to_dt
|
||||
from applications.utils.send import send
|
||||
@@ -20,6 +21,8 @@ class MusicResource:
|
||||
return QmusicClient()
|
||||
elif info == "kugou":
|
||||
return KugouClient()
|
||||
elif info == "kuwo":
|
||||
return KuwoClient()
|
||||
raise Exception("暂不支持该音乐平台")
|
||||
|
||||
def fetch_lyric(self, song_id):
|
||||
|
||||
@@ -345,7 +345,8 @@
|
||||
{id: 'netease', name: '网易云音乐'},
|
||||
{id: 'migu', name: '咪咕音乐'},
|
||||
{id: 'qmusic', name: 'QQ音乐'},
|
||||
{id: 'kugou', name: '酷狗音乐'}
|
||||
{id: 'kugou', name: '酷狗音乐'},
|
||||
{id: 'kuwo', name: '酷我音乐'}
|
||||
],
|
||||
baseMusicInfo: {
|
||||
'genre': '流行',
|
||||
|
||||
Reference in New Issue
Block a user