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