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