]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/neteasemusic.py
Update changelog.
[youtubedl] / youtube_dl / extractor / neteasemusic.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from hashlib import md5
5 from base64 import b64encode
6 from datetime import datetime
7 import re
8
9 from .common import InfoExtractor
10 from ..compat import (
11 compat_urllib_parse,
12 compat_str,
13 compat_itertools_count,
14 )
15 from ..utils import (
16 sanitized_Request,
17 float_or_none,
18 )
19
20
21 class NetEaseMusicBaseIE(InfoExtractor):
22 _FORMATS = ['bMusic', 'mMusic', 'hMusic']
23 _NETEASE_SALT = '3go8&$8*3*3h0k(2)2'
24 _API_BASE = 'http://music.163.com/api/'
25
26 @classmethod
27 def _encrypt(cls, dfsid):
28 salt_bytes = bytearray(cls._NETEASE_SALT.encode('utf-8'))
29 string_bytes = bytearray(compat_str(dfsid).encode('ascii'))
30 salt_len = len(salt_bytes)
31 for i in range(len(string_bytes)):
32 string_bytes[i] = string_bytes[i] ^ salt_bytes[i % salt_len]
33 m = md5()
34 m.update(bytes(string_bytes))
35 result = b64encode(m.digest()).decode('ascii')
36 return result.replace('/', '_').replace('+', '-')
37
38 def extract_formats(self, info):
39 formats = []
40 for song_format in self._FORMATS:
41 details = info.get(song_format)
42 if not details:
43 continue
44 song_file_path = '/%s/%s.%s' % (
45 self._encrypt(details['dfsId']), details['dfsId'], details['extension'])
46
47 # 203.130.59.9, 124.40.233.182, 115.231.74.139, etc is a reverse proxy-like feature
48 # from NetEase's CDN provider that can be used if m5.music.126.net does not
49 # work, especially for users outside of Mainland China
50 # via: https://github.com/JixunMoe/unblock-163/issues/3#issuecomment-163115880
51 for host in ('http://m5.music.126.net', 'http://115.231.74.139/m1.music.126.net',
52 'http://124.40.233.182/m1.music.126.net', 'http://203.130.59.9/m1.music.126.net'):
53 song_url = host + song_file_path
54 if self._is_valid_url(song_url, info['id'], 'song'):
55 formats.append({
56 'url': song_url,
57 'ext': details.get('extension'),
58 'abr': float_or_none(details.get('bitrate'), scale=1000),
59 'format_id': song_format,
60 'filesize': details.get('size'),
61 'asr': details.get('sr')
62 })
63 break
64 return formats
65
66 @classmethod
67 def convert_milliseconds(cls, ms):
68 return int(round(ms / 1000.0))
69
70 def query_api(self, endpoint, video_id, note):
71 req = sanitized_Request('%s%s' % (self._API_BASE, endpoint))
72 req.add_header('Referer', self._API_BASE)
73 return self._download_json(req, video_id, note)
74
75
76 class NetEaseMusicIE(NetEaseMusicBaseIE):
77 IE_NAME = 'netease:song'
78 IE_DESC = '网易云音乐'
79 _VALID_URL = r'https?://music\.163\.com/(#/)?song\?id=(?P<id>[0-9]+)'
80 _TESTS = [{
81 'url': 'http://music.163.com/#/song?id=32102397',
82 'md5': 'f2e97280e6345c74ba9d5677dd5dcb45',
83 'info_dict': {
84 'id': '32102397',
85 'ext': 'mp3',
86 'title': 'Bad Blood (feat. Kendrick Lamar)',
87 'creator': 'Taylor Swift / Kendrick Lamar',
88 'upload_date': '20150517',
89 'timestamp': 1431878400,
90 'description': 'md5:a10a54589c2860300d02e1de821eb2ef',
91 },
92 }, {
93 'note': 'No lyrics translation.',
94 'url': 'http://music.163.com/#/song?id=29822014',
95 'info_dict': {
96 'id': '29822014',
97 'ext': 'mp3',
98 'title': '听见下雨的声音',
99 'creator': '周杰伦',
100 'upload_date': '20141225',
101 'timestamp': 1419523200,
102 'description': 'md5:a4d8d89f44656af206b7b2555c0bce6c',
103 },
104 }, {
105 'note': 'No lyrics.',
106 'url': 'http://music.163.com/song?id=17241424',
107 'info_dict': {
108 'id': '17241424',
109 'ext': 'mp3',
110 'title': 'Opus 28',
111 'creator': 'Dustin O\'Halloran',
112 'upload_date': '20080211',
113 'timestamp': 1202745600,
114 },
115 }, {
116 'note': 'Has translated name.',
117 'url': 'http://music.163.com/#/song?id=22735043',
118 'info_dict': {
119 'id': '22735043',
120 'ext': 'mp3',
121 'title': '소원을 말해봐 (Genie)',
122 'creator': '少女时代',
123 'description': 'md5:79d99cc560e4ca97e0c4d86800ee4184',
124 'upload_date': '20100127',
125 'timestamp': 1264608000,
126 'alt_title': '说出愿望吧(Genie)',
127 }
128 }]
129
130 def _process_lyrics(self, lyrics_info):
131 original = lyrics_info.get('lrc', {}).get('lyric')
132 translated = lyrics_info.get('tlyric', {}).get('lyric')
133
134 if not translated:
135 return original
136
137 lyrics_expr = r'(\[[0-9]{2}:[0-9]{2}\.[0-9]{2,}\])([^\n]+)'
138 original_ts_texts = re.findall(lyrics_expr, original)
139 translation_ts_dict = dict(
140 (time_stamp, text) for time_stamp, text in re.findall(lyrics_expr, translated)
141 )
142 lyrics = '\n'.join([
143 '%s%s / %s' % (time_stamp, text, translation_ts_dict.get(time_stamp, ''))
144 for time_stamp, text in original_ts_texts
145 ])
146 return lyrics
147
148 def _real_extract(self, url):
149 song_id = self._match_id(url)
150
151 params = {
152 'id': song_id,
153 'ids': '[%s]' % song_id
154 }
155 info = self.query_api(
156 'song/detail?' + compat_urllib_parse.urlencode(params),
157 song_id, 'Downloading song info')['songs'][0]
158
159 formats = self.extract_formats(info)
160 self._sort_formats(formats)
161
162 lyrics_info = self.query_api(
163 'song/lyric?id=%s&lv=-1&tv=-1' % song_id,
164 song_id, 'Downloading lyrics data')
165 lyrics = self._process_lyrics(lyrics_info)
166
167 alt_title = None
168 if info.get('transNames'):
169 alt_title = '/'.join(info.get('transNames'))
170
171 return {
172 'id': song_id,
173 'title': info['name'],
174 'alt_title': alt_title,
175 'creator': ' / '.join([artist['name'] for artist in info.get('artists', [])]),
176 'timestamp': self.convert_milliseconds(info.get('album', {}).get('publishTime')),
177 'thumbnail': info.get('album', {}).get('picUrl'),
178 'duration': self.convert_milliseconds(info.get('duration', 0)),
179 'description': lyrics,
180 'formats': formats,
181 }
182
183
184 class NetEaseMusicAlbumIE(NetEaseMusicBaseIE):
185 IE_NAME = 'netease:album'
186 IE_DESC = '网易云音乐 - 专辑'
187 _VALID_URL = r'https?://music\.163\.com/(#/)?album\?id=(?P<id>[0-9]+)'
188 _TEST = {
189 'url': 'http://music.163.com/#/album?id=220780',
190 'info_dict': {
191 'id': '220780',
192 'title': 'B\'day',
193 },
194 'playlist_count': 23,
195 }
196
197 def _real_extract(self, url):
198 album_id = self._match_id(url)
199
200 info = self.query_api(
201 'album/%s?id=%s' % (album_id, album_id),
202 album_id, 'Downloading album data')['album']
203
204 name = info['name']
205 desc = info.get('description')
206 entries = [
207 self.url_result('http://music.163.com/#/song?id=%s' % song['id'],
208 'NetEaseMusic', song['id'])
209 for song in info['songs']
210 ]
211 return self.playlist_result(entries, album_id, name, desc)
212
213
214 class NetEaseMusicSingerIE(NetEaseMusicBaseIE):
215 IE_NAME = 'netease:singer'
216 IE_DESC = '网易云音乐 - 歌手'
217 _VALID_URL = r'https?://music\.163\.com/(#/)?artist\?id=(?P<id>[0-9]+)'
218 _TESTS = [{
219 'note': 'Singer has aliases.',
220 'url': 'http://music.163.com/#/artist?id=10559',
221 'info_dict': {
222 'id': '10559',
223 'title': '张惠妹 - aMEI;阿密特',
224 },
225 'playlist_count': 50,
226 }, {
227 'note': 'Singer has translated name.',
228 'url': 'http://music.163.com/#/artist?id=124098',
229 'info_dict': {
230 'id': '124098',
231 'title': '李昇基 - 이승기',
232 },
233 'playlist_count': 50,
234 }]
235
236 def _real_extract(self, url):
237 singer_id = self._match_id(url)
238
239 info = self.query_api(
240 'artist/%s?id=%s' % (singer_id, singer_id),
241 singer_id, 'Downloading singer data')
242
243 name = info['artist']['name']
244 if info['artist']['trans']:
245 name = '%s - %s' % (name, info['artist']['trans'])
246 if info['artist']['alias']:
247 name = '%s - %s' % (name, ';'.join(info['artist']['alias']))
248
249 entries = [
250 self.url_result('http://music.163.com/#/song?id=%s' % song['id'],
251 'NetEaseMusic', song['id'])
252 for song in info['hotSongs']
253 ]
254 return self.playlist_result(entries, singer_id, name)
255
256
257 class NetEaseMusicListIE(NetEaseMusicBaseIE):
258 IE_NAME = 'netease:playlist'
259 IE_DESC = '网易云音乐 - 歌单'
260 _VALID_URL = r'https?://music\.163\.com/(#/)?(playlist|discover/toplist)\?id=(?P<id>[0-9]+)'
261 _TESTS = [{
262 'url': 'http://music.163.com/#/playlist?id=79177352',
263 'info_dict': {
264 'id': '79177352',
265 'title': 'Billboard 2007 Top 100',
266 'description': 'md5:12fd0819cab2965b9583ace0f8b7b022'
267 },
268 'playlist_count': 99,
269 }, {
270 'note': 'Toplist/Charts sample',
271 'url': 'http://music.163.com/#/discover/toplist?id=3733003',
272 'info_dict': {
273 'id': '3733003',
274 'title': 're:韩国Melon排行榜周榜 [0-9]{4}-[0-9]{2}-[0-9]{2}',
275 'description': 'md5:73ec782a612711cadc7872d9c1e134fc',
276 },
277 'playlist_count': 50,
278 }]
279
280 def _real_extract(self, url):
281 list_id = self._match_id(url)
282
283 info = self.query_api(
284 'playlist/detail?id=%s&lv=-1&tv=-1' % list_id,
285 list_id, 'Downloading playlist data')['result']
286
287 name = info['name']
288 desc = info.get('description')
289
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)
294
295 entries = [
296 self.url_result('http://music.163.com/#/song?id=%s' % song['id'],
297 'NetEaseMusic', song['id'])
298 for song in info['tracks']
299 ]
300 return self.playlist_result(entries, list_id, name, desc)
301
302
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]+)'
307 _TEST = {
308 'url': 'http://music.163.com/#/mv?id=415350',
309 'info_dict': {
310 'id': '415350',
311 'ext': 'mp4',
312 'title': '이럴거면 그러지말지',
313 'description': '白雅言自作曲唱甜蜜爱情',
314 'creator': '白雅言',
315 'upload_date': '20150520',
316 },
317 }
318
319 def _real_extract(self, url):
320 mv_id = self._match_id(url)
321
322 info = self.query_api(
323 'mv/detail?id=%s&type=mp4' % mv_id,
324 mv_id, 'Downloading mv info')['data']
325
326 formats = [
327 {'url': mv_url, 'ext': 'mp4', 'format_id': '%sp' % brs, 'height': int(brs)}
328 for brs, mv_url in info['brs'].items()
329 ]
330 self._sort_formats(formats)
331
332 return {
333 'id': mv_id,
334 'title': info['name'],
335 'description': info.get('desc') or info.get('briefDesc'),
336 'creator': info['artistName'],
337 'upload_date': info['publishTime'].replace('-', ''),
338 'formats': formats,
339 'thumbnail': info.get('cover'),
340 'duration': self.convert_milliseconds(info.get('duration', 0)),
341 }
342
343
344 class NetEaseMusicProgramIE(NetEaseMusicBaseIE):
345 IE_NAME = 'netease:program'
346 IE_DESC = '网易云音乐 - 电台节目'
347 _VALID_URL = r'https?://music\.163\.com/(#/?)program\?id=(?P<id>[0-9]+)'
348 _TESTS = [{
349 'url': 'http://music.163.com/#/program?id=10109055',
350 'info_dict': {
351 'id': '10109055',
352 'ext': 'mp3',
353 'title': '不丹足球背后的故事',
354 'description': '喜马拉雅人的足球梦 ...',
355 'creator': '大话西藏',
356 'timestamp': 1434179342,
357 'upload_date': '20150613',
358 'duration': 900,
359 },
360 }, {
361 'note': 'This program has accompanying songs.',
362 'url': 'http://music.163.com/#/program?id=10141022',
363 'info_dict': {
364 'id': '10141022',
365 'title': '25岁,你是自在如风的少年<27°C>',
366 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
367 },
368 'playlist_count': 4,
369 }, {
370 'note': 'This program has accompanying songs.',
371 'url': 'http://music.163.com/#/program?id=10141022',
372 'info_dict': {
373 'id': '10141022',
374 'ext': 'mp3',
375 'title': '25岁,你是自在如风的少年<27°C>',
376 'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b',
377 'timestamp': 1434450841,
378 'upload_date': '20150616',
379 },
380 'params': {
381 'noplaylist': True
382 }
383 }]
384
385 def _real_extract(self, url):
386 program_id = self._match_id(url)
387
388 info = self.query_api(
389 'dj/program/detail?id=%s' % program_id,
390 program_id, 'Downloading program info')['program']
391
392 name = info['name']
393 description = info['description']
394
395 if not info['songs'] or self._downloader.params.get('noplaylist'):
396 if info['songs']:
397 self.to_screen(
398 'Downloading just the main audio %s because of --no-playlist'
399 % info['mainSong']['id'])
400
401 formats = self.extract_formats(info['mainSong'])
402 self._sort_formats(formats)
403
404 return {
405 'id': program_id,
406 'title': name,
407 'description': description,
408 'creator': info['dj']['brand'],
409 'timestamp': self.convert_milliseconds(info['createTime']),
410 'thumbnail': info['coverUrl'],
411 'duration': self.convert_milliseconds(info.get('duration', 0)),
412 'formats': formats,
413 }
414
415 self.to_screen(
416 'Downloading playlist %s - add --no-playlist to just download the main audio %s'
417 % (program_id, info['mainSong']['id']))
418
419 song_ids = [info['mainSong']['id']]
420 song_ids.extend([song['id'] for song in info['songs']])
421 entries = [
422 self.url_result('http://music.163.com/#/song?id=%s' % song_id,
423 'NetEaseMusic', song_id)
424 for song_id in song_ids
425 ]
426 return self.playlist_result(entries, program_id, name, description)
427
428
429 class NetEaseMusicDjRadioIE(NetEaseMusicBaseIE):
430 IE_NAME = 'netease:djradio'
431 IE_DESC = '网易云音乐 - 电台'
432 _VALID_URL = r'https?://music\.163\.com/(#/)?djradio\?id=(?P<id>[0-9]+)'
433 _TEST = {
434 'url': 'http://music.163.com/#/djradio?id=42',
435 'info_dict': {
436 'id': '42',
437 'title': '声音蔓延',
438 'description': 'md5:766220985cbd16fdd552f64c578a6b15'
439 },
440 'playlist_mincount': 40,
441 }
442 _PAGE_SIZE = 1000
443
444 def _real_extract(self, url):
445 dj_id = self._match_id(url)
446
447 name = None
448 desc = None
449 entries = []
450 for offset in compat_itertools_count(start=0, step=self._PAGE_SIZE):
451 info = self.query_api(
452 'dj/program/byradio?asc=false&limit=%d&radioId=%s&offset=%d'
453 % (self._PAGE_SIZE, dj_id, offset),
454 dj_id, 'Downloading dj programs - %d' % offset)
455
456 entries.extend([
457 self.url_result(
458 'http://music.163.com/#/program?id=%s' % program['id'],
459 'NetEaseMusicProgram', program['id'])
460 for program in info['programs']
461 ])
462
463 if name is None:
464 radio = info['programs'][0]['radio']
465 name = radio['name']
466 desc = radio['desc']
467
468 if not info['more']:
469 break
470
471 return self.playlist_result(entries, dj_id, name, desc)