def migu_search(keyword, allSong=None) -> list: """ 搜索音乐 """ number = config.get("number") or 5 params = {"keyword": keyword, "type": 2, "rows": number, "pgc": 1} songs_list = [] MiguApi.session.headers.update({ "referer": "http://music.migu.cn/", "User-Agent": config.get("ios_useragent") }) res_data = (MiguApi.request( "https://m.music.migu.cn/migu/remoting/scr_search_tag", method="GET", data=params, ).get("musics", [])) for item in res_data: song = MiguSong() song.source = "migu" song.id = item.get("mp3", "") song.title = item.get("title", "") song.singer = item.get("singerName", "") song.album = item.get("albumName", "") song.cover_url = item.get("cover", "") song.lyrics_url = item.get("lyrics", "") song._song_url = item.get("mp3", "") songs_list.append(song) if isinstance(allSong, queue.Queue): allSong.put(song.getObj()) return songs_list
def qq_search(keyword, allSong=None) -> list: """ 搜索音乐 """ number = config.get("number") or 5 params = {"w": keyword, "format": "json", "p": 1, "n": number} songs_list = [] QQApi.session.headers.update({ "referer": "http://m.y.qq.com", "User-Agent": config.get("ios_useragent") }) res_data = (QQApi.request( "http://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp", method="GET", data=params, ).get("data", {}).get("song", {}).get("list", [])) for item in res_data: # 获得歌手名字 if (item["msgid"] == 13): continue singers = [s.get("name", "") for s in item.get("singer", "")] song = QQSong() song.source = "qq" song.id = item.get("songmid", "") song.title = item.get("songname", "") song.singer = "、".join(singers) song.album = item.get("albumname", "") song.duration = int(item.get("interval", 0)) * 1000 song.size = round(item.get("size128", 0) / 1048576, 2) songs_list.append(song) if isinstance(allSong, queue.Queue): allSong.put(song.getObj()) return songs_list
def request(cls, url, method="POST", data=None): if method == "GET": resp = cls.session.get(url, params=data, timeout=config.get("timeout")) else: resp = cls.session.post(url, data=data, timeout=config.get("timeout")) if resp.status_code != requests.codes.ok: raise RequestError(resp.text) if not resp.text: raise ResponseError("No response data.") return resp.json()
def kuwo_search(keyword, allSong=None) -> list: """ 搜索音乐 """ keyword = quote(keyword) number = config.get("number") or 5 token = randint(1000000000, 9999999999) KuwoApi.session.headers.update({ "Referer": "http://kuwo.cn/search/list?key=" + keyword, "csrf": str(token), "Cookie": "kw_token=" + str(token), }) songs_list = [] res_data = (KuwoApi.request( "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=" + keyword + "&pn=" + str(1) + "&rn=" + str(number), method="GET").get("data", {}).get("list", [])) for item in res_data: if "%" in item.get("name", ""): continue song = KuwoSong() song.source = "kuwo" song.id = item.get("musicrid", "") song.title = item.get("name", "") song.singer = item.get("artist", "") song.album = item.get("album", "") songs_list.append(song) if isinstance(allSong, queue.Queue): allSong.put(song.getObj()) return songs_list
class MusicApi: # class property # 子类修改时使用deepcopy session = requests.Session() session.headers.update(config.get("fake_headers")) if config.get("proxies"): session.proxies.update(config.get("proxies")) session.headers.update({"referer": "http://www.google.com/"}) @classmethod def request(cls, url, method="POST", data=None): if method == "GET": resp = cls.session.get(url, params=data, timeout=config.get("timeout")) else: resp = cls.session.post(url, data=data, timeout=config.get("timeout")) if resp.status_code != requests.codes.ok: raise RequestError(resp.text) if not resp.text: raise ResponseError("No response data.") return resp.json()
def kugou_search(keyword,allSong=None) -> list: """ 搜索音乐 """ number = config.get("number") or 5 params = dict( keyword=keyword, platform="WebFilter", format="json", page=1, pagesize=number ) songs_list = [] res_data = ( KugouApi.request( "http://songsearch.kugou.com/song_search_v2", method="GET", data=params ) .get("data", {}) .get("lists", []) ) for item in res_data: " vip trans_param 下 musicpack_advance==1 " if item.get("trans_param",[]).get("musicpack_advance","")==1: continue song = KugouSong() song.source = "kugou" song.id = item.get("FileHash", "") song.title = item.get("SongName", "") song.singer = item.get("SingerName", "") song.duration = item.get("Duration", 0) song.album = item.get("AlbumName", "") song.size = round(item.get("FileSize", 0) / 1048576, 2) song.hash = item.get("FileHash", "") # 如果有更高品质的音乐选择高品质(尽管好像没什么卵用) keys_list = ["SQFileHash", "HQFileHash"] for key in keys_list: hash = item.get(key, "") if hash and hash != "00000000000000000000000000000000": song.id = hash break songs_list.append(song) if isinstance(allSong, queue.Queue): allSong.put(song.getObj()) return songs_list
class MiguApi(MusicApi): session = copy.deepcopy(MusicApi.session) session.headers.update({ "referer": "http://music.migu.cn/", "User-Agent": config.get("ios_useragent") })
class QQApi(MusicApi): session = copy.deepcopy(MusicApi.session) session.headers.update({ "referer": "https://y.qq.com/portal/player.html", "User-Agent": config.get("ios_useragent"), })
def getMp3Url(id) -> str: params = dict(type="convert_url", format="mp3", response="url", rid=id) return requests.get("http://antiserver.kuwo.cn/anti.s", headers=config.get("fake_headers"), params=params).text
class KugouApi(MusicApi): session = copy.deepcopy(MusicApi.session) session.headers.update( {"referer": "http://m.kugou.com", "User-Agent": config.get("ios_headers")} )