]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/neteasemusic.py
7830616f8fb9498c058504d52c199c6a69a9d98b
   2 from __future__ 
import unicode_literals
 
   4 from hashlib 
import md5
 
   5 from base64 
import b64encode
 
   6 from datetime 
import datetime
 
   9 from .common 
import InfoExtractor
 
  10 from ..compat 
import ( 
  13     compat_itertools_count
, 
  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/' 
  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
] 
  34         m
.update(bytes(string_bytes
)) 
  35         result 
= b64encode(m
.digest()).decode('ascii') 
  36         return result
.replace('/', '_').replace('+', '-') 
  38     def extract_formats(self
, info
): 
  40         for song_format 
in self
._FORMATS
: 
  41             details 
= info
.get(song_format
) 
  44             song_file_path 
= '/%s/%s.%s' % ( 
  45                 self
._encrypt
(details
['dfsId']), details
['dfsId'], details
['extension']) 
  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'): 
  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') 
  67     def convert_milliseconds(cls
, ms
): 
  68         return int(round(ms 
/ 1000.0)) 
  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
) 
  76 class NetEaseMusicIE(NetEaseMusicBaseIE
): 
  77     IE_NAME 
= 'netease:song' 
  79     _VALID_URL 
= r
'https?://music\.163\.com/(#/)?song\?id=(?P<id>[0-9]+)' 
  81         'url': 'http://music.163.com/#/song?id=32102397', 
  82         'md5': 'f2e97280e6345c74ba9d5677dd5dcb45', 
  86             'title': 'Bad Blood (feat. Kendrick Lamar)', 
  87             'creator': 'Taylor Swift / Kendrick Lamar', 
  88             'upload_date': '20150517', 
  89             'timestamp': 1431878400, 
  90             'description': 'md5:a10a54589c2860300d02e1de821eb2ef', 
  93         'note': 'No lyrics translation.', 
  94         'url': 'http://music.163.com/#/song?id=29822014', 
 100             'upload_date': '20141225', 
 101             'timestamp': 1419523200, 
 102             'description': 'md5:a4d8d89f44656af206b7b2555c0bce6c', 
 105         'note': 'No lyrics.', 
 106         'url': 'http://music.163.com/song?id=17241424', 
 111             'creator': 'Dustin O\'Halloran', 
 112             'upload_date': '20080211', 
 113             'timestamp': 1202745600, 
 116         'note': 'Has translated name.', 
 117         'url': 'http://music.163.com/#/song?id=22735043', 
 121             'title': '소원을 말해봐 (Genie)', 
 123             'description': 'md5:79d99cc560e4ca97e0c4d86800ee4184', 
 124             'upload_date': '20100127', 
 125             'timestamp': 1264608000, 
 126             'alt_title': '说出愿望吧(Genie)', 
 130     def _process_lyrics(self
, lyrics_info
): 
 131         original 
= lyrics_info
.get('lrc', {}).get('lyric') 
 132         translated 
= lyrics_info
.get('tlyric', {}).get('lyric') 
 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
) 
 143             '%s%s / %s' % (time_stamp
, text
, translation_ts_dict
.get(time_stamp
, '')) 
 144             for time_stamp
, text 
in original_ts_texts
 
 148     def _real_extract(self
, url
): 
 149         song_id 
= self
._match
_id
(url
) 
 153             'ids': '[%s]' % song_id
 
 155         info 
= self
.query_api( 
 156             'song/detail?' + compat_urllib_parse
.urlencode(params
), 
 157             song_id
, 'Downloading song info')['songs'][0] 
 159         formats 
= self
.extract_formats(info
) 
 160         self
._sort
_formats
(formats
) 
 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
) 
 168         if info
.get('transNames'): 
 169             alt_title 
= '/'.join(info
.get('transNames')) 
 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
, 
 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]+)' 
 189         'url': 'http://music.163.com/#/album?id=220780', 
 194         'playlist_count': 23, 
 197     def _real_extract(self
, url
): 
 198         album_id 
= self
._match
_id
(url
) 
 200         info 
= self
.query_api( 
 201             'album/%s?id=%s' % (album_id
, album_id
), 
 202             album_id
, 'Downloading album data')['album'] 
 205         desc 
= info
.get('description') 
 207             self
.url_result('http://music.163.com/#/song?id=%s' % song
['id'], 
 208                             'NetEaseMusic', song
['id']) 
 209             for song 
in info
['songs'] 
 211         return self
.playlist_result(entries
, album_id
, name
, desc
) 
 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]+)' 
 219         'note': 'Singer has aliases.', 
 220         'url': 'http://music.163.com/#/artist?id=10559', 
 223             'title': '张惠妹 - aMEI;阿密特', 
 225         'playlist_count': 50, 
 227         'note': 'Singer has translated name.', 
 228         'url': 'http://music.163.com/#/artist?id=124098', 
 231             'title': '李昇基 - 이승기', 
 233         'playlist_count': 50, 
 236     def _real_extract(self
, url
): 
 237         singer_id 
= self
._match
_id
(url
) 
 239         info 
= self
.query_api( 
 240             'artist/%s?id=%s' % (singer_id
, singer_id
), 
 241             singer_id
, 'Downloading singer data') 
 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'])) 
 250             self
.url_result('http://music.163.com/#/song?id=%s' % song
['id'], 
 251                             'NetEaseMusic', song
['id']) 
 252             for song 
in info
['hotSongs'] 
 254         return self
.playlist_result(entries
, singer_id
, name
) 
 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]+)' 
 262         'url': 'http://music.163.com/#/playlist?id=79177352', 
 265             'title': 'Billboard 2007 Top 100', 
 266             'description': 'md5:12fd0819cab2965b9583ace0f8b7b022' 
 268         'playlist_count': 99, 
 270         'note': 'Toplist/Charts sample', 
 271         'url': 'http://music.163.com/#/discover/toplist?id=3733003', 
 274             'title': 're:韩国Melon排行榜周榜 [0-9]{4}-[0-9]{2}-[0-9]{2}', 
 275             'description': 'md5:73ec782a612711cadc7872d9c1e134fc', 
 277         'playlist_count': 50, 
 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', 
 319     def _real_extract(self
, url
): 
 320         mv_id 
= self
._match
_id
(url
) 
 322         info 
= self
.query_api( 
 323             'mv/detail?id=%s&type=mp4' % mv_id
, 
 324             mv_id
, 'Downloading mv info')['data'] 
 327             {'url': mv_url
, 'ext': 'mp4', 'format_id': '%sp' % brs
, 'height': int(brs
)} 
 328             for brs
, mv_url 
in info
['brs'].items() 
 330         self
._sort
_formats
(formats
) 
 334             'title': info
['name'], 
 335             'description': info
.get('desc') or info
.get('briefDesc'), 
 336             'creator': info
['artistName'], 
 337             'upload_date': info
['publishTime'].replace('-', ''), 
 339             'thumbnail': info
.get('cover'), 
 340             'duration': self
.convert_milliseconds(info
.get('duration', 0)), 
 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]+)' 
 349         'url': 'http://music.163.com/#/program?id=10109055', 
 353             'title': '不丹足球背后的故事', 
 354             'description': '喜马拉雅人的足球梦 ...', 
 356             'timestamp': 1434179342, 
 357             'upload_date': '20150613', 
 361         'note': 'This program has accompanying songs.', 
 362         'url': 'http://music.163.com/#/program?id=10141022', 
 365             'title': '25岁,你是自在如风的少年<27°C>', 
 366             'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b', 
 370         'note': 'This program has accompanying songs.', 
 371         'url': 'http://music.163.com/#/program?id=10141022', 
 375             'title': '25岁,你是自在如风的少年<27°C>', 
 376             'description': 'md5:8d594db46cc3e6509107ede70a4aaa3b', 
 377             'timestamp': 1434450841, 
 378             'upload_date': '20150616', 
 385     def _real_extract(self
, url
): 
 386         program_id 
= self
._match
_id
(url
) 
 388         info 
= self
.query_api( 
 389             'dj/program/detail?id=%s' % program_id
, 
 390             program_id
, 'Downloading program info')['program'] 
 393         description 
= info
['description'] 
 395         if not info
['songs'] or self
._downloader
.params
.get('noplaylist'): 
 398                     'Downloading just the main audio %s because of --no-playlist' 
 399                     % info
['mainSong']['id']) 
 401             formats 
= self
.extract_formats(info
['mainSong']) 
 402             self
._sort
_formats
(formats
) 
 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)), 
 416             'Downloading playlist %s - add --no-playlist to just download the main audio %s' 
 417             % (program_id
, info
['mainSong']['id'])) 
 419         song_ids 
= [info
['mainSong']['id']] 
 420         song_ids
.extend([song
['id'] for song 
in info
['songs']]) 
 422             self
.url_result('http://music.163.com/#/song?id=%s' % song_id
, 
 423                             'NetEaseMusic', song_id
) 
 424             for song_id 
in song_ids
 
 426         return self
.playlist_result(entries
, program_id
, name
, description
) 
 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]+)' 
 434         'url': 'http://music.163.com/#/djradio?id=42', 
 438             'description': 'md5:766220985cbd16fdd552f64c578a6b15' 
 440         'playlist_mincount': 40, 
 444     def _real_extract(self
, url
): 
 445         dj_id 
= self
._match
_id
(url
) 
 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
) 
 458                     'http://music.163.com/#/program?id=%s' % program
['id'], 
 459                     'NetEaseMusicProgram', program
['id']) 
 460                 for program 
in info
['programs'] 
 464                 radio 
= info
['programs'][0]['radio'] 
 471         return self
.playlist_result(entries
, dj_id
, name
, desc
)