8 from .common
import InfoExtractor
9 from ..aes
import aes_ecb_encrypt
, pkcs7_padding
24 class NetEaseMusicBaseIE(InfoExtractor
):
25 # XXX: _extract_formats logic depends on the order of the levels in each tier
27 'standard', # free tier; 标准; 128kbps mp3 or aac
28 'higher', # free tier; 192kbps mp3 or aac
29 'exhigh', # free tier; 极高 (HQ); 320kbps mp3 or aac
30 'lossless', # VIP tier; 无损 (SQ); 48kHz/16bit flac
31 'hires', # VIP tier; 高解析度无损 (Hi-Res); 192kHz/24bit flac
32 'jyeffect', # VIP tier; 高清臻音 (Spatial Audio); 96kHz/24bit flac
33 'jymaster', # SVIP tier; 超清母带 (Master); 192kHz/24bit flac
34 'sky', # SVIP tier; 沉浸环绕声 (Surround Audio); flac
36 _API_BASE
= 'http://music.163.com/api/'
39 def _create_eapi_cipher(self
, api_path
, query_body
, cookies
):
40 request_text
= json
.dumps({**query_body
, 'header': cookies
}, separators
=(',', ':'))
42 message
= f
'nobody{api_path}use{request_text}md5forencrypt'.encode('latin1')
43 msg_digest
= hashlib
.md5(message
).hexdigest()
45 data
= pkcs7_padding(list(str.encode(
46 f
'{api_path}-36cd479b6b5-{request_text}-36cd479b6b5-{msg_digest}')))
47 encrypted
= bytes(aes_ecb_encrypt(data
, list(b
'e82ckenh8dichen8')))
48 return f
'params={encrypted.hex().upper()}'.encode()
50 def _download_eapi_json(self
, path
, video_id
, query_body
, headers
={}, **kwargs
):
53 'deviceId': 'undefined',
56 'mobilename': 'undefined',
57 'buildver': '1623435496',
58 'resolution': '1920x1080',
61 'channel': 'undefined',
62 'requestId': f
'{int(time.time() * 1000)}_{random.randint(0, 1000):04}',
63 **traverse_obj(self
._get
_cookies
(self
._API
_BASE
), {
64 'MUSIC_U': ('MUSIC_U', {lambda i
: i
.value
}),
67 return self
._download
_json
(
68 urljoin('https://interface3.music.163.com/', f
'/eapi{path}'), video_id
,
69 data
=self
._create
_eapi
_cipher
(f
'/api{path}', query_body
, cookies
), headers
={
70 'Referer': 'https://music.163.com',
71 'Cookie': '; '.join([f
'{k}={v}' for k
, v
in cookies
.items()]),
75 def _call_player_api(self
, song_id
, level
):
76 return self
._download
_eapi
_json
(
77 '/song/enhance/player/url/v1', song_id
,
78 {'ids': f
'[{song_id}]', 'level': level
, 'encodeType': 'flac'},
79 note
=f
'Downloading song URL info: level {level}')
81 def _extract_formats(self
, info
):
84 for level
in self
._LEVELS
:
86 self
._call
_player
_api
(song_id
, level
), ('data', lambda _
, v
: url_or_none(v
['url']), any
))
88 break # Media is not available due to removal or geo-restriction
89 actual_level
= song
.get('level')
90 if actual_level
and actual_level
!= level
:
91 if level
in ('lossless', 'jymaster'):
92 break # We've already extracted the highest level of the user's account tier
98 **traverse_obj(song
, {
99 'ext': ('type', {str}
),
100 'abr': ('br', {int_or_none(scale
=1000)}),
101 'filesize': ('size', {int_or_none}
),
105 break # Only 1 level is available if API does not return a value (netease:program)
107 self
.raise_geo_restricted(
108 'No media links found; possibly due to geo restriction', countries
=['CN'])
111 def _query_api(self
, endpoint
, video_id
, note
):
112 result
= self
._download
_json
(
113 f
'{self._API_BASE}{endpoint}', video_id
, note
, headers
={'Referer': self
._API
_BASE
})
114 code
= traverse_obj(result
, ('code', {int}
))
115 message
= traverse_obj(result
, ('message', {str}
)) or ''
117 self
.raise_login_required(f
'Login required to download: {message}')
119 raise ExtractorError(f
'Failed to get meta info: {code} {message}')
122 def _get_entries(self
, songs_data
, entry_keys
=None, id_key
='id', name_key
='name'):
123 for song
in traverse_obj(songs_data
, (
124 *variadic(entry_keys
, (str, bytes
, dict, set)),
125 lambda _
, v
: int_or_none(v
[id_key
]) is not None)):
126 song_id
= str(song
[id_key
])
127 yield self
.url_result(
128 f
'http://music.163.com/#/song?id={song_id}', NetEaseMusicIE
,
129 song_id
, traverse_obj(song
, (name_key
, {str}
)))
132 class NetEaseMusicIE(NetEaseMusicBaseIE
):
133 IE_NAME
= 'netease:song'
135 _VALID_URL
= r
'https?://(?:y\.)?music\.163\.com/(?:[#m]/)?song\?.*?\bid=(?P<id>[0-9]+)'
137 'url': 'https://music.163.com/#/song?id=550136151',
141 'title': 'It\'s Ok (Live)',
142 'creators': 'count:10',
143 'timestamp': 1522944000,
144 'upload_date': '20180405',
145 'description': 'md5:9fd07059c2ccee3950dc8363429a3135',
147 'thumbnail': r
're:^http.*\.jpg',
148 'album': '偶像练习生 表演曲目合集',
149 'average_rating': int,
150 'album_artists': ['偶像练习生'],
153 'url': 'http://music.163.com/song?id=17241424',
158 'upload_date': '20080211',
159 'timestamp': 1202745600,
161 'thumbnail': r
're:^http.*\.jpg',
162 'album': 'Piano Solos Vol. 2',
163 'album_artist': 'Dustin O\'Halloran',
164 'average_rating': int,
165 'description': '[00:05.00]纯音乐,请欣赏\n',
166 'album_artists': ['Dustin O\'Halloran'],
167 'creators': ['Dustin O\'Halloran'],
168 'subtitles': {'lyrics': [{'ext': 'lrc'}]},
171 'url': 'https://y.music.163.com/m/song?app_version=8.8.45&id=95670&uct2=sKnvS4+0YStsWkqsPhFijw%3D%3D&dlt=0846',
172 'md5': 'b896be78d8d34bd7bb665b26710913ff',
177 'upload_date': '19911130',
178 'timestamp': 691516800,
179 'description': 'md5:1ba2f911a2b0aa398479f595224f2141',
180 'subtitles': {'lyrics': [{'ext': 'lrc'}]},
182 'alt_title': '伴唱:现代人乐队 合唱:总政歌舞团',
183 'thumbnail': r
're:^http.*\.jpg',
184 'average_rating': int,
186 'album_artist': '侯牧人',
188 'album_artists': ['侯牧人'],
191 'url': 'http://music.163.com/#/song?id=32102397',
192 'md5': '3e909614ce09b1ccef4a3eb205441190',
196 'title': 'Bad Blood',
197 'creators': ['Taylor Swift', 'Kendrick Lamar'],
198 'upload_date': '20150516',
199 'timestamp': 1431792000,
200 'description': 'md5:21535156efb73d6d1c355f95616e285a',
201 'subtitles': {'lyrics': [{'ext': 'lrc'}]},
203 'thumbnail': r
're:^http.*\.jpg',
204 'album': 'Bad Blood',
205 'average_rating': int,
206 'album_artist': 'Taylor Swift',
208 'skip': 'Blocked outside Mainland China',
210 'note': 'Has translated name.',
211 'url': 'http://music.163.com/#/song?id=22735043',
215 'title': '소원을 말해봐 (Genie)',
216 'creators': ['少女时代'],
217 'upload_date': '20100127',
218 'timestamp': 1264608000,
219 'description': 'md5:03d1ffebec3139aa4bafe302369269c5',
220 'subtitles': {'lyrics': [{'ext': 'lrc'}]},
222 'alt_title': '说出愿望吧(Genie)',
223 'thumbnail': r
're:^http.*\.jpg',
224 'average_rating': int,
226 'album_artist': '少女时代',
228 'skip': 'Blocked outside Mainland China',
231 def _process_lyrics(self
, lyrics_info
):
232 original
= traverse_obj(lyrics_info
, ('lrc', 'lyric', {str}
))
233 translated
= traverse_obj(lyrics_info
, ('tlyric', 'lyric', {str}
))
235 if not original
or original
== '[99:00.00]纯音乐,请欣赏\n':
240 'lyrics': [{'data': original
, 'ext': 'lrc'}],
243 lyrics_expr
= r
'(\[[0-9]{2}:[0-9]{2}\.[0-9]{2,}\])([^\n]+)'
244 original_ts_texts
= re
.findall(lyrics_expr
, original
)
245 translation_ts_dict
= dict(re
.findall(lyrics_expr
, translated
))
248 join_nonempty(f
'{timestamp}{text}', translation_ts_dict
.get(timestamp
, ''), delim
=' / ')
249 for timestamp
, text
in original_ts_texts
)
252 'lyrics_merged': [{'data': merged
, 'ext': 'lrc'}],
253 'lyrics': [{'data': original
, 'ext': 'lrc'}],
254 'lyrics_translated': [{'data': translated
, 'ext': 'lrc'}],
257 def _real_extract(self
, url
):
258 song_id
= self
._match
_id
(url
)
260 info
= self
._query
_api
(
261 f
'song/detail?id={song_id}&ids=%5B{song_id}%5D', song_id
, 'Downloading song info')['songs'][0]
263 formats
= self
._extract
_formats
(info
)
265 lyrics
= self
._process
_lyrics
(self
._query
_api
(
266 f
'song/lyric?id={song_id}&lv=-1&tv=-1', song_id
, 'Downloading lyrics data'))
268 'description': traverse_obj(lyrics
, (('lyrics_merged', 'lyrics'), 0, 'data'), get_all
=False),
275 'alt_title': '/'.join(traverse_obj(info
, (('transNames', 'alias'), ...))) or None,
276 'creators': traverse_obj(info
, ('artists', ..., 'name')) or None,
277 'album_artists': traverse_obj(info
, ('album', 'artists', ..., 'name')) or None,
279 **traverse_obj(info
, {
280 'title': ('name', {str}
),
281 'timestamp': ('album', 'publishTime', {int_or_none(scale
=1000)}),
282 'thumbnail': ('album', 'picUrl', {url_or_none}
),
283 'duration': ('duration', {int_or_none(scale
=1000)}),
284 'album': ('album', 'name', {str}
),
285 'average_rating': ('score', {int_or_none}
),
290 class NetEaseMusicAlbumIE(NetEaseMusicBaseIE
):
291 IE_NAME
= 'netease:album'
292 IE_DESC
= '网易云音乐 - 专辑'
293 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?album\?id=(?P<id>[0-9]+)'
295 'url': 'https://music.163.com/#/album?id=133153666',
299 'upload_date': '20210913',
300 'description': '桃几2021年翻唱合集',
301 'thumbnail': r
're:^http.*\.jpg',
303 'playlist_mincount': 12,
305 'url': 'http://music.163.com/#/album?id=220780',
309 'upload_date': '20060904',
310 'description': 'md5:71a74e1d8f392d88cf1bbe48879ad0b0',
311 'thumbnail': r
're:^http.*\.jpg',
313 'playlist_count': 23,
316 def _real_extract(self
, url
):
317 album_id
= self
._match
_id
(url
)
318 webpage
= self
._download
_webpage
(f
'https://music.163.com/album?id={album_id}', album_id
)
320 songs
= self
._search
_json
(
321 r
'<textarea[^>]+\bid="song-list-pre-data"[^>]*>', webpage
, 'metainfo', album_id
,
322 end_pattern
=r
'</textarea>', contains_pattern
=r
'\[(?s:.+)\]')
324 'title': self
._og
_search
_property
('title', webpage
, 'title', fatal
=False),
325 'description': self
._html
_search
_regex
(
326 (rf
'<div[^>]+\bid="album-desc-{suffix}"[^>]*>(.*?)</div>' for suffix
in ('more', 'dot')),
327 webpage
, 'description', flags
=re
.S
, fatal
=False),
328 'thumbnail': self
._og
_search
_property
('image', webpage
, 'thumbnail', fatal
=False),
329 'upload_date': unified_strdate(self
._html
_search
_meta
('music:release_date', webpage
, 'date', fatal
=False)),
331 return self
.playlist_result(self
._get
_entries
(songs
), album_id
, **metainfo
)
334 class NetEaseMusicSingerIE(NetEaseMusicBaseIE
):
335 IE_NAME
= 'netease:singer'
336 IE_DESC
= '网易云音乐 - 歌手'
337 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?artist\?id=(?P<id>[0-9]+)'
339 'note': 'Singer has aliases.',
340 'url': 'http://music.163.com/#/artist?id=10559',
343 'title': '张惠妹 - aMEI;阿妹;阿密特',
345 'playlist_count': 50,
347 'note': 'Singer has translated name.',
348 'url': 'http://music.163.com/#/artist?id=124098',
351 'title': '李昇基 - 이승기',
353 'playlist_count': 50,
355 'note': 'Singer with both translated and alias',
356 'url': 'https://music.163.com/#/artist?id=159692',
359 'title': '初音ミク - 初音未来;Hatsune Miku',
361 'playlist_count': 50,
364 def _real_extract(self
, url
):
365 singer_id
= self
._match
_id
(url
)
367 info
= self
._query
_api
(
368 f
'artist/{singer_id}?id={singer_id}', singer_id
, note
='Downloading singer data')
370 name
= join_nonempty(
371 traverse_obj(info
, ('artist', 'name', {str}
)),
372 join_nonempty(*traverse_obj(info
, ('artist', ('trans', ('alias', ...)), {str}
)), delim
=';'),
375 return self
.playlist_result(self
._get
_entries
(info
, 'hotSongs'), singer_id
, name
)
378 class NetEaseMusicListIE(NetEaseMusicBaseIE
):
379 IE_NAME
= 'netease:playlist'
380 IE_DESC
= '网易云音乐 - 歌单'
381 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?(?:playlist|discover/toplist)\?id=(?P<id>[0-9]+)'
383 'url': 'http://music.163.com/#/playlist?id=79177352',
386 'title': 'Billboard 2007 Top 100',
387 'description': 'md5:12fd0819cab2965b9583ace0f8b7b022',
390 'uploader_id': '67549805',
392 'upload_date': r
're:\d{8}',
394 'playlist_mincount': 95,
396 'note': 'Toplist/Charts sample',
397 'url': 'https://music.163.com/#/discover/toplist?id=60198',
400 'title': 're:美国Billboard榜 [0-9]{4}-[0-9]{2}-[0-9]{2}',
401 'description': '美国Billboard排行榜',
402 'tags': ['流行', '欧美', '榜单'],
403 'uploader': 'Billboard公告牌',
404 'uploader_id': '48171',
406 'upload_date': r
're:\d{8}',
408 'playlist_count': 100,
410 'note': 'Toplist/Charts sample',
411 'url': 'http://music.163.com/#/discover/toplist?id=3733003',
414 'title': 're:韩国Melon排行榜周榜(?: [0-9]{4}-[0-9]{2}-[0-9]{2})?',
415 'description': 'md5:73ec782a612711cadc7872d9c1e134fc',
416 'upload_date': '20200109',
417 'uploader_id': '2937386',
418 'tags': ['韩语', '榜单'],
419 'uploader': 'Melon榜单',
420 'timestamp': 1578569373,
422 'playlist_count': 50,
425 def _real_extract(self
, url
):
426 list_id
= self
._match
_id
(url
)
428 info
= self
._download
_eapi
_json
(
429 '/v3/playlist/detail', list_id
,
430 {'id': list_id
, 't': '-1', 'n': '500', 's': '0'},
431 note
='Downloading playlist info')
433 metainfo
= traverse_obj(info
, ('playlist', {
434 'title': ('name', {str}
),
435 'description': ('description', {str}
),
436 'tags': ('tags', ..., {str}
),
437 'uploader': ('creator', 'nickname', {str}
),
438 'uploader_id': ('creator', 'userId', {str_or_none}
),
439 'timestamp': ('updateTime', {int_or_none(scale
=1000)}),
441 if traverse_obj(info
, ('playlist', 'specialType')) == 10:
442 metainfo
['title'] = f
'{metainfo.get("title")} {strftime_or_none(metainfo.get("timestamp"), "%Y-%m-%d")}'
444 return self
.playlist_result(self
._get
_entries
(info
, ('playlist', 'tracks')), list_id
, **metainfo
)
447 class NetEaseMusicMvIE(NetEaseMusicBaseIE
):
448 IE_NAME
= 'netease:mv'
449 IE_DESC
= '网易云音乐 - MV'
450 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?mv\?id=(?P<id>[0-9]+)'
452 'url': 'https://music.163.com/#/mv?id=10958064',
457 'description': 'md5:e845872cff28820642a2b02eda428fea',
459 'upload_date': '20200916',
460 'thumbnail': r
're:http.*\.jpg',
464 'comment_count': int,
467 'url': 'http://music.163.com/#/mv?id=415350',
471 'title': '이럴거면 그러지말지',
472 'description': '白雅言自作曲唱甜蜜爱情',
474 'upload_date': '20150520',
475 'thumbnail': r
're:http.*\.jpg',
479 'comment_count': int,
481 'skip': 'Blocked outside Mainland China',
483 'note': 'This MV has multiple creators.',
484 'url': 'https://music.163.com/#/mv?id=22593543',
489 'creators': ['秃子2z', '辉子', 'Saber梁维嘉'],
491 'upload_date': '20240618',
493 'comment_count': int,
494 'thumbnail': r
're:http.*\.jpg',
499 def _real_extract(self
, url
):
500 mv_id
= self
._match
_id
(url
)
502 info
= self
._query
_api
(
503 f
'mv/detail?id={mv_id}&type=mp4', mv_id
, 'Downloading mv info')['data']
506 {'url': mv_url
, 'ext': 'mp4', 'format_id': f
'{brs}p', 'height': int_or_none(brs
)}
507 for brs
, mv_url
in info
['brs'].items()
513 'creators': traverse_obj(info
, ('artists', ..., 'name')) or [info
.get('artistName')],
514 **traverse_obj(info
, {
515 'title': ('name', {str}
),
516 'description': (('desc', 'briefDesc'), {str}
, filter),
517 'upload_date': ('publishTime', {unified_strdate}
),
518 'thumbnail': ('cover', {url_or_none}
),
519 'duration': ('duration', {int_or_none(scale
=1000)}),
520 'view_count': ('playCount', {int_or_none}
),
521 'like_count': ('likeCount', {int_or_none}
),
522 'comment_count': ('commentCount', {int_or_none}
),
527 class NetEaseMusicProgramIE(NetEaseMusicBaseIE
):
528 IE_NAME
= 'netease:program'
529 IE_DESC
= '网易云音乐 - 电台节目'
530 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?program\?id=(?P<id>[0-9]+)'
532 'url': 'http://music.163.com/#/program?id=10109055',
536 'title': '不丹足球背后的故事',
537 'description': '喜马拉雅人的足球梦 ...',
538 'creators': ['大话西藏'],
539 'timestamp': 1434179287,
540 'upload_date': '20150613',
541 'thumbnail': r
're:http.*\.jpg',
545 'note': 'This program has accompanying songs.',
546 'url': 'http://music.163.com/#/program?id=10141022',
549 'title': '滚滚电台的有声节目',
550 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
551 'creators': ['滚滚电台ORZ'],
552 'timestamp': 1434450733,
553 'upload_date': '20150616',
554 'thumbnail': r
're:http.*\.jpg',
558 'note': 'This program has accompanying songs.',
559 'url': 'http://music.163.com/#/program?id=10141022',
563 'title': '滚滚电台的有声节目',
564 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
565 'creators': ['滚滚电台ORZ'],
566 'timestamp': 1434450733,
567 'upload_date': '20150616',
568 'thumbnail': r
're:http.*\.jpg',
576 def _real_extract(self
, url
):
577 program_id
= self
._match
_id
(url
)
579 info
= self
._query
_api
(
580 f
'dj/program/detail?id={program_id}', program_id
, note
='Downloading program info')['program']
582 metainfo
= traverse_obj(info
, {
583 'title': ('name', {str}
),
584 'description': ('description', {str}
),
585 'creator': ('dj', 'brand', {str}
),
586 'thumbnail': ('coverUrl', {url_or_none}
),
587 'timestamp': ('createTime', {int_or_none(scale
=1000)}),
590 if not self
._yes
_playlist
(
591 info
['songs'] and program_id
, info
['mainSong']['id'], playlist_label
='program', video_label
='song'):
592 formats
= self
._extract
_formats
(info
['mainSong'])
595 'id': str(info
['mainSong']['id']),
597 'duration': traverse_obj(info
, ('mainSong', 'duration', {int_or_none(scale
=1000)})),
601 songs
= traverse_obj(info
, (('mainSong', ('songs', ...)),))
602 return self
.playlist_result(self
._get
_entries
(songs
), program_id
, **metainfo
)
605 class NetEaseMusicDjRadioIE(NetEaseMusicBaseIE
):
606 IE_NAME
= 'netease:djradio'
607 IE_DESC
= '网易云音乐 - 电台'
608 _VALID_URL
= r
'https?://music\.163\.com/(?:#/)?djradio\?id=(?P<id>[0-9]+)'
610 'url': 'http://music.163.com/#/djradio?id=42',
614 'description': 'md5:c7381ebd7989f9f367668a5aee7d5f08',
616 'playlist_mincount': 40,
620 def _real_extract(self
, url
):
621 dj_id
= self
._match
_id
(url
)
625 for offset
in itertools
.count(start
=0, step
=self
._PAGE
_SIZE
):
626 info
= self
._query
_api
(
627 f
'dj/program/byradio?asc=false&limit={self._PAGE_SIZE}&radioId={dj_id}&offset={offset}',
628 dj_id
, note
=f
'Downloading dj programs - {offset}')
630 entries
.extend(self
.url_result(
631 f
'http://music.163.com/#/program?id={program["id"]}', NetEaseMusicProgramIE
,
632 program
['id'], program
.get('name')) for program
in info
['programs'])
634 metainfo
= traverse_obj(info
, ('programs', 0, 'radio', {
635 'title': ('name', {str}
),
636 'description': ('desc', {str}
),
642 return self
.playlist_result(entries
, dj_id
, **metainfo
)