Skip to content

Commit

Permalink
完成QQ音乐API统一化
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHJK committed Jun 11, 2019
1 parent 2a42b03 commit ec3ed47
Showing 1 changed file with 45 additions and 53 deletions.
98 changes: 45 additions & 53 deletions music_dl/addons/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

import random
import base64
import requests
import click
import copy
from .. import config
from ..exceptions import RequestError, ResponseError, DataError
from ..api import MusicApi
from ..song import BasicSong
from ..utils import colorize


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"),
}
)


class QQSong(BasicSong):
Expand All @@ -34,19 +42,13 @@ def download_lyrics(self):
"platform": "yqq.json",
"needNewCode": "0",
}
s = requests.Session()
s.headers.update(config.get("fake_headers"))
if config.get("proxies"):
s.proxies.update(config.get("proxies"))
s.headers.update(
{
"referer": "https://y.qq.com/portal/player.html",
"User-Agent": config.get("ios_useragent"),
}

res_data = QQApi.request(
"https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg",
method="GET",
data=params,
)
r = s.get(url, params=params)
json_data = r.json()
lyric = json_data["lyric"]
lyric = res_data.get("lyric", "")
self.lyrics_text = base64.b64decode(lyric).decode("utf-8")
super(QQSong, self)._save_lyrics_text()

Expand All @@ -57,24 +59,14 @@ def download(self):
# 计算vkey
guid = str(random.randrange(1000000000, 10000000000))
params = {"guid": guid, "format": "json", "json": 3}
s = requests.Session()
s.headers.update(config.get("fake_headers"))
if config.get("proxies"):
s.proxies.update(config.get("proxies"))
s.headers.update(
{"referer": "http://y.qq.com", "User-Agent": config.get("ios_useragent")}
)

r = s.get(
"http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg", params=params
QQApi.session.headers.update({"referer": "http://y.qq.com"})
res_data = QQApi.request(
"http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg",
method="GET",
data=params,
)
if r.status_code != requests.codes.ok:
raise RequestError(r.text)
j = r.json()
if j["code"] != 0:
raise ResponseError(r.text)

vkey = j["key"]
vkey = res_data.get("key", "")

for prefix in ["M800", "M500", "C400"]:
url = (
Expand All @@ -92,35 +84,35 @@ def qq_search(keyword) -> list:
""" 搜索音乐 """
number = config.get("number") or 5
params = {"w": keyword, "format": "json", "p": 1, "n": number}
s = requests.Session()
s.headers.update(config.get("fake_headers"))
if config.get("proxies"):
s.proxies.update(config.get("proxies"))
s.headers.update(

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", [])
)

songs_list = []
r = s.get("http://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp", params=params)
if r.status_code != requests.codes.ok:
raise RequestError(r.text)
j = r.json()
if j["code"] != 0:
raise ResponseError(r.text)

for m in j["data"]["song"]["list"]:
for item in res_data:
# 获得歌手名字
singers = [s["name"] for s in m["singer"]]
singers = [s.get("name", "") for s in item.get("singer", "")]
song = QQSong()
song.source = "qq"
song.id = m["songid"]
song.title = m["songname"]
song.id = item.get("songid", "")
song.title = item.get("songname", "")
song.singer = "、".join(singers)
song.album = m["albumname"]
song.duration = m["interval"]
song.size = round(m["size128"] / 1048576, 2)
song.album = item.get("albumname", "")
song.duration = item.get("interval", 0)
song.size = round(item.get("size128", 0) / 1048576, 2)
# 特有字段
song.mid = m["songmid"]
song.mid = item.get("songmid", "")

songs_list.append(song)

Expand Down

0 comments on commit ec3ed47

Please sign in to comment.