-# coding: utf-8
-from __future__ import unicode_literals
-
import hashlib
import itertools
-import re
from .common import InfoExtractor
-from ..compat import compat_str
from ..utils import (
ExtractorError,
- int_or_none,
float_or_none,
+ int_or_none,
try_get,
)
expected=True)
def _download_webpage_handle(self, *args, **kwargs):
- webpage = super(YandexMusicBaseIE, self)._download_webpage_handle(*args, **kwargs)
+ webpage = super()._download_webpage_handle(*args, **kwargs)
if 'Нам очень жаль, но запросы, поступившие с вашего IP-адреса, похожи на автоматические.' in webpage:
self._raise_captcha()
return webpage
def _download_json(self, *args, **kwargs):
- response = super(YandexMusicBaseIE, self)._download_json(*args, **kwargs)
+ response = super()._download_json(*args, **kwargs)
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),
+ f'https://music.yandex.{tld}/handlers/{ep}.jsx',
item_id, note,
fatal=False,
headers={
class YandexMusicTrackIE(YandexMusicBaseIE):
IE_NAME = 'yandexmusic:track'
IE_DESC = 'Яндекс.Музыка - Трек'
- _VALID_URL = r'%s/album/(?P<album_id>\d+)/track/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE
+ _VALID_URL = rf'{YandexMusicBaseIE._VALID_URL_BASE}/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
_TESTS = [{
'url': 'http://music.yandex.ru/album/540508/track/4878838',
}]
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
tld, album_id, track_id = mobj.group('tld'), mobj.group('album_id'), mobj.group('id')
track = self._call_api(
'track', tld, url, track_id, 'Downloading track JSON',
- {'track': '%s:%s' % (track_id, album_id)})['track']
+ {'track': f'{track_id}:{album_id}'})['track']
track_title = track['title']
download_data = self._download_json(
- 'https://music.yandex.ru/api/v2.1/handlers/track/%s:%s/web-album_track-track-track-main/download/m' % (track_id, album_id),
- track_id, 'Downloading track location url JSON',
- headers={'X-Retpath-Y': url})
+ f'https://music.yandex.ru/api/v2.1/handlers/track/{track_id}:{album_id}/web-album_track-track-track-main/download/m',
+ track_id, 'Downloading track location url JSON', query={'hq': 1}, headers={'X-Retpath-Y': url})
fd_data = self._download_json(
download_data['src'], track_id,
'Downloading track location JSON',
query={'format': 'json'})
- key = hashlib.md5(('XGRlBW9FXlekgbPrRHuSiA' + fd_data['path'][1:] + fd_data['s']).encode('utf-8')).hexdigest()
- f_url = 'http://%s/get-mp3/%s/%s?track-id=%s ' % (fd_data['host'], key, fd_data['ts'] + fd_data['path'], track['id'])
+ key = hashlib.md5(('XGRlBW9FXlekgbPrRHuSiA' + fd_data['path'][1:] + fd_data['s']).encode()).hexdigest()
+ f_url = 'http://{}/get-mp3/{}/{}?track-id={} '.format(fd_data['host'], key, fd_data['ts'] + fd_data['path'], track['id'])
thumbnail = None
cover_uri = track.get('albums', [{}])[0].get('coverUri')
for element in decomposed:
if isinstance(element, dict) and element.get('name'):
parts.append(element['name'])
- elif isinstance(element, compat_str):
+ elif isinstance(element, str):
parts.append(element)
return ''.join(parts)
if track_artist:
track_info.update({
'artist': track_artist,
- 'title': '%s - %s' % (track_artist, track_title),
+ 'title': f'{track_artist} - {track_title}',
})
else:
track_info['title'] = track_title
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']]
+ track_ids = [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')])
+ present_track_ids = {
+ 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]
assert missing_track_ids_req
missing_tracks = self._call_api(
'track-entries', tld, url, item_id,
- 'Downloading missing tracks JSON chunk %d' % (chunk_num + 1), {
+ f'Downloading missing tracks JSON chunk {chunk_num + 1}', {
'entries': ','.join(missing_track_ids_req),
'lang': tld,
- 'external-domain': 'music.yandex.%s' % tld,
+ 'external-domain': f'music.yandex.{tld}',
'overembed': 'false',
'strict': 'true',
})
if not album_id:
continue
entries.append(self.url_result(
- 'http://music.yandex.ru/album/%s/track/%s' % (album_id, track_id),
+ f'http://music.yandex.ru/album/{album_id}/track/{track_id}',
ie=YandexMusicTrackIE.ie_key(), video_id=track_id))
return entries
class YandexMusicAlbumIE(YandexMusicPlaylistBaseIE):
IE_NAME = 'yandexmusic:album'
IE_DESC = 'Яндекс.Музыка - Альбом'
- _VALID_URL = r'%s/album/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE
+ _VALID_URL = rf'{YandexMusicBaseIE._VALID_URL_BASE}/album/(?P<id>\d+)'
_TESTS = [{
'url': 'http://music.yandex.ru/album/540508',
@classmethod
def suitable(cls, url):
- return False if YandexMusicTrackIE.suitable(url) else super(YandexMusicAlbumIE, cls).suitable(url)
+ return False if YandexMusicTrackIE.suitable(url) else super().suitable(url)
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
tld = mobj.group('tld')
album_id = mobj.group('id')
entries = self._build_playlist([track for volume in album['volumes'] for track in volume])
title = album['title']
- artist = try_get(album, lambda x: x['artists'][0]['name'], compat_str)
+ artist = try_get(album, lambda x: x['artists'][0]['name'], str)
if artist:
- title = '%s - %s' % (artist, title)
+ title = f'{artist} - {title}'
year = album.get('year')
if year:
- title += ' (%s)' % year
+ title += f' ({year})'
- return self.playlist_result(entries, compat_str(album['id']), title)
+ return self.playlist_result(entries, str(album['id']), title)
class YandexMusicPlaylistIE(YandexMusicPlaylistBaseIE):
IE_NAME = 'yandexmusic:playlist'
IE_DESC = 'Яндекс.Музыка - Плейлист'
- _VALID_URL = r'%s/users/(?P<user>[^/]+)/playlists/(?P<id>\d+)' % YandexMusicBaseIE._VALID_URL_BASE
+ _VALID_URL = rf'{YandexMusicBaseIE._VALID_URL_BASE}/users/(?P<user>[^/]+)/playlists/(?P<id>\d+)'
_TESTS = [{
'url': 'http://music.yandex.ru/users/music.partners/playlists/1245',
}]
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
tld = mobj.group('tld')
user = mobj.group('user')
playlist_id = mobj.group('id')
'kinds': playlist_id,
'light': 'true',
'lang': tld,
- 'external-domain': 'music.yandex.%s' % tld,
+ 'external-domain': f'music.yandex.{tld}',
'overembed': 'false',
})['playlist']
return self.playlist_result(
self._build_playlist(tracks),
- compat_str(playlist_id),
+ str(playlist_id),
playlist.get('title'), playlist.get('description'))
def _call_artist(self, tld, url, artist_id):
return self._call_api(
'artist', tld, url, artist_id,
- 'Downloading artist %s JSON' % self._ARTIST_WHAT, {
+ f'Downloading artist {self._ARTIST_WHAT} JSON', {
'artist': artist_id,
'what': self._ARTIST_WHAT,
'sort': self._ARTIST_SORT or '',
'dir': '',
'period': '',
'lang': tld,
- 'external-domain': 'music.yandex.%s' % tld,
+ 'external-domain': f'music.yandex.{tld}',
'overembed': 'false',
})
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_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)
+ title = try_get(data, lambda x: x['artist']['name'], 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'%s/artist/(?P<id>\d+)/tracks' % YandexMusicBaseIE._VALID_URL_BASE
+ _VALID_URL = rf'{YandexMusicBaseIE._VALID_URL_BASE}/artist/(?P<id>\d+)/tracks'
_TESTS = [{
'url': 'https://music.yandex.ru/artist/617526/tracks',
_ARTIST_WHAT = 'tracks'
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_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, 'Треки')
+ artist = try_get(data, lambda x: x['artist']['name'], str)
+ title = '{} - {}'.format(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'%s/artist/(?P<id>\d+)/albums' % YandexMusicBaseIE._VALID_URL_BASE
+ _VALID_URL = rf'{YandexMusicBaseIE._VALID_URL_BASE}/artist/(?P<id>\d+)/albums'
_TESTS = [{
'url': 'https://music.yandex.ru/artist/617526/albums',
_ARTIST_WHAT = 'albums'
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
tld = mobj.group('tld')
artist_id = mobj.group('id')
data = self._call_artist(tld, url, artist_id)
if not album_id:
continue
entries.append(self.url_result(
- 'http://music.yandex.ru/album/%s' % album_id,
+ f'http://music.yandex.ru/album/{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, 'Альбомы')
+ artist = try_get(data, lambda x: x['artist']['name'], str)
+ title = '{} - {}'.format(artist or artist_id, 'Альбомы')
return self.playlist_result(entries, artist_id, title)