2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
18 class SpotifyBaseIE(InfoExtractor
):
21 'Episode': '8276d4423d709ae9b68ec1b74cc047ba0f7479059a37820be730f125189ac2bf',
22 'MinimalShow': '13ee079672fad3f858ea45a55eb109553b4fb0969ed793185b2e34cbb6ee7cc0',
23 'ShowEpisodes': 'e0e5ce27bd7748d2c59b4d44ba245a8992a05be75d6fabc3b20753fc8857444d',
25 _VALID_URL_TEMPL
= r
'https?://open\.spotify\.com/%s/(?P<id>[^/?&#]+)'
27 def _real_initialize(self
):
28 self
._ACCESS
_TOKEN
= self
._download
_json
(
29 'https://open.spotify.com/get_access_token', None)['accessToken']
31 def _call_api(self
, operation
, video_id
, variables
):
32 return self
._download
_json
(
33 'https://api-partner.spotify.com/pathfinder/v1/query', video_id
, query
={
34 'operationName': 'query' + operation
,
35 'variables': json
.dumps(variables
),
36 'extensions': json
.dumps({
38 'sha256Hash': self
._OPERATION
_HASHES
[operation
],
41 }, headers
={'authorization': 'Bearer ' + self._ACCESS_TOKEN}
)['data']
43 def _extract_episode(self
, episode
, series
):
44 episode_id
= episode
['id']
45 title
= episode
['name'].strip()
48 audio_preview
= episode
.get('audioPreview') or {}
49 audio_preview_url
= audio_preview
.get('url')
52 'url': audio_preview_url
.replace('://p.scdn.co/mp3-preview/', '://anon-podcast.scdn.co/'),
55 audio_preview_format
= audio_preview
.get('format')
56 if audio_preview_format
:
57 f
['format_id'] = audio_preview_format
58 mobj
= re
.match(r
'([0-9A-Z]{3})_(?:[A-Z]+_)?(\d+)', audio_preview_format
)
61 'abr': int(mobj
.group(2)),
62 'ext': mobj
.group(1).lower(),
66 for item
in (try_get(episode
, lambda x
: x
['audio']['items']) or []):
67 item_url
= item
.get('url')
68 if not (item_url
and item
.get('externallyHosted')):
71 'url': clean_podcast_url(item_url
),
76 for source
in (try_get(episode
, lambda x
: x
['coverArt']['sources']) or []):
77 source_url
= source
.get('url')
82 'width': int_or_none(source
.get('width')),
83 'height': int_or_none(source
.get('height')),
90 'thumbnails': thumbnails
,
91 'description': strip_or_none(episode
.get('description')),
92 'duration': float_or_none(try_get(
93 episode
, lambda x
: x
['duration']['totalMilliseconds']), 1000),
94 'release_date': unified_strdate(try_get(
95 episode
, lambda x
: x
['releaseDate']['isoString'])),
100 class SpotifyIE(SpotifyBaseIE
):
102 _VALID_URL
= SpotifyBaseIE
._VALID
_URL
_TEMPL
% 'episode'
104 'url': 'https://open.spotify.com/episode/4Z7GAJ50bgctf6uclHlWKo',
105 'md5': '74010a1e3fa4d9e1ab3aa7ad14e42d3b',
107 'id': '4Z7GAJ50bgctf6uclHlWKo',
109 'title': 'From the archive: Why time management is ruining our lives',
110 'description': 'md5:b120d9c4ff4135b42aa9b6d9cde86935',
111 'duration': 2083.605,
112 'release_date': '20201217',
113 'series': "The Guardian's Audio Long Reads",
117 def _real_extract(self
, url
):
118 episode_id
= self
._match
_id
(url
)
119 episode
= self
._call
_api
('Episode', episode_id
, {
120 'uri': 'spotify:episode:' + episode_id
122 return self
._extract
_episode
(
123 episode
, try_get(episode
, lambda x
: x
['podcast']['name']))
126 class SpotifyShowIE(SpotifyBaseIE
):
127 IE_NAME
= 'spotify:show'
128 _VALID_URL
= SpotifyBaseIE
._VALID
_URL
_TEMPL
% 'show'
130 'url': 'https://open.spotify.com/show/4PM9Ke6l66IRNpottHKV9M',
132 'id': '4PM9Ke6l66IRNpottHKV9M',
133 'title': 'The Story from the Guardian',
134 'description': 'The Story podcast is dedicated to our finest audio documentaries, investigations and long form stories',
136 'playlist_mincount': 36,
139 def _real_extract(self
, url
):
140 show_id
= self
._match
_id
(url
)
141 podcast
= self
._call
_api
('ShowEpisodes', show_id
, {
144 'uri': 'spotify:show:' + show_id
,
146 podcast_name
= podcast
.get('name')
149 for item
in (try_get(podcast
, lambda x
: x
['episodes']['items']) or []):
150 episode
= item
.get('episode')
153 entries
.append(self
._extract
_episode
(episode
, podcast_name
))
155 return self
.playlist_result(
156 entries
, show_id
, podcast_name
, podcast
.get('description'))