From 4eb76f0579678ec6809fe44a207e691206fc609b Mon Sep 17 00:00:00 2001 From: charlesxie <408737515@qq.com> Date: Thu, 13 Jul 2023 12:03:37 +0800 Subject: [PATCH] =?UTF-8?q?feature=EF=BC=9A=E6=96=B0=E5=A2=9E=E9=85=B7?= =?UTF-8?q?=E6=88=91=E9=9F=B3=E4=B9=90=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/task/services/kuwo.py | 77 ++++++++++++++++++++ applications/task/services/music_resource.py | 3 + web/src/views/home/home.vue | 3 +- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 applications/task/services/kuwo.py diff --git a/applications/task/services/kuwo.py b/applications/task/services/kuwo.py new file mode 100644 index 0000000..47b3c97 --- /dev/null +++ b/applications/task/services/kuwo.py @@ -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")) diff --git a/applications/task/services/music_resource.py b/applications/task/services/music_resource.py index bbc25fd..41e559e 100644 --- a/applications/task/services/music_resource.py +++ b/applications/task/services/music_resource.py @@ -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): diff --git a/web/src/views/home/home.vue b/web/src/views/home/home.vue index d0e60ba..a5a90d4 100644 --- a/web/src/views/home/home.vue +++ b/web/src/views/home/home.vue @@ -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': '流行',