mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-11-06 23:27:00 +01:00
[yandexmusic] Refactor and add support for artist's tracks and albums (closes #11887, closes #22284)
This commit is contained in:
parent
d6ce649f15
commit
6da0e5e7a2
@ -1478,6 +1478,8 @@ from .yandexmusic import (
|
||||
YandexMusicTrackIE,
|
||||
YandexMusicAlbumIE,
|
||||
YandexMusicPlaylistIE,
|
||||
YandexMusicArtistTracksIE,
|
||||
YandexMusicArtistAlbumsIE,
|
||||
)
|
||||
from .yandexvideo import YandexVideoIE
|
||||
from .yapfiles import YapFilesIE
|
||||
|
@ -46,57 +46,69 @@ class YandexMusicBaseIE(InfoExtractor):
|
||||
self._handle_error(response)
|
||||
return response
|
||||
|
||||
def _call_api(self, ep, tld, url, item_id, note, query):
|
||||
return self._download_json(
|
||||
'https://music.yandex.%s/handlers/%s.jsx' % (tld, ep),
|
||||
item_id, note,
|
||||
fatal=False,
|
||||
headers={
|
||||
'Referer': url,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'X-Retpath-Y': url,
|
||||
},
|
||||
query=query)
|
||||
|
||||
|
||||
class YandexMusicTrackIE(YandexMusicBaseIE):
|
||||
IE_NAME = 'yandexmusic:track'
|
||||
IE_DESC = 'Яндекс.Музыка - Трек'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?P<tld>ru|kz|ua|by)/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
|
||||
|
||||
_TESTS = [{
|
||||
'url': 'http://music.yandex.ru/album/540508/track/4878838',
|
||||
'md5': 'f496818aa2f60b6c0062980d2e00dc20',
|
||||
'md5': 'dec8b661f12027ceaba33318787fff76',
|
||||
'info_dict': {
|
||||
'id': '4878838',
|
||||
'ext': 'mp3',
|
||||
'title': 'Carlo Ambrosio & Fabio Di Bari - Gypsy Eyes 1',
|
||||
'filesize': 4628061,
|
||||
'title': 'md5:c63e19341fdbe84e43425a30bc777856',
|
||||
'filesize': int,
|
||||
'duration': 193.04,
|
||||
'track': 'Gypsy Eyes 1',
|
||||
'album': 'Gypsy Soul',
|
||||
'album_artist': 'Carlo Ambrosio',
|
||||
'artist': 'Carlo Ambrosio & Fabio Di Bari',
|
||||
'track': 'md5:210508c6ffdfd67a493a6c378f22c3ff',
|
||||
'album': 'md5:cd04fb13c4efeafdfa0a6a6aca36d01a',
|
||||
'album_artist': 'md5:5f54c35462c07952df33d97cfb5fc200',
|
||||
'artist': 'md5:e6fd86621825f14dc0b25db3acd68160',
|
||||
'release_year': 2009,
|
||||
},
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}, {
|
||||
# multiple disks
|
||||
'url': 'http://music.yandex.ru/album/3840501/track/705105',
|
||||
'md5': 'ebe7b4e2ac7ac03fe11c19727ca6153e',
|
||||
'md5': '82a54e9e787301dd45aba093cf6e58c0',
|
||||
'info_dict': {
|
||||
'id': '705105',
|
||||
'ext': 'mp3',
|
||||
'title': 'Hooverphonic - Sometimes',
|
||||
'filesize': 5743386,
|
||||
'title': 'md5:f86d4a9188279860a83000277024c1a6',
|
||||
'filesize': int,
|
||||
'duration': 239.27,
|
||||
'track': 'Sometimes',
|
||||
'album': 'The Best of Hooverphonic',
|
||||
'album_artist': 'Hooverphonic',
|
||||
'artist': 'Hooverphonic',
|
||||
'track': 'md5:40f887f0666ba1aa10b835aca44807d1',
|
||||
'album': 'md5:624f5224b14f5c88a8e812fd7fbf1873',
|
||||
'album_artist': 'md5:dd35f2af4e8927100cbe6f5e62e1fb12',
|
||||
'artist': 'md5:dd35f2af4e8927100cbe6f5e62e1fb12',
|
||||
'release_year': 2016,
|
||||
'genre': 'pop',
|
||||
'disc_number': 2,
|
||||
'track_number': 9,
|
||||
},
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}]
|
||||
|
||||
def _real_extract(self, url):
|
||||
mobj = re.match(self._VALID_URL, url)
|
||||
album_id, track_id = mobj.group('album_id'), mobj.group('id')
|
||||
tld, album_id, track_id = mobj.group('tld'), mobj.group('album_id'), mobj.group('id')
|
||||
|
||||
track = self._download_json(
|
||||
'http://music.yandex.ru/handlers/track.jsx?track=%s:%s' % (track_id, album_id),
|
||||
track_id, 'Downloading track JSON')['track']
|
||||
track = self._call_api(
|
||||
'track', tld, url, track_id, 'Downloading track JSON',
|
||||
{'track': '%s:%s' % (track_id, album_id)})['track']
|
||||
track_title = track['title']
|
||||
|
||||
download_data = self._download_json(
|
||||
@ -179,42 +191,85 @@ class YandexMusicTrackIE(YandexMusicBaseIE):
|
||||
|
||||
|
||||
class YandexMusicPlaylistBaseIE(YandexMusicBaseIE):
|
||||
def _extract_tracks(self, source, item_id, url, tld):
|
||||
tracks = source['tracks']
|
||||
track_ids = [compat_str(track_id) for track_id in source['trackIds']]
|
||||
|
||||
# tracks dictionary shipped with playlist.jsx API is limited to 150 tracks,
|
||||
# missing tracks should be retrieved manually.
|
||||
if len(tracks) < len(track_ids):
|
||||
present_track_ids = set([
|
||||
compat_str(track['id'])
|
||||
for track in tracks if track.get('id')])
|
||||
missing_track_ids = [
|
||||
track_id for track_id in track_ids
|
||||
if track_id not in present_track_ids]
|
||||
missing_tracks = self._call_api(
|
||||
'track-entries', tld, url, item_id,
|
||||
'Downloading missing tracks JSON', {
|
||||
'entries': ','.join(missing_track_ids),
|
||||
'lang': tld,
|
||||
'external-domain': 'music.yandex.%s' % tld,
|
||||
'overembed': 'false',
|
||||
'strict': 'true',
|
||||
})
|
||||
if missing_tracks:
|
||||
tracks.extend(missing_tracks)
|
||||
|
||||
return tracks
|
||||
|
||||
def _build_playlist(self, tracks):
|
||||
return [
|
||||
self.url_result(
|
||||
'http://music.yandex.ru/album/%s/track/%s' % (track['albums'][0]['id'], track['id']))
|
||||
for track in tracks if track.get('albums') and isinstance(track.get('albums'), list)]
|
||||
entries = []
|
||||
for track in tracks:
|
||||
track_id = track.get('id') or track.get('realId')
|
||||
if not track_id:
|
||||
continue
|
||||
albums = track.get('albums')
|
||||
if not albums or not isinstance(albums, list):
|
||||
continue
|
||||
album = albums[0]
|
||||
if not isinstance(album, dict):
|
||||
continue
|
||||
album_id = album.get('id')
|
||||
if not album_id:
|
||||
continue
|
||||
entries.append(self.url_result(
|
||||
'http://music.yandex.ru/album/%s/track/%s' % (album_id, track_id),
|
||||
ie=YandexMusicTrackIE.ie_key(), video_id=track_id))
|
||||
return entries
|
||||
|
||||
|
||||
class YandexMusicAlbumIE(YandexMusicPlaylistBaseIE):
|
||||
IE_NAME = 'yandexmusic:album'
|
||||
IE_DESC = 'Яндекс.Музыка - Альбом'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<id>\d+)/?(\?|$)'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?P<tld>ru|kz|ua|by)/album/(?P<id>\d+)/?(\?|$)'
|
||||
|
||||
_TESTS = [{
|
||||
'url': 'http://music.yandex.ru/album/540508',
|
||||
'info_dict': {
|
||||
'id': '540508',
|
||||
'title': 'Carlo Ambrosio - Gypsy Soul (2009)',
|
||||
'title': 'md5:7ed1c3567f28d14be9f61179116f5571',
|
||||
},
|
||||
'playlist_count': 50,
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}, {
|
||||
'url': 'https://music.yandex.ru/album/3840501',
|
||||
'info_dict': {
|
||||
'id': '3840501',
|
||||
'title': 'Hooverphonic - The Best of Hooverphonic (2016)',
|
||||
'title': 'md5:36733472cdaa7dcb1fd9473f7da8e50f',
|
||||
},
|
||||
'playlist_count': 33,
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}]
|
||||
|
||||
def _real_extract(self, url):
|
||||
album_id = self._match_id(url)
|
||||
mobj = re.match(self._VALID_URL, url)
|
||||
tld = mobj.group('tld')
|
||||
album_id = mobj.group('id')
|
||||
|
||||
album = self._download_json(
|
||||
'http://music.yandex.ru/handlers/album.jsx?album=%s' % album_id,
|
||||
album_id, 'Downloading album JSON')
|
||||
album = self._call_api(
|
||||
'album', tld, url, album_id, 'Downloading album JSON',
|
||||
{'album': album_id})
|
||||
|
||||
entries = self._build_playlist([track for volume in album['volumes'] for track in volume])
|
||||
|
||||
@ -235,21 +290,24 @@ class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE):
|
||||
'url': 'http://music.yandex.ru/users/music.partners/playlists/1245',
|
||||
'info_dict': {
|
||||
'id': '1245',
|
||||
'title': 'Что слушают Enter Shikari',
|
||||
'title': 'md5:841559b3fe2b998eca88d0d2e22a3097',
|
||||
'description': 'md5:3b9f27b0efbe53f2ee1e844d07155cc9',
|
||||
},
|
||||
'playlist_count': 6,
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
'playlist_count': 5,
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}, {
|
||||
# playlist exceeding the limit of 150 tracks shipped with webpage (see
|
||||
# https://github.com/ytdl-org/youtube-dl/issues/6666)
|
||||
'url': 'https://music.yandex.ru/users/ya.playlist/playlists/1036',
|
||||
'only_matching': True,
|
||||
}, {
|
||||
# playlist exceeding the limit of 150 tracks (see
|
||||
# https://github.com/ytdl-org/youtube-dl/issues/6666)
|
||||
'url': 'https://music.yandex.ru/users/mesiaz/playlists/1364',
|
||||
'info_dict': {
|
||||
'id': '1036',
|
||||
'title': 'Музыка 90-х',
|
||||
'id': '1364',
|
||||
'title': 'md5:b3b400f997d3f878a13ae0699653f7db',
|
||||
},
|
||||
'playlist_mincount': 300,
|
||||
'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
'playlist_mincount': 437,
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}]
|
||||
|
||||
def _real_extract(self, url):
|
||||
@ -258,16 +316,8 @@ class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE):
|
||||
user = mobj.group('user')
|
||||
playlist_id = mobj.group('id')
|
||||
|
||||
playlist = self._download_json(
|
||||
'https://music.yandex.%s/handlers/playlist.jsx' % tld,
|
||||
playlist_id, 'Downloading missing tracks JSON',
|
||||
fatal=False,
|
||||
headers={
|
||||
'Referer': url,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'X-Retpath-Y': url,
|
||||
},
|
||||
query={
|
||||
playlist = self._call_api(
|
||||
'playlist', tld, url, playlist_id, 'Downloading playlist JSON', {
|
||||
'owner': user,
|
||||
'kinds': playlist_id,
|
||||
'light': 'true',
|
||||
@ -276,37 +326,103 @@ class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE):
|
||||
'overembed': 'false',
|
||||
})['playlist']
|
||||
|
||||
tracks = playlist['tracks']
|
||||
track_ids = [compat_str(track_id) for track_id in playlist['trackIds']]
|
||||
|
||||
# tracks dictionary shipped with playlist.jsx API is limited to 150 tracks,
|
||||
# missing tracks should be retrieved manually.
|
||||
if len(tracks) < len(track_ids):
|
||||
present_track_ids = set([
|
||||
compat_str(track['id'])
|
||||
for track in tracks if track.get('id')])
|
||||
missing_track_ids = [
|
||||
track_id for track_id in track_ids
|
||||
if track_id not in present_track_ids]
|
||||
missing_tracks = self._download_json(
|
||||
'https://music.yandex.%s/handlers/track-entries.jsx' % tld,
|
||||
playlist_id, 'Downloading missing tracks JSON',
|
||||
fatal=False,
|
||||
headers={
|
||||
'Referer': url,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
query={
|
||||
'entries': ','.join(missing_track_ids),
|
||||
'lang': tld,
|
||||
'external-domain': 'music.yandex.%s' % tld,
|
||||
'overembed': 'false',
|
||||
'strict': 'true',
|
||||
})
|
||||
if missing_tracks:
|
||||
tracks.extend(missing_tracks)
|
||||
tracks = self._extract_tracks(playlist, playlist_id, url, tld)
|
||||
|
||||
return self.playlist_result(
|
||||
self._build_playlist(tracks),
|
||||
compat_str(playlist_id),
|
||||
playlist.get('title'), playlist.get('description'))
|
||||
|
||||
|
||||
class YandexMusicArtistBaseIE(YandexMusicPlaylistBaseIE):
|
||||
def _call_artist(self, tld, url, artist_id):
|
||||
return self._call_api(
|
||||
'artist', tld, url, artist_id,
|
||||
'Downloading artist %s JSON' % self._ARTIST_WHAT, {
|
||||
'artist': artist_id,
|
||||
'what': self._ARTIST_WHAT,
|
||||
'sort': self._ARTIST_SORT or '',
|
||||
'dir': '',
|
||||
'period': '',
|
||||
'lang': tld,
|
||||
'external-domain': 'music.yandex.%s' % tld,
|
||||
'overembed': 'false',
|
||||
})
|
||||
|
||||
def _real_extract(self, url):
|
||||
mobj = re.match(self._VALID_URL, url)
|
||||
tld = mobj.group('tld')
|
||||
artist_id = mobj.group('id')
|
||||
data = self._call_artist(tld, url, artist_id)
|
||||
tracks = self._extract_tracks(data, artist_id, url, tld)
|
||||
title = try_get(data, lambda x: x['artist']['name'], compat_str)
|
||||
return self.playlist_result(
|
||||
self._build_playlist(tracks), artist_id, title)
|
||||
|
||||
|
||||
class YandexMusicArtistTracksIE(YandexMusicArtistBaseIE):
|
||||
IE_NAME = 'yandexmusic:artist:tracks'
|
||||
IE_DESC = 'Яндекс.Музыка - Артист - Треки'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?P<tld>ru|kz|ua|by)/artist/(?P<id>\d+)/tracks'
|
||||
|
||||
_TESTS = [{
|
||||
'url': 'https://music.yandex.ru/artist/617526/tracks',
|
||||
'info_dict': {
|
||||
'id': '617526',
|
||||
'title': 'md5:131aef29d45fd5a965ca613e708c040b',
|
||||
},
|
||||
'playlist_count': 507,
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}]
|
||||
|
||||
_ARTIST_SORT = ''
|
||||
_ARTIST_WHAT = 'tracks'
|
||||
|
||||
def _real_extract(self, url):
|
||||
mobj = re.match(self._VALID_URL, url)
|
||||
tld = mobj.group('tld')
|
||||
artist_id = mobj.group('id')
|
||||
data = self._call_artist(tld, url, artist_id)
|
||||
tracks = self._extract_tracks(data, artist_id, url, tld)
|
||||
artist = try_get(data, lambda x: x['artist']['name'], compat_str)
|
||||
title = '%s - %s' % (artist or artist_id, 'Треки')
|
||||
return self.playlist_result(
|
||||
self._build_playlist(tracks), artist_id, title)
|
||||
|
||||
|
||||
class YandexMusicArtistAlbumsIE(YandexMusicArtistBaseIE):
|
||||
IE_NAME = 'yandexmusic:artist:albums'
|
||||
IE_DESC = 'Яндекс.Музыка - Артист - Альбомы'
|
||||
_VALID_URL = r'https?://music\.yandex\.(?P<tld>ru|kz|ua|by)/artist/(?P<id>\d+)/albums'
|
||||
|
||||
_TESTS = [{
|
||||
'url': 'https://music.yandex.ru/artist/617526/albums',
|
||||
'info_dict': {
|
||||
'id': '617526',
|
||||
'title': 'md5:55dc58d5c85699b7fb41ee926700236c',
|
||||
},
|
||||
'playlist_count': 8,
|
||||
# 'skip': 'Travis CI servers blocked by YandexMusic',
|
||||
}]
|
||||
|
||||
_ARTIST_SORT = 'year'
|
||||
_ARTIST_WHAT = 'albums'
|
||||
|
||||
def _real_extract(self, url):
|
||||
mobj = re.match(self._VALID_URL, url)
|
||||
tld = mobj.group('tld')
|
||||
artist_id = mobj.group('id')
|
||||
data = self._call_artist(tld, url, artist_id)
|
||||
entries = []
|
||||
for album in data['albums']:
|
||||
if not isinstance(album, dict):
|
||||
continue
|
||||
album_id = album.get('id')
|
||||
if not album_id:
|
||||
continue
|
||||
entries.append(self.url_result(
|
||||
'http://music.yandex.ru/album/%s' % album_id,
|
||||
ie=YandexMusicAlbumIE.ie_key(), video_id=album_id))
|
||||
artist = try_get(data, lambda x: x['artist']['name'], compat_str)
|
||||
title = '%s - %s' % (artist or artist_id, 'Альбомы')
|
||||
return self.playlist_result(entries, artist_id, title)
|
||||
|
Loading…
Reference in New Issue
Block a user