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