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