]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/qqmusic.py
[qqmusic]update valid url
[yt-dlp.git] / youtube_dl / extractor / qqmusic.py
CommitLineData
5d98908b
YCH
1# coding: utf-8
2from __future__ import unicode_literals
3
a2043572 4import random
8afff9f8 5import re
19ada898 6import time
a2043572 7
5d98908b 8from .common import InfoExtractor
5edea45f 9from ..utils import (
5c2266df 10 sanitized_Request,
5edea45f
YCH
11 strip_jsonp,
12 unescapeHTML,
0d0d5d37 13 clean_html,
2fffb1dc 14 ExtractorError,
5edea45f 15)
5d98908b 16
5d98908b
YCH
17
18class QQMusicIE(InfoExtractor):
7ec676bb 19 IE_NAME = 'qqmusic'
a7ada46b 20 IE_DESC = 'QQ音乐'
19ada898 21 _VALID_URL = r'https?://y\.qq\.com/n/yqq/song/(?P<id>[0-9A-Za-z]+)\.html'
5d98908b 22 _TESTS = [{
19ada898 23 'url': 'https://y.qq.com/n/yqq/song/004295Et37taLD.html',
55e5841f 24 'md5': '9ce1c1c8445f561506d2e3cfb0255705',
5d98908b
YCH
25 'info_dict': {
26 'id': '004295Et37taLD',
55e5841f 27 'ext': 'mp3',
5d98908b 28 'title': '可惜没如果',
a5d09d68 29 'release_date': '20141227',
5d98908b 30 'creator': '林俊杰',
b813d8ca 31 'description': 'md5:d327722d0361576fde558f1ac68a7065',
ec85ded8 32 'thumbnail': r're:^https?://.*\.jpg$',
5e3915cb 33 }
34 }, {
35 'note': 'There is no mp3-320 version of this song.',
19ada898 36 'url': 'https://y.qq.com/n/yqq/song/004MsGEo3DdNxV.html',
5e3915cb 37 'md5': 'fa3926f0c585cda0af8fa4f796482e3e',
38 'info_dict': {
39 'id': '004MsGEo3DdNxV',
40 'ext': 'mp3',
41 'title': '如果',
a5d09d68 42 'release_date': '20050626',
5e3915cb 43 'creator': '李季美',
44 'description': 'md5:46857d5ed62bc4ba84607a805dccf437',
ec85ded8 45 'thumbnail': r're:^https?://.*\.jpg$',
5d98908b 46 }
cd1bb549
S
47 }, {
48 'note': 'lyrics not in .lrc format',
19ada898 49 'url': 'https://y.qq.com/n/yqq/song/001JyApY11tIp6.html',
cd1bb549
S
50 'info_dict': {
51 'id': '001JyApY11tIp6',
52 'ext': 'mp3',
53 'title': 'Shadows Over Transylvania',
a5d09d68 54 'release_date': '19970225',
cd1bb549
S
55 'creator': 'Dark Funeral',
56 'description': 'md5:ed14d5bd7ecec19609108052c25b2c11',
ec85ded8 57 'thumbnail': r're:^https?://.*\.jpg$',
cd1bb549
S
58 },
59 'params': {
60 'skip_download': True,
61 },
5d98908b
YCH
62 }]
63
55e5841f 64 _FORMATS = {
8b8cde21 65 'mp3-320': {'prefix': 'M800', 'ext': 'mp3', 'preference': 40, 'abr': 320},
66 'mp3-128': {'prefix': 'M500', 'ext': 'mp3', 'preference': 30, 'abr': 128},
55e5841f 67 'm4a': {'prefix': 'C200', 'ext': 'm4a', 'preference': 10}
68 }
69
a2043572
YCH
70 # Reference: m_r_GetRUin() in top_player.js
71 # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
72 @staticmethod
73 def m_r_get_ruin():
74 curMs = int(time.time() * 1000) % 1000
75 return int(round(random.random() * 2147483647) * curMs % 1E10)
76
5d98908b
YCH
77 def _real_extract(self, url):
78 mid = self._match_id(url)
79
80 detail_info_page = self._download_webpage(
81 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
8afff9f8 82 mid, note='Download song detail info',
c9a77969 83 errnote='Unable to get song detail info', encoding='gbk')
5d98908b
YCH
84
85 song_name = self._html_search_regex(
86 r"songname:\s*'([^']+)'", detail_info_page, 'song name')
87
88 publish_time = self._html_search_regex(
89 r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
a685ae51
YCH
90 'publish time', default=None)
91 if publish_time:
92 publish_time = publish_time.replace('-', '')
5d98908b
YCH
93
94 singer = self._html_search_regex(
a685ae51
YCH
95 r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None)
96
97 lrc_content = self._html_search_regex(
98 r'<div class="content" id="lrc_content"[^<>]*>([^<>]+)</div>',
99 detail_info_page, 'LRC lyrics', default=None)
b813d8ca
YCH
100 if lrc_content:
101 lrc_content = lrc_content.replace('\\n', '\n')
5d98908b 102
5e3915cb 103 thumbnail_url = None
104 albummid = self._search_regex(
0392ac98 105 [r'albummid:\'([0-9a-zA-Z]+)\'', r'"albummid":"([0-9a-zA-Z]+)"'],
106 detail_info_page, 'album mid', default=None)
5e3915cb 107 if albummid:
108 thumbnail_url = "http://i.gtimg.cn/music/photo/mid_album_500/%s/%s/%s.jpg" \
109 % (albummid[-2:-1], albummid[-1], albummid)
110
a2043572
YCH
111 guid = self.m_r_get_ruin()
112
5d98908b
YCH
113 vkey = self._download_json(
114 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
115 mid, note='Retrieve vkey', errnote='Unable to get vkey',
116 transform_source=strip_jsonp)['key']
55e5841f 117
118 formats = []
e8ac61e8 119 for format_id, details in self._FORMATS.items():
55e5841f 120 formats.append({
121 'url': 'http://cc.stream.qqmusic.qq.com/%s%s.%s?vkey=%s&guid=%s&fromtag=0'
e8ac61e8
YCH
122 % (details['prefix'], mid, details['ext'], vkey, guid),
123 'format': format_id,
124 'format_id': format_id,
125 'preference': details['preference'],
126 'abr': details.get('abr'),
55e5841f 127 })
4d58b24c 128 self._check_formats(formats, mid)
55e5841f 129 self._sort_formats(formats)
5d98908b 130
961c5cbf
S
131 actual_lrc_lyrics = ''.join(
132 line + '\n' for line in re.findall(
95c5e101 133 r'(?m)^(\[[0-9]{2}:[0-9]{2}(?:\.[0-9]{2,})?\][^\n]*|\[[^\]]*\])', lrc_content))
b65e5bb7
QF
134
135 info_dict = {
5d98908b 136 'id': mid,
55e5841f 137 'formats': formats,
5d98908b 138 'title': song_name,
a5d09d68 139 'release_date': publish_time,
5d98908b 140 'creator': singer,
a685ae51 141 'description': lrc_content,
b65e5bb7 142 'thumbnail': thumbnail_url
5d98908b 143 }
b65e5bb7
QF
144 if actual_lrc_lyrics:
145 info_dict['subtitles'] = {
146 'origin': [{
147 'ext': 'lrc',
148 'data': actual_lrc_lyrics,
149 }]
150 }
151 return info_dict
8afff9f8
YCH
152
153
5edea45f
YCH
154class QQPlaylistBaseIE(InfoExtractor):
155 @staticmethod
156 def qq_static_url(category, mid):
157 return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid)
158
5edea45f
YCH
159 @classmethod
160 def get_entries_from_page(cls, page):
161 entries = []
162
163 for item in re.findall(r'class="data"[^<>]*>([^<>]+)</', page):
164 song_mid = unescapeHTML(item).split('|')[-5]
165 entries.append(cls.url_result(
19ada898 166 # https://y.qq.com/n/yqq/song/004Dbsoo1yCbNZ.html
167 'https://y.qq.com/n/yqq/song/' + song_mid + ".html", 'QQMusic',
a685ae51 168 song_mid))
5edea45f
YCH
169
170 return entries
171
172
173class QQMusicSingerIE(QQPlaylistBaseIE):
7ec676bb 174 IE_NAME = 'qqmusic:singer'
181c4cca 175 IE_DESC = 'QQ音乐 - 歌手'
19ada898 176 _VALID_URL = r'https?://y\.qq\.com/n/yqq/singer/(?P<id>[0-9A-Za-z]+)\.html'
8afff9f8 177 _TEST = {
19ada898 178 'url': 'https://y.qq.com/n/yqq/singer/001BLpXF2DyJe2.html',
8afff9f8
YCH
179 'info_dict': {
180 'id': '001BLpXF2DyJe2',
181 'title': '林俊杰',
141a273a 182 'description': 'md5:870ec08f7d8547c29c93010899103751',
8afff9f8
YCH
183 },
184 'playlist_count': 12,
185 }
186
187 def _real_extract(self, url):
188 mid = self._match_id(url)
189
190 singer_page = self._download_webpage(
5edea45f 191 self.qq_static_url('singer', mid), mid, 'Download singer page')
8afff9f8 192
5edea45f 193 entries = self.get_entries_from_page(singer_page)
8afff9f8
YCH
194
195 singer_name = self._html_search_regex(
196 r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
197 default=None)
198
199 singer_id = self._html_search_regex(
200 r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
201 default=None)
202
203 singer_desc = None
204
205 if singer_id:
5c2266df 206 req = sanitized_Request(
8afff9f8
YCH
207 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
208 req.add_header(
209 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
210 singer_desc_page = self._download_xml(
5edea45f 211 req, mid, 'Donwload singer description XML')
8afff9f8
YCH
212
213 singer_desc = singer_desc_page.find('./data/info/desc').text
214
215 return self.playlist_result(entries, mid, singer_name, singer_desc)
5edea45f
YCH
216
217
218class QQMusicAlbumIE(QQPlaylistBaseIE):
7ec676bb 219 IE_NAME = 'qqmusic:album'
181c4cca 220 IE_DESC = 'QQ音乐 - 专辑'
19ada898 221 _VALID_URL = r'https?://y\.qq\.com/n/yqq/album/(?P<id>[0-9A-Za-z]+)\.html'
5edea45f 222
29b809de 223 _TESTS = [{
19ada898 224 'url': 'https://y.qq.com/n/yqq/album/000gXCTb2AhRR1.html',
5edea45f
YCH
225 'info_dict': {
226 'id': '000gXCTb2AhRR1',
227 'title': '我们都是这样长大的',
fc7ae675 228 'description': 'md5:179c5dce203a5931970d306aa9607ea6',
5edea45f
YCH
229 },
230 'playlist_count': 4,
29b809de 231 }, {
19ada898 232 'url': 'https://y.qq.com/n/yqq/album/002Y5a3b3AlCu3.html',
29b809de 233 'info_dict': {
234 'id': '002Y5a3b3AlCu3',
235 'title': '그리고...',
fc7ae675 236 'description': 'md5:a48823755615508a95080e81b51ba729',
29b809de 237 },
238 'playlist_count': 8,
239 }]
5edea45f
YCH
240
241 def _real_extract(self, url):
242 mid = self._match_id(url)
243
29b809de 244 album = self._download_json(
245 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg?albummid=%s&format=json' % mid,
246 mid, 'Download album page')['data']
247
248 entries = [
249 self.url_result(
19ada898 250 'https://y.qq.com/n/yqq/song/' + song['songmid'] + ".html", 'QQMusic', song['songmid']
29b809de 251 ) for song in album['list']
252 ]
dfc4eca2 253 album_name = album.get('name')
29b809de 254 album_detail = album.get('desc')
fc7ae675
YCH
255 if album_detail is not None:
256 album_detail = album_detail.strip()
5edea45f
YCH
257
258 return self.playlist_result(entries, mid, album_name, album_detail)
41333b97 259
260
261class QQMusicToplistIE(QQPlaylistBaseIE):
7ec676bb 262 IE_NAME = 'qqmusic:toplist'
181c4cca 263 IE_DESC = 'QQ音乐 - 排行榜'
5d1bd3b9 264 _VALID_URL = r'https?://y\.qq\.com/n/yqq/toplist/(?P<id>[0-9]+)\.html'
54889739 265
41333b97 266 _TESTS = [{
19ada898 267 'url': 'https://y.qq.com/n/yqq/toplist/123.html',
41333b97 268 'info_dict': {
eedda32e 269 'id': 'global_123',
270 'title': '美国iTunes榜',
41333b97 271 },
272 'playlist_count': 10,
273 }, {
19ada898 274 'url': 'https://y.qq.com/n/yqq/toplist/3.html',
41333b97 275 'info_dict': {
eedda32e 276 'id': 'top_3',
141a273a 277 'title': '巅峰榜·欧美',
eedda32e 278 'description': 'QQ音乐巅峰榜·欧美根据用户收听行为自动生成,集结当下最流行的欧美新歌!:更新时间:每周四22点|统'
279 '计周期:一周(上周四至本周三)|统计对象:三个月内发行的欧美歌曲|统计数量:100首|统计算法:根据'
280 '歌曲在一周内的有效播放次数,由高到低取前100名(同一歌手最多允许5首歌曲同时上榜)|有效播放次数:'
281 '登录用户完整播放一首歌曲,记为一次有效播放;同一用户收听同一首歌曲,每天记录为1次有效播放'
41333b97 282 },
283 'playlist_count': 100,
fd4eefed 284 }, {
19ada898 285 'url': 'https://y.qq.com/n/yqq/toplist/106.html',
fd4eefed 286 'info_dict': {
eedda32e 287 'id': 'global_106',
288 'title': '韩国Mnet榜',
fd4eefed 289 },
290 'playlist_count': 50,
41333b97 291 }]
292
41333b97 293 def _real_extract(self, url):
294 list_id = self._match_id(url)
295
5d1bd3b9 296 # list_type, num_id = list_id.split("_")
297 list_type = "toplist"
298 num_id = list_id
41333b97 299
29ea5728 300 toplist_json = self._download_json(
eedda32e 301 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?type=%s&topid=%s&format=json'
302 % (list_type, num_id),
303 list_id, 'Download toplist page')
41333b97 304
eedda32e 305 entries = [
306 self.url_result(
19ada898 307 'https://y.qq.com/n/yqq/song/' + song['data']['songmid'] + ".html", 'QQMusic', song['data']['songmid']
eedda32e 308 ) for song in toplist_json['songlist']
309 ]
41333b97 310
9d4f213f
YCH
311 topinfo = toplist_json.get('topinfo', {})
312 list_name = topinfo.get('ListName')
313 list_description = topinfo.get('info')
eedda32e 314 return self.playlist_result(entries, list_id, list_name, list_description)
0d0d5d37 315
316
317class QQMusicPlaylistIE(QQPlaylistBaseIE):
318 IE_NAME = 'qqmusic:playlist'
181c4cca 319 IE_DESC = 'QQ音乐 - 歌单'
19ada898 320 _VALID_URL = r'https?://y\.qq\.com/n/yqq/playlist/(?P<id>[0-9]+)\.html'
0d0d5d37 321
2fffb1dc 322 _TESTS = [{
19ada898 323 'url': 'http://y.qq.com/n/yqq/playlist/3462654915.html',
0d0d5d37 324 'info_dict': {
325 'id': '3462654915',
326 'title': '韩国5月新歌精选下旬',
327 'description': 'md5:d2c9d758a96b9888cf4fe82f603121d4',
328 },
329 'playlist_count': 40,
2fffb1dc
YCH
330 'skip': 'playlist gone',
331 }, {
19ada898 332 'url': 'https://y.qq.com/n/yqq/playlist/1374105607.html',
2fffb1dc
YCH
333 'info_dict': {
334 'id': '1374105607',
335 'title': '易入人心的华语民谣',
336 'description': '民谣的歌曲易于传唱、、歌词朗朗伤口、旋律简单温馨。属于那种才入耳孔。却上心头的感觉。没有太多的复杂情绪。简单而直接地表达乐者的情绪,就是这样的简单才易入人心。',
337 },
338 'playlist_count': 20,
339 }]
0d0d5d37 340
341 def _real_extract(self, url):
342 list_id = self._match_id(url)
343
344 list_json = self._download_json(
345 'http://i.y.qq.com/qzone-music/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&disstid=%s'
346 % list_id, list_id, 'Download list page',
2fffb1dc
YCH
347 transform_source=strip_jsonp)
348 if not len(list_json.get('cdlist', [])):
349 if list_json.get('code'):
350 raise ExtractorError(
351 'QQ Music said: error %d in fetching playlist info' % list_json['code'],
352 expected=True)
353 raise ExtractorError('Unable to get playlist info')
354
355 cdlist = list_json['cdlist'][0]
0d0d5d37 356 entries = [
357 self.url_result(
19ada898 358 'https://y.qq.com/n/yqq/song/' + song['songmid'] + ".html", 'QQMusic', song['songmid']
2fffb1dc 359 ) for song in cdlist['songlist']
0d0d5d37 360 ]
361
2fffb1dc
YCH
362 list_name = cdlist.get('dissname')
363 list_description = clean_html(unescapeHTML(cdlist.get('desc')))
0d0d5d37 364 return self.playlist_result(entries, list_id, list_name, list_description)