]>
Commit | Line | Data |
---|---|---|
4c603938 | 1 | import hashlib |
bc2ca1bb | 2 | import itertools |
4c603938 MA |
3 | |
4 | from .common import InfoExtractor | |
6e6bc8da | 5 | from ..compat import compat_str |
47fe42e1 | 6 | from ..utils import ( |
ae7d31af | 7 | ExtractorError, |
47fe42e1 S |
8 | int_or_none, |
9 | float_or_none, | |
0250161c | 10 | try_get, |
47fe42e1 | 11 | ) |
4c603938 | 12 | |
4c603938 | 13 | |
ae7d31af | 14 | class YandexMusicBaseIE(InfoExtractor): |
29f7c58a | 15 | _VALID_URL_BASE = r'https?://music\.yandex\.(?P<tld>ru|kz|ua|by|com)' |
16 | ||
ae7d31af S |
17 | @staticmethod |
18 | def _handle_error(response): | |
eebe6b38 S |
19 | if isinstance(response, dict): |
20 | error = response.get('error') | |
21 | if error: | |
22 | raise ExtractorError(error, expected=True) | |
197a5da1 S |
23 | if response.get('type') == 'captcha' or 'captcha' in response: |
24 | YandexMusicBaseIE._raise_captcha() | |
25 | ||
26 | @staticmethod | |
27 | def _raise_captcha(): | |
28 | raise ExtractorError( | |
7a5c1cfe | 29 | 'YandexMusic has considered yt-dlp requests automated and ' |
197a5da1 S |
30 | 'asks you to solve a CAPTCHA. You can either wait for some ' |
31 | 'time until unblocked and optionally use --sleep-interval ' | |
32 | 'in future or alternatively you can go to https://music.yandex.ru/ ' | |
33 | 'solve CAPTCHA, then export cookies and pass cookie file to ' | |
7a5c1cfe | 34 | 'yt-dlp with --cookies', |
197a5da1 | 35 | expected=True) |
ae7d31af | 36 | |
e5eadfa8 S |
37 | def _download_webpage_handle(self, *args, **kwargs): |
38 | webpage = super(YandexMusicBaseIE, self)._download_webpage_handle(*args, **kwargs) | |
7f776fa4 | 39 | if 'Нам очень жаль, но запросы, поступившие с вашего IP-адреса, похожи на автоматические.' in webpage: |
197a5da1 | 40 | self._raise_captcha() |
7f776fa4 YCH |
41 | return webpage |
42 | ||
ae7d31af S |
43 | def _download_json(self, *args, **kwargs): |
44 | response = super(YandexMusicBaseIE, self)._download_json(*args, **kwargs) | |
45 | self._handle_error(response) | |
46 | return response | |
47 | ||
29f7c58a | 48 | def _call_api(self, ep, tld, url, item_id, note, query): |
49 | return self._download_json( | |
50 | 'https://music.yandex.%s/handlers/%s.jsx' % (tld, ep), | |
51 | item_id, note, | |
52 | fatal=False, | |
53 | headers={ | |
54 | 'Referer': url, | |
55 | 'X-Requested-With': 'XMLHttpRequest', | |
56 | 'X-Retpath-Y': url, | |
57 | }, | |
58 | query=query) | |
59 | ||
ae7d31af S |
60 | |
61 | class YandexMusicTrackIE(YandexMusicBaseIE): | |
e4df2f98 S |
62 | IE_NAME = 'yandexmusic:track' |
63 | IE_DESC = 'Яндекс.Музыка - Трек' | |
29f7c58a | 64 | _VALID_URL = r'%s/album/(?P<album_id>\d+)/track/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE |
e4df2f98 | 65 | |
c452790a | 66 | _TESTS = [{ |
e4df2f98 | 67 | 'url': 'http://music.yandex.ru/album/540508/track/4878838', |
29f7c58a | 68 | 'md5': 'dec8b661f12027ceaba33318787fff76', |
e4df2f98 S |
69 | 'info_dict': { |
70 | 'id': '4878838', | |
71 | 'ext': 'mp3', | |
29f7c58a | 72 | 'title': 'md5:c63e19341fdbe84e43425a30bc777856', |
73 | 'filesize': int, | |
e4df2f98 | 74 | 'duration': 193.04, |
29f7c58a | 75 | 'track': 'md5:210508c6ffdfd67a493a6c378f22c3ff', |
76 | 'album': 'md5:cd04fb13c4efeafdfa0a6a6aca36d01a', | |
77 | 'album_artist': 'md5:5f54c35462c07952df33d97cfb5fc200', | |
78 | 'artist': 'md5:e6fd86621825f14dc0b25db3acd68160', | |
796bf9de | 79 | 'release_year': 2009, |
7f776fa4 | 80 | }, |
29f7c58a | 81 | # 'skip': 'Travis CI servers blocked by YandexMusic', |
c452790a | 82 | }, { |
83 | # multiple disks | |
84 | 'url': 'http://music.yandex.ru/album/3840501/track/705105', | |
29f7c58a | 85 | 'md5': '82a54e9e787301dd45aba093cf6e58c0', |
c452790a | 86 | 'info_dict': { |
87 | 'id': '705105', | |
88 | 'ext': 'mp3', | |
29f7c58a | 89 | 'title': 'md5:f86d4a9188279860a83000277024c1a6', |
90 | 'filesize': int, | |
c452790a | 91 | 'duration': 239.27, |
29f7c58a | 92 | 'track': 'md5:40f887f0666ba1aa10b835aca44807d1', |
93 | 'album': 'md5:624f5224b14f5c88a8e812fd7fbf1873', | |
94 | 'album_artist': 'md5:dd35f2af4e8927100cbe6f5e62e1fb12', | |
95 | 'artist': 'md5:dd35f2af4e8927100cbe6f5e62e1fb12', | |
c452790a | 96 | 'release_year': 2016, |
97 | 'genre': 'pop', | |
98 | 'disc_number': 2, | |
99 | 'track_number': 9, | |
100 | }, | |
29f7c58a | 101 | # 'skip': 'Travis CI servers blocked by YandexMusic', |
102 | }, { | |
103 | 'url': 'http://music.yandex.com/album/540508/track/4878838', | |
104 | 'only_matching': True, | |
c452790a | 105 | }] |
e4df2f98 | 106 | |
58ef5e78 | 107 | def _real_extract(self, url): |
5ad28e7f | 108 | mobj = self._match_valid_url(url) |
29f7c58a | 109 | tld, album_id, track_id = mobj.group('tld'), mobj.group('album_id'), mobj.group('id') |
4c603938 | 110 | |
29f7c58a | 111 | track = self._call_api( |
112 | 'track', tld, url, track_id, 'Downloading track JSON', | |
113 | {'track': '%s:%s' % (track_id, album_id)})['track'] | |
58ef5e78 | 114 | track_title = track['title'] |
cf03e34a | 115 | |
58ef5e78 RA |
116 | download_data = self._download_json( |
117 | 'https://music.yandex.ru/api/v2.1/handlers/track/%s:%s/web-album_track-track-track-main/download/m' % (track_id, album_id), | |
118 | track_id, 'Downloading track location url JSON', | |
119 | headers={'X-Retpath-Y': url}) | |
4c603938 | 120 | |
58ef5e78 RA |
121 | fd_data = self._download_json( |
122 | download_data['src'], track_id, | |
123 | 'Downloading track location JSON', | |
124 | query={'format': 'json'}) | |
125 | key = hashlib.md5(('XGRlBW9FXlekgbPrRHuSiA' + fd_data['path'][1:] + fd_data['s']).encode('utf-8')).hexdigest() | |
29f7c58a | 126 | f_url = 'http://%s/get-mp3/%s/%s?track-id=%s ' % (fd_data['host'], key, fd_data['ts'] + fd_data['path'], track['id']) |
4c603938 | 127 | |
ab953c64 S |
128 | thumbnail = None |
129 | cover_uri = track.get('albums', [{}])[0].get('coverUri') | |
130 | if cover_uri: | |
131 | thumbnail = cover_uri.replace('%%', 'orig') | |
132 | if not thumbnail.startswith('http'): | |
133 | thumbnail = 'http://' + thumbnail | |
e90d1754 | 134 | |
e90d1754 | 135 | track_info = { |
58ef5e78 | 136 | 'id': track_id, |
47fe42e1 | 137 | 'ext': 'mp3', |
58ef5e78 | 138 | 'url': f_url, |
47fe42e1 S |
139 | 'filesize': int_or_none(track.get('fileSize')), |
140 | 'duration': float_or_none(track.get('durationMs'), 1000), | |
ab953c64 | 141 | 'thumbnail': thumbnail, |
e90d1754 | 142 | 'track': track_title, |
58ef5e78 RA |
143 | 'acodec': download_data.get('codec'), |
144 | 'abr': int_or_none(download_data.get('bitrate')), | |
47fe42e1 | 145 | } |
4c603938 | 146 | |
c452790a | 147 | def extract_artist_name(artist): |
148 | decomposed = artist.get('decomposed') | |
149 | if not isinstance(decomposed, list): | |
150 | return artist['name'] | |
151 | parts = [artist['name']] | |
152 | for element in decomposed: | |
153 | if isinstance(element, dict) and element.get('name'): | |
154 | parts.append(element['name']) | |
155 | elif isinstance(element, compat_str): | |
156 | parts.append(element) | |
157 | return ''.join(parts) | |
158 | ||
e90d1754 S |
159 | def extract_artist(artist_list): |
160 | if artist_list and isinstance(artist_list, list): | |
c452790a | 161 | artists_names = [extract_artist_name(a) for a in artist_list if a.get('name')] |
e90d1754 S |
162 | if artists_names: |
163 | return ', '.join(artists_names) | |
164 | ||
165 | albums = track.get('albums') | |
166 | if albums and isinstance(albums, list): | |
167 | album = albums[0] | |
168 | if isinstance(album, dict): | |
169 | year = album.get('year') | |
c452790a | 170 | disc_number = int_or_none(try_get( |
171 | album, lambda x: x['trackPosition']['volume'])) | |
172 | track_number = int_or_none(try_get( | |
173 | album, lambda x: x['trackPosition']['index'])) | |
e90d1754 S |
174 | track_info.update({ |
175 | 'album': album.get('title'), | |
176 | 'album_artist': extract_artist(album.get('artists')), | |
796bf9de | 177 | 'release_year': int_or_none(year), |
c452790a | 178 | 'genre': album.get('genre'), |
179 | 'disc_number': disc_number, | |
180 | 'track_number': track_number, | |
e90d1754 S |
181 | }) |
182 | ||
183 | track_artist = extract_artist(track.get('artists')) | |
184 | if track_artist: | |
185 | track_info.update({ | |
186 | 'artist': track_artist, | |
187 | 'title': '%s - %s' % (track_artist, track_title), | |
188 | }) | |
189 | else: | |
190 | track_info['title'] = track_title | |
e90d1754 | 191 | |
58ef5e78 | 192 | return track_info |
4c603938 | 193 | |
4c603938 | 194 | |
ae7d31af | 195 | class YandexMusicPlaylistBaseIE(YandexMusicBaseIE): |
29f7c58a | 196 | def _extract_tracks(self, source, item_id, url, tld): |
197 | tracks = source['tracks'] | |
198 | track_ids = [compat_str(track_id) for track_id in source['trackIds']] | |
199 | ||
200 | # tracks dictionary shipped with playlist.jsx API is limited to 150 tracks, | |
201 | # missing tracks should be retrieved manually. | |
202 | if len(tracks) < len(track_ids): | |
203 | present_track_ids = set([ | |
204 | compat_str(track['id']) | |
205 | for track in tracks if track.get('id')]) | |
206 | missing_track_ids = [ | |
207 | track_id for track_id in track_ids | |
208 | if track_id not in present_track_ids] | |
bc2ca1bb | 209 | # Request missing tracks in chunks to avoid exceeding max HTTP header size, |
210 | # see https://github.com/ytdl-org/youtube-dl/issues/27355 | |
211 | _TRACKS_PER_CHUNK = 250 | |
212 | for chunk_num in itertools.count(0): | |
213 | start = chunk_num * _TRACKS_PER_CHUNK | |
214 | end = start + _TRACKS_PER_CHUNK | |
215 | missing_track_ids_req = missing_track_ids[start:end] | |
216 | assert missing_track_ids_req | |
217 | missing_tracks = self._call_api( | |
218 | 'track-entries', tld, url, item_id, | |
219 | 'Downloading missing tracks JSON chunk %d' % (chunk_num + 1), { | |
220 | 'entries': ','.join(missing_track_ids_req), | |
221 | 'lang': tld, | |
222 | 'external-domain': 'music.yandex.%s' % tld, | |
223 | 'overembed': 'false', | |
224 | 'strict': 'true', | |
225 | }) | |
226 | if missing_tracks: | |
227 | tracks.extend(missing_tracks) | |
228 | if end >= len(missing_track_ids): | |
229 | break | |
29f7c58a | 230 | |
231 | return tracks | |
232 | ||
e7c14660 | 233 | def _build_playlist(self, tracks): |
29f7c58a | 234 | entries = [] |
235 | for track in tracks: | |
236 | track_id = track.get('id') or track.get('realId') | |
237 | if not track_id: | |
238 | continue | |
239 | albums = track.get('albums') | |
240 | if not albums or not isinstance(albums, list): | |
241 | continue | |
242 | album = albums[0] | |
243 | if not isinstance(album, dict): | |
244 | continue | |
245 | album_id = album.get('id') | |
246 | if not album_id: | |
247 | continue | |
248 | entries.append(self.url_result( | |
249 | 'http://music.yandex.ru/album/%s/track/%s' % (album_id, track_id), | |
250 | ie=YandexMusicTrackIE.ie_key(), video_id=track_id)) | |
251 | return entries | |
e7c14660 S |
252 | |
253 | ||
254 | class YandexMusicAlbumIE(YandexMusicPlaylistBaseIE): | |
47fe42e1 S |
255 | IE_NAME = 'yandexmusic:album' |
256 | IE_DESC = 'Яндекс.Музыка - Альбом' | |
29f7c58a | 257 | _VALID_URL = r'%s/album/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE |
4c603938 | 258 | |
c452790a | 259 | _TESTS = [{ |
47fe42e1 S |
260 | 'url': 'http://music.yandex.ru/album/540508', |
261 | 'info_dict': { | |
262 | 'id': '540508', | |
29f7c58a | 263 | 'title': 'md5:7ed1c3567f28d14be9f61179116f5571', |
47fe42e1 S |
264 | }, |
265 | 'playlist_count': 50, | |
29f7c58a | 266 | # 'skip': 'Travis CI servers blocked by YandexMusic', |
c452790a | 267 | }, { |
268 | 'url': 'https://music.yandex.ru/album/3840501', | |
269 | 'info_dict': { | |
270 | 'id': '3840501', | |
29f7c58a | 271 | 'title': 'md5:36733472cdaa7dcb1fd9473f7da8e50f', |
c452790a | 272 | }, |
273 | 'playlist_count': 33, | |
29f7c58a | 274 | # 'skip': 'Travis CI servers blocked by YandexMusic', |
275 | }, { | |
276 | # empty artists | |
277 | 'url': 'https://music.yandex.ru/album/9091882', | |
278 | 'info_dict': { | |
279 | 'id': '9091882', | |
280 | 'title': 'ТЕД на русском', | |
281 | }, | |
282 | 'playlist_count': 187, | |
c452790a | 283 | }] |
4c603938 | 284 | |
29f7c58a | 285 | @classmethod |
286 | def suitable(cls, url): | |
287 | return False if YandexMusicTrackIE.suitable(url) else super(YandexMusicAlbumIE, cls).suitable(url) | |
288 | ||
4c603938 | 289 | def _real_extract(self, url): |
5ad28e7f | 290 | mobj = self._match_valid_url(url) |
29f7c58a | 291 | tld = mobj.group('tld') |
292 | album_id = mobj.group('id') | |
4c603938 | 293 | |
29f7c58a | 294 | album = self._call_api( |
295 | 'album', tld, url, album_id, 'Downloading album JSON', | |
296 | {'album': album_id}) | |
4c603938 | 297 | |
c452790a | 298 | entries = self._build_playlist([track for volume in album['volumes'] for track in volume]) |
47fe42e1 | 299 | |
29f7c58a | 300 | title = album['title'] |
301 | artist = try_get(album, lambda x: x['artists'][0]['name'], compat_str) | |
302 | if artist: | |
303 | title = '%s - %s' % (artist, title) | |
47fe42e1 S |
304 | year = album.get('year') |
305 | if year: | |
306 | title += ' (%s)' % year | |
307 | ||
308 | return self.playlist_result(entries, compat_str(album['id']), title) | |
4c603938 | 309 | |
4c603938 | 310 | |
e7c14660 | 311 | class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE): |
47fe42e1 S |
312 | IE_NAME = 'yandexmusic:playlist' |
313 | IE_DESC = 'Яндекс.Музыка - Плейлист' | |
29f7c58a | 314 | _VALID_URL = r'%s/users/(?P<user>[^/]+)/playlists/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE |
47fe42e1 | 315 | |
baf510bf | 316 | _TESTS = [{ |
47fe42e1 | 317 | 'url': 'http://music.yandex.ru/users/music.partners/playlists/1245', |
4c603938 | 318 | 'info_dict': { |
47fe42e1 | 319 | 'id': '1245', |
29f7c58a | 320 | 'title': 'md5:841559b3fe2b998eca88d0d2e22a3097', |
47fe42e1 S |
321 | 'description': 'md5:3b9f27b0efbe53f2ee1e844d07155cc9', |
322 | }, | |
29f7c58a | 323 | 'playlist_count': 5, |
324 | # 'skip': 'Travis CI servers blocked by YandexMusic', | |
baf510bf | 325 | }, { |
baf510bf | 326 | 'url': 'https://music.yandex.ru/users/ya.playlist/playlists/1036', |
29f7c58a | 327 | 'only_matching': True, |
328 | }, { | |
329 | # playlist exceeding the limit of 150 tracks (see | |
330 | # https://github.com/ytdl-org/youtube-dl/issues/6666) | |
331 | 'url': 'https://music.yandex.ru/users/mesiaz/playlists/1364', | |
baf510bf | 332 | 'info_dict': { |
29f7c58a | 333 | 'id': '1364', |
334 | 'title': 'md5:b3b400f997d3f878a13ae0699653f7db', | |
baf510bf | 335 | }, |
29f7c58a | 336 | 'playlist_mincount': 437, |
337 | # 'skip': 'Travis CI servers blocked by YandexMusic', | |
baf510bf | 338 | }] |
4c603938 MA |
339 | |
340 | def _real_extract(self, url): | |
5ad28e7f | 341 | mobj = self._match_valid_url(url) |
e960c3c2 S |
342 | tld = mobj.group('tld') |
343 | user = mobj.group('user') | |
344 | playlist_id = mobj.group('id') | |
345 | ||
29f7c58a | 346 | playlist = self._call_api( |
347 | 'playlist', tld, url, playlist_id, 'Downloading playlist JSON', { | |
e960c3c2 S |
348 | 'owner': user, |
349 | 'kinds': playlist_id, | |
350 | 'light': 'true', | |
351 | 'lang': tld, | |
352 | 'external-domain': 'music.yandex.%s' % tld, | |
353 | 'overembed': 'false', | |
354 | })['playlist'] | |
baf510bf | 355 | |
29f7c58a | 356 | tracks = self._extract_tracks(playlist, playlist_id, url, tld) |
47fe42e1 | 357 | |
47fe42e1 | 358 | return self.playlist_result( |
baf510bf | 359 | self._build_playlist(tracks), |
e7c14660 | 360 | compat_str(playlist_id), |
203a3c0e | 361 | playlist.get('title'), playlist.get('description')) |
29f7c58a | 362 | |
363 | ||
364 | class YandexMusicArtistBaseIE(YandexMusicPlaylistBaseIE): | |
365 | def _call_artist(self, tld, url, artist_id): | |
366 | return self._call_api( | |
367 | 'artist', tld, url, artist_id, | |
368 | 'Downloading artist %s JSON' % self._ARTIST_WHAT, { | |
369 | 'artist': artist_id, | |
370 | 'what': self._ARTIST_WHAT, | |
371 | 'sort': self._ARTIST_SORT or '', | |
372 | 'dir': '', | |
373 | 'period': '', | |
374 | 'lang': tld, | |
375 | 'external-domain': 'music.yandex.%s' % tld, | |
376 | 'overembed': 'false', | |
377 | }) | |
378 | ||
379 | def _real_extract(self, url): | |
5ad28e7f | 380 | mobj = self._match_valid_url(url) |
29f7c58a | 381 | tld = mobj.group('tld') |
382 | artist_id = mobj.group('id') | |
383 | data = self._call_artist(tld, url, artist_id) | |
384 | tracks = self._extract_tracks(data, artist_id, url, tld) | |
385 | title = try_get(data, lambda x: x['artist']['name'], compat_str) | |
386 | return self.playlist_result( | |
387 | self._build_playlist(tracks), artist_id, title) | |
388 | ||
389 | ||
390 | class YandexMusicArtistTracksIE(YandexMusicArtistBaseIE): | |
391 | IE_NAME = 'yandexmusic:artist:tracks' | |
392 | IE_DESC = 'Яндекс.Музыка - Артист - Треки' | |
393 | _VALID_URL = r'%s/artist/(?P<id>\d+)/tracks' % YandexMusicBaseIE._VALID_URL_BASE | |
394 | ||
395 | _TESTS = [{ | |
396 | 'url': 'https://music.yandex.ru/artist/617526/tracks', | |
397 | 'info_dict': { | |
398 | 'id': '617526', | |
399 | 'title': 'md5:131aef29d45fd5a965ca613e708c040b', | |
400 | }, | |
401 | 'playlist_count': 507, | |
402 | # 'skip': 'Travis CI servers blocked by YandexMusic', | |
403 | }] | |
404 | ||
405 | _ARTIST_SORT = '' | |
406 | _ARTIST_WHAT = 'tracks' | |
407 | ||
408 | def _real_extract(self, url): | |
5ad28e7f | 409 | mobj = self._match_valid_url(url) |
29f7c58a | 410 | tld = mobj.group('tld') |
411 | artist_id = mobj.group('id') | |
412 | data = self._call_artist(tld, url, artist_id) | |
413 | tracks = self._extract_tracks(data, artist_id, url, tld) | |
414 | artist = try_get(data, lambda x: x['artist']['name'], compat_str) | |
415 | title = '%s - %s' % (artist or artist_id, 'Треки') | |
416 | return self.playlist_result( | |
417 | self._build_playlist(tracks), artist_id, title) | |
418 | ||
419 | ||
420 | class YandexMusicArtistAlbumsIE(YandexMusicArtistBaseIE): | |
421 | IE_NAME = 'yandexmusic:artist:albums' | |
422 | IE_DESC = 'Яндекс.Музыка - Артист - Альбомы' | |
423 | _VALID_URL = r'%s/artist/(?P<id>\d+)/albums' % YandexMusicBaseIE._VALID_URL_BASE | |
424 | ||
425 | _TESTS = [{ | |
426 | 'url': 'https://music.yandex.ru/artist/617526/albums', | |
427 | 'info_dict': { | |
428 | 'id': '617526', | |
429 | 'title': 'md5:55dc58d5c85699b7fb41ee926700236c', | |
430 | }, | |
431 | 'playlist_count': 8, | |
432 | # 'skip': 'Travis CI servers blocked by YandexMusic', | |
433 | }] | |
434 | ||
435 | _ARTIST_SORT = 'year' | |
436 | _ARTIST_WHAT = 'albums' | |
437 | ||
438 | def _real_extract(self, url): | |
5ad28e7f | 439 | mobj = self._match_valid_url(url) |
29f7c58a | 440 | tld = mobj.group('tld') |
441 | artist_id = mobj.group('id') | |
442 | data = self._call_artist(tld, url, artist_id) | |
443 | entries = [] | |
444 | for album in data['albums']: | |
445 | if not isinstance(album, dict): | |
446 | continue | |
447 | album_id = album.get('id') | |
448 | if not album_id: | |
449 | continue | |
450 | entries.append(self.url_result( | |
451 | 'http://music.yandex.ru/album/%s' % album_id, | |
452 | ie=YandexMusicAlbumIE.ie_key(), video_id=album_id)) | |
453 | artist = try_get(data, lambda x: x['artist']['name'], compat_str) | |
454 | title = '%s - %s' % (artist or artist_id, 'Альбомы') | |
455 | return self.playlist_result(entries, artist_id, title) |