[spotify:show] Fix extractor

Closes #3768
This commit is contained in:
pukkandan 2022-05-27 04:20:02 +05:30
parent 6b9e832db7
commit 8246f8402b
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

View File

@ -1,12 +1,15 @@
import functools
import json import json
import re import re
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
OnDemandPagedList,
clean_podcast_url, clean_podcast_url,
float_or_none, float_or_none,
int_or_none, int_or_none,
strip_or_none, strip_or_none,
traverse_obj,
try_get, try_get,
unified_strdate, unified_strdate,
) )
@ -25,7 +28,7 @@ class SpotifyBaseIE(InfoExtractor):
self._ACCESS_TOKEN = self._download_json( self._ACCESS_TOKEN = self._download_json(
'https://open.spotify.com/get_access_token', None)['accessToken'] 'https://open.spotify.com/get_access_token', None)['accessToken']
def _call_api(self, operation, video_id, variables): def _call_api(self, operation, video_id, variables, **kwargs):
return self._download_json( return self._download_json(
'https://api-partner.spotify.com/pathfinder/v1/query', video_id, query={ 'https://api-partner.spotify.com/pathfinder/v1/query', video_id, query={
'operationName': 'query' + operation, 'operationName': 'query' + operation,
@ -35,7 +38,8 @@ class SpotifyBaseIE(InfoExtractor):
'sha256Hash': self._OPERATION_HASHES[operation], 'sha256Hash': self._OPERATION_HASHES[operation],
}, },
}) })
}, headers={'authorization': 'Bearer ' + self._ACCESS_TOKEN})['data'] }, headers={'authorization': 'Bearer ' + self._ACCESS_TOKEN},
**kwargs)['data']
def _extract_episode(self, episode, series): def _extract_episode(self, episode, series):
episode_id = episode['id'] episode_id = episode['id']
@ -143,22 +147,25 @@ class SpotifyShowIE(SpotifyBaseIE):
}, },
'playlist_mincount': 36, 'playlist_mincount': 36,
} }
_PER_PAGE = 100
def _fetch_page(self, show_id, page=0):
return self._call_api('ShowEpisodes', show_id, {
'limit': 100,
'offset': page * self._PER_PAGE,
'uri': f'spotify:show:{show_id}',
}, note=f'Downloading page {page + 1} JSON metadata')['podcast']
def _real_extract(self, url): def _real_extract(self, url):
show_id = self._match_id(url) show_id = self._match_id(url)
podcast = self._call_api('ShowEpisodes', show_id, { first_page = self._fetch_page(show_id)
'limit': 1000000000,
'offset': 0,
'uri': 'spotify:show:' + show_id,
})['podcast']
podcast_name = podcast.get('name')
entries = [] def _entries(page):
for item in (try_get(podcast, lambda x: x['episodes']['items']) or []): podcast = self._fetch_page(show_id, page) if page else first_page
episode = item.get('episode') yield from map(
if not episode: functools.partial(self._extract_episode, series=podcast.get('name')),
continue traverse_obj(podcast, ('episodes', 'items', ..., 'episode')))
entries.append(self._extract_episode(episode, podcast_name))
return self.playlist_result( return self.playlist_result(
entries, show_id, podcast_name, podcast.get('description')) OnDemandPagedList(_entries, self._PER_PAGE),
show_id, first_page.get('name'), first_page.get('description'))