2 from __future__ import unicode_literals
7 from .common import InfoExtractor
19 class YandexMusicTrackIE(InfoExtractor):
20 IE_NAME = 'yandexmusic:track'
21 IE_DESC = 'Яндекс.Музыка - Трек'
22 _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
25 'url': 'http://music.yandex.ru/album/540508/track/4878838',
26 'md5': 'f496818aa2f60b6c0062980d2e00dc20',
30 'title': 'Carlo Ambrosio - Gypsy Eyes 1',
36 def _get_track_url(self, storage_dir, track_id):
37 data = self._download_json(
38 'http://music.yandex.ru/api/v1.5/handlers/api-jsonp.jsx?action=getTrackSrc&p=download-info/%s'
40 track_id, 'Downloading track location JSON')
42 key = hashlib.md5(('XGRlBW9FXlekgbPrRHuSiA' + data['path'][1:] + data['s']).encode('utf-8')).hexdigest()
43 storage = storage_dir.split('.')
45 return ('http://%s/get-mp3/%s/%s?track-id=%s&from=service-10-track&similarities-experiment=default'
46 % (data['host'], key, data['ts'] + data['path'], storage[1]))
48 def _get_track_info(self, track):
50 cover_uri = track.get('albums', [{}])[0].get('coverUri')
52 thumbnail = cover_uri.replace('%%', 'orig')
53 if not thumbnail.startswith('http'):
54 thumbnail = 'http://' + thumbnail
58 'url': self._get_track_url(track['storageDir'], track['id']),
59 'title': '%s - %s' % (track['artists'][0]['name'], track['title']),
60 'filesize': int_or_none(track.get('fileSize')),
61 'duration': float_or_none(track.get('durationMs'), 1000),
62 'thumbnail': thumbnail,
65 def _real_extract(self, url):
66 mobj = re.match(self._VALID_URL, url)
67 album_id, track_id = mobj.group('album_id'), mobj.group('id')
69 track = self._download_json(
70 'http://music.yandex.ru/handlers/track.jsx?track=%s:%s' % (track_id, album_id),
71 track_id, 'Downloading track JSON')['track']
73 return self._get_track_info(track)
76 class YandexMusicPlaylistBaseIE(InfoExtractor):
77 def _build_playlist(self, tracks):
80 'http://music.yandex.ru/album/%s/track/%s' % (track['albums'][0]['id'], track['id']))
81 for track in tracks if track.get('albums') and isinstance(track.get('albums'), list)]
84 class YandexMusicAlbumIE(YandexMusicPlaylistBaseIE):
85 IE_NAME = 'yandexmusic:album'
86 IE_DESC = 'Яндекс.Музыка - Альбом'
87 _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<id>\d+)/?(\?|$)'
90 'url': 'http://music.yandex.ru/album/540508',
93 'title': 'Carlo Ambrosio - Gypsy Soul (2009)',
98 def _real_extract(self, url):
99 album_id = self._match_id(url)
101 album = self._download_json(
102 'http://music.yandex.ru/handlers/album.jsx?album=%s' % album_id,
103 album_id, 'Downloading album JSON')
105 entries = self._build_playlist(album['volumes'][0])
107 title = '%s - %s' % (album['artists'][0]['name'], album['title'])
108 year = album.get('year')
110 title += ' (%s)' % year
112 return self.playlist_result(entries, compat_str(album['id']), title)
115 class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE):
116 IE_NAME = 'yandexmusic:playlist'
117 IE_DESC = 'Яндекс.Музыка - Плейлист'
118 _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/users/[^/]+/playlists/(?P<id>\d+)'
121 'url': 'http://music.yandex.ru/users/music.partners/playlists/1245',
124 'title': 'Что слушают Enter Shikari',
125 'description': 'md5:3b9f27b0efbe53f2ee1e844d07155cc9',
129 # playlist exceeding the limit of 150 tracks shipped with webpage (see
130 # https://github.com/rg3/youtube-dl/issues/6666)
131 'url': 'https://music.yandex.ru/users/ya.playlist/playlists/1036',
134 'title': 'Музыка 90-х',
136 'playlist_count': 310,
139 def _real_extract(self, url):
140 playlist_id = self._match_id(url)
142 webpage = self._download_webpage(url, playlist_id)
144 mu = self._parse_json(
146 r'var\s+Mu\s*=\s*({.+?});\s*</script>', webpage, 'player'),
149 playlist = mu['pageData']['playlist']
150 tracks, track_ids = playlist['tracks'], playlist['trackIds']
152 # tracks dictionary shipped with webpage is limited to 150 tracks,
153 # missing tracks should be retrieved manually.
154 if len(tracks) < len(track_ids):
155 present_track_ids = set([compat_str(track['id']) for track in tracks if track.get('id')])
156 missing_track_ids = set(map(compat_str, track_ids)) - set(present_track_ids)
157 request = sanitized_Request(
158 'https://music.yandex.ru/handlers/track-entries.jsx',
159 compat_urllib_parse.urlencode({
160 'entries': ','.join(missing_track_ids),
161 'lang': mu.get('settings', {}).get('lang', 'en'),
162 'external-domain': 'music.yandex.ru',
163 'overembed': 'false',
164 'sign': mu.get('authData', {}).get('user', {}).get('sign'),
167 request.add_header('Referer', url)
168 request.add_header('X-Requested-With', 'XMLHttpRequest')
170 missing_tracks = self._download_json(
171 request, playlist_id, 'Downloading missing tracks JSON', fatal=False)
173 tracks.extend(missing_tracks)
175 return self.playlist_result(
176 self._build_playlist(tracks),
177 compat_str(playlist_id),
178 playlist['title'], playlist.get('description'))