3 from base64
import b64encode
4 from datetime
import datetime
5 from hashlib
import md5
7 from .common
import InfoExtractor
8 from ..compat
import compat_str
, compat_urllib_parse_urlencode
9 from ..utils
import float_or_none
, sanitized_Request
12 class NetEaseMusicBaseIE(InfoExtractor
):
13 _FORMATS
= ['bMusic', 'mMusic', 'hMusic']
14 _NETEASE_SALT
= '3go8&$8*3*3h0k(2)2'
15 _API_BASE
= 'http://music.163.com/api/'
18 def _encrypt(cls
, dfsid
):
19 salt_bytes
= bytearray(cls
._NETEASE
_SALT
.encode('utf-8'))
20 string_bytes
= bytearray(compat_str(dfsid
).encode('ascii'))
21 salt_len
= len(salt_bytes
)
22 for i
in range(len(string_bytes
)):
23 string_bytes
[i
] = string_bytes
[i
] ^ salt_bytes
[i
% salt_len
]
25 m
.update(bytes(string_bytes
))
26 result
= b64encode(m
.digest()).decode('ascii')
27 return result
.replace('/', '_').replace('+', '-')
29 def extract_formats(self
, info
):
31 for song_format
in self
._FORMATS
:
32 details
= info
.get(song_format
)
35 song_file_path
= '/%s/%s.%s' % (
36 self
._encrypt
(details
['dfsId']), details
['dfsId'], details
['extension'])
38 # 203.130.59.9, 124.40.233.182, 115.231.74.139, etc is a reverse proxy-like feature
39 # from NetEase's CDN provider that can be used if m5.music.126.net does not
40 # work, especially for users outside of Mainland China
41 # via: https://github.com/JixunMoe/unblock-163/issues/3#issuecomment-163115880
42 for host
in ('http://m5.music.126.net', 'http://115.231.74.139/m1.music.126.net',
43 'http://124.40.233.182/m1.music.126.net', 'http://203.130.59.9/m1.music.126.net'):
44 song_url
= host
+ song_file_path
45 if self
._is
_valid
_url
(song_url
, info
['id'], 'song'):
48 'ext': details
.get('extension'),
49 'abr': float_or_none(details
.get('bitrate'), scale
=1000),
50 'format_id': song_format
,
51 'filesize': details
.get('size'),
52 'asr': details
.get('sr')
58 def convert_milliseconds(cls
, ms
):
59 return int(round(ms
/ 1000.0))
61 def query_api(self
, endpoint
, video_id
, note
):
62 req
= sanitized_Request('%s%s' % (self
._API
_BASE
, endpoint
))
63 req
.add_header('Referer', self
._API
_BASE
)
64 return self
._download
_json
(req
, video_id
, note
)
67 class NetEaseMusicIE(NetEaseMusicBaseIE
):
68 IE_NAME
= 'netease:song'
70 _VALID_URL
= r
'https?://music\.163\.com/(#/)?song\?id=(?P<id>[0-9]+)'
72 'url': 'http://music.163.com/#/song?id=32102397',
73 'md5': 'f2e97280e6345c74ba9d5677dd5dcb45',
77 'title': 'Bad Blood (feat. Kendrick Lamar)',
78 'creator': 'Taylor Swift / Kendrick Lamar',
79 'upload_date': '20150517',
80 'timestamp': 1431878400,
81 'description': 'md5:a10a54589c2860300d02e1de821eb2ef',
83 'skip': 'Blocked outside Mainland China',
85 'note': 'No lyrics translation.',
86 'url': 'http://music.163.com/#/song?id=29822014',
92 'upload_date': '20141225',
93 'timestamp': 1419523200,
94 'description': 'md5:a4d8d89f44656af206b7b2555c0bce6c',
96 'skip': 'Blocked outside Mainland China',
99 'url': 'http://music.163.com/song?id=17241424',
104 'creator': 'Dustin O\'Halloran',
105 'upload_date': '20080211',
106 'timestamp': 1202745600,
108 'skip': 'Blocked outside Mainland China',
110 'note': 'Has translated name.',
111 'url': 'http://music.163.com/#/song?id=22735043',
115 'title': '소원을 말해봐 (Genie)',
117 'description': 'md5:79d99cc560e4ca97e0c4d86800ee4184',
118 'upload_date': '20100127',
119 'timestamp': 1264608000,
120 'alt_title': '说出愿望吧(Genie)',
122 'skip': 'Blocked outside Mainland China',
125 def _process_lyrics(self
, lyrics_info
):
126 original
= lyrics_info
.get('lrc', {}).get('lyric')
127 translated
= lyrics_info
.get('tlyric', {}).get('lyric')
132 lyrics_expr
= r
'(\[[0-9]{2}:[0-9]{2}\.[0-9]{2,}\])([^\n]+)'
133 original_ts_texts
= re
.findall(lyrics_expr
, original
)
134 translation_ts_dict
= dict(
135 (time_stamp
, text
) for time_stamp
, text
in re
.findall(lyrics_expr
, translated
)
138 '%s%s / %s' % (time_stamp
, text
, translation_ts_dict
.get(time_stamp
, ''))
139 for time_stamp
, text
in original_ts_texts
143 def _real_extract(self
, url
):
144 song_id
= self
._match
_id
(url
)
148 'ids': '[%s]' % song_id
150 info
= self
.query_api(
151 'song/detail?' + compat_urllib_parse_urlencode(params
),
152 song_id
, 'Downloading song info')['songs'][0]
154 formats
= self
.extract_formats(info
)
155 self
._sort
_formats
(formats
)
157 lyrics_info
= self
.query_api(
158 'song/lyric?id=%s&lv=-1&tv=-1' % song_id
,
159 song_id
, 'Downloading lyrics data')
160 lyrics
= self
._process
_lyrics
(lyrics_info
)
163 if info
.get('transNames'):
164 alt_title
= '/'.join(info
.get('transNames'))
168 'title': info
['name'],
169 'alt_title': alt_title
,
170 'creator': ' / '.join([artist
['name'] for artist
in info
.get('artists', [])]),
171 'timestamp': self
.convert_milliseconds(info
.get('album', {}).get('publishTime')),
172 'thumbnail': info
.get('album', {}).get('picUrl'),
173 'duration': self
.convert_milliseconds(info
.get('duration', 0)),
174 'description': lyrics
,
179 class NetEaseMusicAlbumIE(NetEaseMusicBaseIE
):
180 IE_NAME
= 'netease:album'
181 IE_DESC
= '网易云音乐 - 专辑'
182 _VALID_URL
= r
'https?://music\.163\.com/(#/)?album\?id=(?P<id>[0-9]+)'
184 'url': 'http://music.163.com/#/album?id=220780',
189 'playlist_count': 23,
190 'skip': 'Blocked outside Mainland China',
193 def _real_extract(self
, url
):
194 album_id
= self
._match
_id
(url
)
196 info
= self
.query_api(
197 'album/%s?id=%s' % (album_id
, album_id
),
198 album_id
, 'Downloading album data')['album']
201 desc
= info
.get('description')
203 self
.url_result('http://music.163.com/#/song?id=%s' % song
['id'],
204 'NetEaseMusic', song
['id'])
205 for song
in info
['songs']
207 return self
.playlist_result(entries
, album_id
, name
, desc
)
210 class NetEaseMusicSingerIE(NetEaseMusicBaseIE
):
211 IE_NAME
= 'netease:singer'
212 IE_DESC
= '网易云音乐 - 歌手'
213 _VALID_URL
= r
'https?://music\.163\.com/(#/)?artist\?id=(?P<id>[0-9]+)'
215 'note': 'Singer has aliases.',
216 'url': 'http://music.163.com/#/artist?id=10559',
219 'title': '张惠妹 - aMEI;阿密特',
221 'playlist_count': 50,
222 'skip': 'Blocked outside Mainland China',
224 'note': 'Singer has translated name.',
225 'url': 'http://music.163.com/#/artist?id=124098',
228 'title': '李昇基 - 이승기',
230 'playlist_count': 50,
231 'skip': 'Blocked outside Mainland China',
234 def _real_extract(self
, url
):
235 singer_id
= self
._match
_id
(url
)
237 info
= self
.query_api(
238 'artist/%s?id=%s' % (singer_id
, singer_id
),
239 singer_id
, 'Downloading singer data')
241 name
= info
['artist']['name']
242 if info
['artist']['trans']:
243 name
= '%s - %s' % (name
, info
['artist']['trans'])
244 if info
['artist']['alias']:
245 name
= '%s - %s' % (name
, ';'.join(info
['artist']['alias']))
248 self
.url_result('http://music.163.com/#/song?id=%s' % song
['id'],
249 'NetEaseMusic', song
['id'])
250 for song
in info
['hotSongs']
252 return self
.playlist_result(entries
, singer_id
, name
)
255 class NetEaseMusicListIE(NetEaseMusicBaseIE
):
256 IE_NAME
= 'netease:playlist'
257 IE_DESC
= '网易云音乐 - 歌单'
258 _VALID_URL
= r
'https?://music\.163\.com/(#/)?(playlist|discover/toplist)\?id=(?P<id>[0-9]+)'
260 'url': 'http://music.163.com/#/playlist?id=79177352',
263 'title': 'Billboard 2007 Top 100',
264 'description': 'md5:12fd0819cab2965b9583ace0f8b7b022'
266 'playlist_count': 99,
267 'skip': 'Blocked outside Mainland China',
269 'note': 'Toplist/Charts sample',
270 'url': 'http://music.163.com/#/discover/toplist?id=3733003',
273 'title': 're:韩国Melon排行榜周榜 [0-9]{4}-[0-9]{2}-[0-9]{2}',
274 'description': 'md5:73ec782a612711cadc7872d9c1e134fc',
276 'playlist_count': 50,
277 'skip': 'Blocked outside Mainland China',
280 def _real_extract(self
, url
):
281 list_id
= self
._match
_id
(url
)
283 info
= self
.query_api(
284 'playlist/detail?id=%s&lv=-1&tv=-1' % list_id
,
285 list_id
, 'Downloading playlist data')['result']
288 desc
= info
.get('description')
290 if info
.get('specialType') == 10: # is a chart/toplist
291 datestamp
= datetime
.fromtimestamp(
292 self
.convert_milliseconds(info
['updateTime'])).strftime('%Y-%m-%d')
293 name
= '%s %s' % (name
, datestamp
)
296 self
.url_result('http://music.163.com/#/song?id=%s' % song
['id'],
297 'NetEaseMusic', song
['id'])
298 for song
in info
['tracks']
300 return self
.playlist_result(entries
, list_id
, name
, desc
)
303 class NetEaseMusicMvIE(NetEaseMusicBaseIE
):
304 IE_NAME
= 'netease:mv'
305 IE_DESC
= '网易云音乐 - MV'
306 _VALID_URL
= r
'https?://music\.163\.com/(#/)?mv\?id=(?P<id>[0-9]+)'
308 'url': 'http://music.163.com/#/mv?id=415350',
312 'title': '이럴거면 그러지말지',
313 'description': '白雅言自作曲唱甜蜜爱情',
315 'upload_date': '20150520',
317 'skip': 'Blocked outside Mainland China',
320 def _real_extract(self
, url
):
321 mv_id
= self
._match
_id
(url
)
323 info
= self
.query_api(
324 'mv/detail?id=%s&type=mp4' % mv_id
,
325 mv_id
, 'Downloading mv info')['data']
328 {'url': mv_url, 'ext': 'mp4', 'format_id': '%sp' % brs, 'height': int(brs)}
329 for brs
, mv_url
in info
['brs'].items()
331 self
._sort
_formats
(formats
)
335 'title': info
['name'],
336 'description': info
.get('desc') or info
.get('briefDesc'),
337 'creator': info
['artistName'],
338 'upload_date': info
['publishTime'].replace('-', ''),
340 'thumbnail': info
.get('cover'),
341 'duration': self
.convert_milliseconds(info
.get('duration', 0)),
345 class NetEaseMusicProgramIE(NetEaseMusicBaseIE
):
346 IE_NAME
= 'netease:program'
347 IE_DESC
= '网易云音乐 - 电台节目'
348 _VALID_URL
= r
'https?://music\.163\.com/(#/?)program\?id=(?P<id>[0-9]+)'
350 'url': 'http://music.163.com/#/program?id=10109055',
354 'title': '不丹足球背后的故事',
355 'description': '喜马拉雅人的足球梦 ...',
357 'timestamp': 1434179342,
358 'upload_date': '20150613',
361 'skip': 'Blocked outside Mainland China',
363 'note': 'This program has accompanying songs.',
364 'url': 'http://music.163.com/#/program?id=10141022',
367 'title': '25岁,你是自在如风的少年<27°C>',
368 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
371 'skip': 'Blocked outside Mainland China',
373 'note': 'This program has accompanying songs.',
374 'url': 'http://music.163.com/#/program?id=10141022',
378 'title': '25岁,你是自在如风的少年<27°C>',
379 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
380 'timestamp': 1434450841,
381 'upload_date': '20150616',
386 'skip': 'Blocked outside Mainland China',
389 def _real_extract(self
, url
):
390 program_id
= self
._match
_id
(url
)
392 info
= self
.query_api(
393 'dj/program/detail?id=%s' % program_id
,
394 program_id
, 'Downloading program info')['program']
397 description
= info
['description']
399 if not self
._yes
_playlist
(info
['songs'] and program_id
, info
['mainSong']['id']):
400 formats
= self
.extract_formats(info
['mainSong'])
401 self
._sort
_formats
(formats
)
404 'id': info
['mainSong']['id'],
406 'description': description
,
407 'creator': info
['dj']['brand'],
408 'timestamp': self
.convert_milliseconds(info
['createTime']),
409 'thumbnail': info
['coverUrl'],
410 'duration': self
.convert_milliseconds(info
.get('duration', 0)),
414 song_ids
= [info
['mainSong']['id']]
415 song_ids
.extend([song
['id'] for song
in info
['songs']])
417 self
.url_result('http://music.163.com/#/song?id=%s' % song_id
,
418 'NetEaseMusic', song_id
)
419 for song_id
in song_ids
421 return self
.playlist_result(entries
, program_id
, name
, description
)
424 class NetEaseMusicDjRadioIE(NetEaseMusicBaseIE
):
425 IE_NAME
= 'netease:djradio'
426 IE_DESC
= '网易云音乐 - 电台'
427 _VALID_URL
= r
'https?://music\.163\.com/(#/)?djradio\?id=(?P<id>[0-9]+)'
429 'url': 'http://music.163.com/#/djradio?id=42',
433 'description': 'md5:766220985cbd16fdd552f64c578a6b15'
435 'playlist_mincount': 40,
436 'skip': 'Blocked outside Mainland China',
440 def _real_extract(self
, url
):
441 dj_id
= self
._match
_id
(url
)
446 for offset
in itertools
.count(start
=0, step
=self
._PAGE
_SIZE
):
447 info
= self
.query_api(
448 'dj/program/byradio?asc=false&limit=%d&radioId=%s&offset=%d'
449 % (self
._PAGE
_SIZE
, dj_id
, offset
),
450 dj_id
, 'Downloading dj programs - %d' % offset
)
454 'http://music.163.com/#/program?id=%s' % program
['id'],
455 'NetEaseMusicProgram', program
['id'])
456 for program
in info
['programs']
460 radio
= info
['programs'][0]['radio']
467 return self
.playlist_result(entries
, dj_id
, name
, desc
)