8 from .common
import InfoExtractor
9 from ..aes
import aes_ecb_encrypt
, pkcs7_padding
24 class NetEaseMusicBaseIE(InfoExtractor
):
25 _FORMATS
= ['bMusic', 'mMusic', 'hMusic']
26 _API_BASE
= 'http://music.163.com/api/'
30 def kilo_or_none(value
):
31 return int_or_none(value
, scale
=1000)
33 def _create_eapi_cipher(self
, api_path
, query_body
, cookies
):
34 request_text
= json
.dumps({**query_body, 'header': cookies}
, separators
=(',', ':'))
36 message
= f
'nobody{api_path}use{request_text}md5forencrypt'.encode('latin1')
37 msg_digest
= hashlib
.md5(message
).hexdigest()
39 data
= pkcs7_padding(list(str.encode(
40 f
'{api_path}-36cd479b6b5-{request_text}-36cd479b6b5-{msg_digest}')))
41 encrypted
= bytes(aes_ecb_encrypt(data
, list(b
'e82ckenh8dichen8')))
42 return f
'params={encrypted.hex().upper()}'.encode()
44 def _download_eapi_json(self
, path
, video_id
, query_body
, headers
={}, **kwargs
):
47 'deviceId': 'undefined',
50 'mobilename': 'undefined',
51 'buildver': '1623435496',
52 'resolution': '1920x1080',
55 'channel': 'undefined',
56 'requestId': f
'{int(time.time() * 1000)}_{random.randint(0, 1000):04}',
57 **traverse_obj(self
._get
_cookies
(self
._API
_BASE
), {
58 'MUSIC_U': ('MUSIC_U', {lambda i: i.value}
),
61 return self
._download
_json
(
62 urljoin('https://interface3.music.163.com/', f
'/eapi{path}'), video_id
,
63 data
=self
._create
_eapi
_cipher
(f
'/api{path}', query_body
, cookies
), headers
={
64 'Referer': 'https://music.163.com',
65 'Cookie': '; '.join([f
'{k}={v}' for k
, v
in cookies
.items()]),
69 def _call_player_api(self
, song_id
, bitrate
):
70 return self
._download
_eapi
_json
(
71 '/song/enhance/player/url', song_id
, {'ids': f'[{song_id}
]', 'br
': bitrate},
72 note=f'Downloading song URL info
: bitrate {bitrate}
')
74 def extract_formats(self, info):
78 for song_format in self._FORMATS:
79 details = info.get(song_format)
82 bitrate = int_or_none(details.get('bitrate
')) or 999000
83 for song in traverse_obj(self._call_player_api(song_id, bitrate), ('data
', lambda _, v: url_or_none(v['url
']))):
84 song_url = song['url
']
85 if self._is_valid_url(song_url, info['id'], 'song
'):
88 'format_id
': song_format,
89 'asr
': traverse_obj(details, ('sr
', {int_or_none})),
90 **traverse_obj(song, {
91 'ext
': ('type', {str}),
92 'abr
': ('br
', {self.kilo_or_none}),
93 'filesize
': ('size
', {int_or_none}),
97 err = traverse_obj(song, ('code
', {int})) or 0
100 if err != 0 and (err < 200 or err >= 400):
101 raise ExtractorError(f'No media links
found (site code {err}
)', expected=True)
103 self.raise_geo_restricted(
104 'No media links found
: probably due to geo restriction
.', countries=['CN
'])
107 def query_api(self, endpoint, video_id, note):
108 result = self._download_json(
109 f'{self._API_BASE}{endpoint}
', video_id, note, headers={'Referer': self._API_BASE})
110 code = traverse_obj(result, ('code
', {int}))
111 message = traverse_obj(result, ('message
', {str})) or ''
113 self.raise_login_required(f'Login required to download
: {message}
')
115 raise ExtractorError(f'Failed to get meta info
: {code} {message}
')
118 def _get_entries(self, songs_data, entry_keys=None, id_key='id', name_key='name
'):
119 for song in traverse_obj(songs_data, (
120 *variadic(entry_keys, (str, bytes, dict, set)),
121 lambda _, v: int_or_none(v[id_key]) is not None)):
122 song_id = str(song[id_key])
123 yield self.url_result(
124 f'http
://music
.163.com
/#/song?id={song_id}', NetEaseMusicIE,
125 song_id
, traverse_obj(song
, (name_key
, {str}
)))
128 class NetEaseMusicIE(NetEaseMusicBaseIE
):
129 IE_NAME
= 'netease:song'
131 _VALID_URL
= r
'https?://(y\.)?music\.163\.com/(?:[#m]/)?song\?.*?\bid=(?P<id>[0-9]+)'
133 'url': 'https://music.163.com/#/song?id=548648087',
137 'title': '戒烟 (Live)',
138 'creator': '李荣浩 / 朱正廷 / 陈立农 / 尤长靖 / ONER灵超 / ONER木子洋 / 杨非同 / 陆定昊',
139 'timestamp': 1522944000,
140 'upload_date': '20180405',
141 'description': 'md5:3650af9ee22c87e8637cb2dde22a765c',
142 'subtitles': {'lyrics': [{'ext': 'lrc'}
]},
144 'thumbnail': r
're:^http.*\.jpg',
145 'album': '偶像练习生 表演曲目合集',
146 'average_rating': int,
147 'album_artist': '偶像练习生',
150 'note': 'No lyrics.',
151 'url': 'http://music.163.com/song?id=17241424',
156 'creator': 'Dustin O\'Halloran',
157 'upload_date': '20080211',
158 'timestamp': 1202745600,
160 'thumbnail': r
're:^http.*\.jpg',
161 'album': 'Piano Solos Vol. 2',
162 'album_artist': 'Dustin O\'Halloran',
163 'average_rating': int,
166 'url': 'https://y.music.163.com/m/song?app_version=8.8.45&id=95670&uct2=sKnvS4+0YStsWkqsPhFijw%3D%3D&dlt=0846',
167 'md5': '95826c73ea50b1c288b22180ec9e754d',
173 'upload_date': '19911130',
174 'timestamp': 691516800,
175 'description': 'md5:1ba2f911a2b0aa398479f595224f2141',
176 'subtitles': {'lyrics': [{'ext': 'lrc'}
]},
178 'alt_title': '伴唱:现代人乐队 合唱:总政歌舞团',
179 'thumbnail': r
're:^http.*\.jpg',
180 'average_rating': int,
182 'album_artist': '侯牧人',
185 'url': 'http://music.163.com/#/song?id=32102397',
186 'md5': '3e909614ce09b1ccef4a3eb205441190',
190 'title': 'Bad Blood',
191 'creator': 'Taylor Swift / Kendrick Lamar',
192 'upload_date': '20150516',
193 'timestamp': 1431792000,
194 'description': 'md5:21535156efb73d6d1c355f95616e285a',
195 'subtitles': {'lyrics': [{'ext': 'lrc'}
]},
197 'thumbnail': r
're:^http.*\.jpg',
198 'album': 'Bad Blood',
199 'average_rating': int,
200 'album_artist': 'Taylor Swift',
202 'skip': 'Blocked outside Mainland China',
204 'note': 'Has translated name.',
205 'url': 'http://music.163.com/#/song?id=22735043',
209 'title': '소원을 말해봐 (Genie)',
211 'upload_date': '20100127',
212 'timestamp': 1264608000,
213 'description': 'md5:03d1ffebec3139aa4bafe302369269c5',
214 'subtitles': {'lyrics': [{'ext': 'lrc'}
]},
216 'alt_title': '说出愿望吧(Genie)',
217 'thumbnail': r
're:^http.*\.jpg',
218 'average_rating': int,
220 'album_artist': '少女时代',
222 'skip': 'Blocked outside Mainland China',
225 def _process_lyrics(self
, lyrics_info
):
226 original
= traverse_obj(lyrics_info
, ('lrc', 'lyric', {str}
))
227 translated
= traverse_obj(lyrics_info
, ('tlyric', 'lyric', {str}
))
229 if not original
or original
== '[99:00.00]纯音乐,请欣赏\n':
234 'lyrics': [{'data': original, 'ext': 'lrc'}
],
237 lyrics_expr
= r
'(\[[0-9]{2}:[0-9]{2}\.[0-9]{2,}\])([^\n]+)'
238 original_ts_texts
= re
.findall(lyrics_expr
, original
)
239 translation_ts_dict
= dict(re
.findall(lyrics_expr
, translated
))
242 join_nonempty(f
'{timestamp}{text}', translation_ts_dict
.get(timestamp
, ''), delim
=' / ')
243 for timestamp
, text
in original_ts_texts
)
246 'lyrics_merged': [{'data': merged, 'ext': 'lrc'}
],
247 'lyrics': [{'data': original, 'ext': 'lrc'}
],
248 'lyrics_translated': [{'data': translated, 'ext': 'lrc'}
],
251 def _real_extract(self
, url
):
252 song_id
= self
._match
_id
(url
)
254 info
= self
.query_api(
255 f
'song/detail?id={song_id}&ids=%5B{song_id}%5D', song_id
, 'Downloading song info')['songs'][0]
257 formats
= self
.extract_formats(info
)
259 lyrics
= self
._process
_lyrics
(self
.query_api(
260 f
'song/lyric?id={song_id}&lv=-1&tv=-1', song_id
, 'Downloading lyrics data'))
262 'description': traverse_obj(lyrics
, (('lyrics_merged', 'lyrics'), 0, 'data'), get_all
=False),
269 'alt_title': '/'.join(traverse_obj(info
, (('transNames', 'alias'), ...))) or None,
270 'creator': ' / '.join(traverse_obj(info
, ('artists', ..., 'name'))) or None,
271 'album_artist': ' / '.join(traverse_obj(info
, ('album', 'artists', ..., 'name'))) or None,
273 **traverse_obj(info
, {
274 'title': ('name', {str}
),
275 'timestamp': ('album', 'publishTime', {self.kilo_or_none}
),
276 'thumbnail': ('album', 'picUrl', {url_or_none}
),
277 'duration': ('duration', {self.kilo_or_none}
),
278 'album': ('album', 'name', {str}
),
279 'average_rating': ('score', {int_or_none}
),
284 class NetEaseMusicAlbumIE(NetEaseMusicBaseIE
):
285 IE_NAME
= 'netease:album'
286 IE_DESC
= '网易云音乐 - 专辑'
287 _VALID_URL
= r
'https?://music\.163\.com/(#/)?album\?id=(?P<id>[0-9]+)'
289 'url': 'https://music.163.com/#/album?id=133153666',
293 'upload_date': '20210913',
294 'description': '桃几2021年翻唱合集',
295 'thumbnail': r
're:^http.*\.jpg',
297 'playlist_mincount': 13,
299 'url': 'http://music.163.com/#/album?id=220780',
303 'upload_date': '20060904',
304 'description': 'md5:71a74e1d8f392d88cf1bbe48879ad0b0',
305 'thumbnail': r
're:^http.*\.jpg',
307 'playlist_count': 23,
310 def _real_extract(self
, url
):
311 album_id
= self
._match
_id
(url
)
312 webpage
= self
._download
_webpage
(f
'https://music.163.com/album?id={album_id}', album_id
)
314 songs
= self
._search
_json
(
315 r
'<textarea[^>]+\bid="song-list-pre-data"[^>]*>', webpage
, 'metainfo', album_id
,
316 end_pattern
=r
'</textarea>', contains_pattern
=r
'\[(?s:.+)\]')
318 'title': self
._og
_search
_property
('title', webpage
, 'title', fatal
=False),
319 'description': self
._html
_search
_regex
(
320 (rf
'<div[^>]+\bid="album-desc-{suffix}"[^>]*>(.*?)</div>' for suffix
in ('more', 'dot')),
321 webpage
, 'description', flags
=re
.S
, fatal
=False),
322 'thumbnail': self
._og
_search
_property
('image', webpage
, 'thumbnail', fatal
=False),
323 'upload_date': unified_strdate(self
._html
_search
_meta
('music:release_date', webpage
, 'date', fatal
=False)),
325 return self
.playlist_result(self
._get
_entries
(songs
), album_id
, **metainfo
)
328 class NetEaseMusicSingerIE(NetEaseMusicBaseIE
):
329 IE_NAME
= 'netease:singer'
330 IE_DESC
= '网易云音乐 - 歌手'
331 _VALID_URL
= r
'https?://music\.163\.com/(#/)?artist\?id=(?P<id>[0-9]+)'
333 'note': 'Singer has aliases.',
334 'url': 'http://music.163.com/#/artist?id=10559',
337 'title': '张惠妹 - aMEI;阿妹;阿密特',
339 'playlist_count': 50,
341 'note': 'Singer has translated name.',
342 'url': 'http://music.163.com/#/artist?id=124098',
345 'title': '李昇基 - 이승기',
347 'playlist_count': 50,
349 'note': 'Singer with both translated and alias',
350 'url': 'https://music.163.com/#/artist?id=159692',
353 'title': '初音ミク - 初音未来;Hatsune Miku',
355 'playlist_count': 50,
358 def _real_extract(self
, url
):
359 singer_id
= self
._match
_id
(url
)
361 info
= self
.query_api(
362 f
'artist/{singer_id}?id={singer_id}', singer_id
, note
='Downloading singer data')
364 name
= join_nonempty(
365 traverse_obj(info
, ('artist', 'name', {str}
)),
366 join_nonempty(*traverse_obj(info
, ('artist', ('trans', ('alias', ...)), {str}
)), delim
=';'),
369 return self
.playlist_result(self
._get
_entries
(info
, 'hotSongs'), singer_id
, name
)
372 class NetEaseMusicListIE(NetEaseMusicBaseIE
):
373 IE_NAME
= 'netease:playlist'
374 IE_DESC
= '网易云音乐 - 歌单'
375 _VALID_URL
= r
'https?://music\.163\.com/(#/)?(playlist|discover/toplist)\?id=(?P<id>[0-9]+)'
377 'url': 'http://music.163.com/#/playlist?id=79177352',
380 'title': 'Billboard 2007 Top 100',
381 'description': 'md5:12fd0819cab2965b9583ace0f8b7b022',
384 'uploader_id': '67549805',
386 'upload_date': r
're:\d{8}',
388 'playlist_mincount': 95,
390 'note': 'Toplist/Charts sample',
391 'url': 'https://music.163.com/#/discover/toplist?id=60198',
394 'title': 're:美国Billboard榜 [0-9]{4}-[0-9]{2}-[0-9]{2}',
395 'description': '美国Billboard排行榜',
396 'tags': ['流行', '欧美', '榜单'],
397 'uploader': 'Billboard公告牌',
398 'uploader_id': '48171',
400 'upload_date': r
're:\d{8}',
402 'playlist_count': 100,
404 'note': 'Toplist/Charts sample',
405 'url': 'http://music.163.com/#/discover/toplist?id=3733003',
408 'title': 're:韩国Melon排行榜周榜 [0-9]{4}-[0-9]{2}-[0-9]{2}',
409 'description': 'md5:73ec782a612711cadc7872d9c1e134fc',
411 'playlist_count': 50,
412 'skip': 'Blocked outside Mainland China',
415 def _real_extract(self
, url
):
416 list_id
= self
._match
_id
(url
)
418 info
= self
._download
_eapi
_json
(
419 '/v3/playlist/detail', list_id
,
420 {'id': list_id, 't': '-1', 'n': '500', 's': '0'}
,
421 note
="Downloading playlist info")
423 metainfo
= traverse_obj(info
, ('playlist', {
424 'title': ('name', {str}
),
425 'description': ('description', {str}
),
426 'tags': ('tags', ..., {str}
),
427 'uploader': ('creator', 'nickname', {str}
),
428 'uploader_id': ('creator', 'userId', {str_or_none}
),
429 'timestamp': ('updateTime', {self.kilo_or_none}
),
431 if traverse_obj(info
, ('playlist', 'specialType')) == 10:
432 metainfo
['title'] = f
'{metainfo.get("title")} {strftime_or_none(metainfo.get("timestamp"), "%Y-%m-%d")}'
434 return self
.playlist_result(self
._get
_entries
(info
, ('playlist', 'tracks')), list_id
, **metainfo
)
437 class NetEaseMusicMvIE(NetEaseMusicBaseIE
):
438 IE_NAME
= 'netease:mv'
439 IE_DESC
= '网易云音乐 - MV'
440 _VALID_URL
= r
'https?://music\.163\.com/(#/)?mv\?id=(?P<id>[0-9]+)'
442 'url': 'https://music.163.com/#/mv?id=10958064',
447 'description': 'md5:e845872cff28820642a2b02eda428fea',
449 'upload_date': '20200916',
450 'thumbnail': r
're:http.*\.jpg',
454 'comment_count': int,
457 'url': 'http://music.163.com/#/mv?id=415350',
461 'title': '이럴거면 그러지말지',
462 'description': '白雅言自作曲唱甜蜜爱情',
464 'upload_date': '20150520',
465 'thumbnail': r
're:http.*\.jpg',
469 'comment_count': int,
473 def _real_extract(self
, url
):
474 mv_id
= self
._match
_id
(url
)
476 info
= self
.query_api(
477 f
'mv/detail?id={mv_id}&type=mp4', mv_id
, 'Downloading mv info')['data']
480 {'url': mv_url, 'ext': 'mp4', 'format_id': f'{brs}p
', 'height
': int_or_none(brs)}
481 for brs, mv_url in info['brs
'].items()
487 **traverse_obj(info, {
488 'title
': ('name
', {str}),
489 'description
': (('desc
', 'briefDesc
'), {str}, {lambda x: x or None}),
490 'creator
': ('artistName
', {str}),
491 'upload_date
': ('publishTime
', {unified_strdate}),
492 'thumbnail
': ('cover
', {url_or_none}),
493 'duration
': ('duration
', {self.kilo_or_none}),
494 'view_count
': ('playCount
', {int_or_none}),
495 'like_count
': ('likeCount
', {int_or_none}),
496 'comment_count
': ('commentCount
', {int_or_none}),
501 class NetEaseMusicProgramIE(NetEaseMusicBaseIE):
502 IE_NAME = 'netease
:program
'
503 IE_DESC = '网易云音乐
- 电台节目
'
504 _VALID_URL = r'https?
://music\
.163\
.com
/(#/?)program\?id=(?P<id>[0-9]+)'
506 'url': 'http://music.163.com/#/program?id=10109055',
510 'title': '不丹足球背后的故事',
511 'description': '喜马拉雅人的足球梦 ...',
513 'timestamp': 1434179287,
514 'upload_date': '20150613',
515 'thumbnail': r
're:http.*\.jpg',
519 'note': 'This program has accompanying songs.',
520 'url': 'http://music.163.com/#/program?id=10141022',
523 'title': '滚滚电台的有声节目',
524 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
525 'creator': '滚滚电台ORZ',
526 'timestamp': 1434450733,
527 'upload_date': '20150616',
528 'thumbnail': r
're:http.*\.jpg',
532 'note': 'This program has accompanying songs.',
533 'url': 'http://music.163.com/#/program?id=10141022',
537 'title': '滚滚电台的有声节目',
538 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
539 'creator': '滚滚电台ORZ',
540 'timestamp': 1434450733,
541 'upload_date': '20150616',
542 'thumbnail': r
're:http.*\.jpg',
550 def _real_extract(self
, url
):
551 program_id
= self
._match
_id
(url
)
553 info
= self
.query_api(
554 f
'dj/program/detail?id={program_id}', program_id
, note
='Downloading program info')['program']
556 metainfo
= traverse_obj(info
, {
557 'title': ('name', {str}
),
558 'description': ('description', {str}
),
559 'creator': ('dj', 'brand', {str}
),
560 'thumbnail': ('coverUrl', {url_or_none}
),
561 'timestamp': ('createTime', {self.kilo_or_none}
),
564 if not self
._yes
_playlist
(info
['songs'] and program_id
, info
['mainSong']['id']):
565 formats
= self
.extract_formats(info
['mainSong'])
568 'id': str(info
['mainSong']['id']),
570 'duration': traverse_obj(info
, ('mainSong', 'duration', {self.kilo_or_none}
)),
574 songs
= traverse_obj(info
, (('mainSong', ('songs', ...)),))
575 return self
.playlist_result(self
._get
_entries
(songs
), program_id
, **metainfo
)
578 class NetEaseMusicDjRadioIE(NetEaseMusicBaseIE
):
579 IE_NAME
= 'netease:djradio'
580 IE_DESC
= '网易云音乐 - 电台'
581 _VALID_URL
= r
'https?://music\.163\.com/(#/)?djradio\?id=(?P<id>[0-9]+)'
583 'url': 'http://music.163.com/#/djradio?id=42',
587 'description': 'md5:c7381ebd7989f9f367668a5aee7d5f08'
589 'playlist_mincount': 40,
593 def _real_extract(self
, url
):
594 dj_id
= self
._match
_id
(url
)
598 for offset
in itertools
.count(start
=0, step
=self
._PAGE
_SIZE
):
599 info
= self
.query_api(
600 f
'dj/program/byradio?asc=false&limit={self._PAGE_SIZE}&radioId={dj_id}&offset={offset}',
601 dj_id
, note
=f
'Downloading dj programs - {offset}')
603 entries
.extend(self
.url_result(
604 f
'http://music.163.com/#/program?id={program["id"]}', NetEaseMusicProgramIE
,
605 program
['id'], program
.get('name')) for program
in info
['programs'])
607 metainfo
= traverse_obj(info
, ('programs', 0, 'radio', {
608 'title': ('name', {str}
),
609 'description': ('desc', {str}
),
615 return self
.playlist_result(entries
, dj_id
, **metainfo
)