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