]>
Commit | Line | Data |
---|---|---|
a820dc72 RA |
1 | import json |
2 | import re | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
6 | clean_podcast_url, | |
7 | float_or_none, | |
8 | int_or_none, | |
9 | strip_or_none, | |
10 | try_get, | |
11 | unified_strdate, | |
12 | ) | |
13 | ||
14 | ||
15 | class SpotifyBaseIE(InfoExtractor): | |
16 | _ACCESS_TOKEN = None | |
17 | _OPERATION_HASHES = { | |
18 | 'Episode': '8276d4423d709ae9b68ec1b74cc047ba0f7479059a37820be730f125189ac2bf', | |
19 | 'MinimalShow': '13ee079672fad3f858ea45a55eb109553b4fb0969ed793185b2e34cbb6ee7cc0', | |
20 | 'ShowEpisodes': 'e0e5ce27bd7748d2c59b4d44ba245a8992a05be75d6fabc3b20753fc8857444d', | |
21 | } | |
a49e777d | 22 | _VALID_URL_TEMPL = r'https?://open\.spotify\.com/(?:embed-podcast/|embed/|)%s/(?P<id>[^/?&#]+)' |
a820dc72 RA |
23 | |
24 | def _real_initialize(self): | |
25 | self._ACCESS_TOKEN = self._download_json( | |
26 | 'https://open.spotify.com/get_access_token', None)['accessToken'] | |
27 | ||
28 | def _call_api(self, operation, video_id, variables): | |
29 | return self._download_json( | |
30 | 'https://api-partner.spotify.com/pathfinder/v1/query', video_id, query={ | |
31 | 'operationName': 'query' + operation, | |
32 | 'variables': json.dumps(variables), | |
33 | 'extensions': json.dumps({ | |
34 | 'persistedQuery': { | |
35 | 'sha256Hash': self._OPERATION_HASHES[operation], | |
36 | }, | |
37 | }) | |
38 | }, headers={'authorization': 'Bearer ' + self._ACCESS_TOKEN})['data'] | |
39 | ||
40 | def _extract_episode(self, episode, series): | |
41 | episode_id = episode['id'] | |
42 | title = episode['name'].strip() | |
43 | ||
44 | formats = [] | |
45 | audio_preview = episode.get('audioPreview') or {} | |
46 | audio_preview_url = audio_preview.get('url') | |
47 | if audio_preview_url: | |
48 | f = { | |
49 | 'url': audio_preview_url.replace('://p.scdn.co/mp3-preview/', '://anon-podcast.scdn.co/'), | |
50 | 'vcodec': 'none', | |
51 | } | |
52 | audio_preview_format = audio_preview.get('format') | |
53 | if audio_preview_format: | |
54 | f['format_id'] = audio_preview_format | |
55 | mobj = re.match(r'([0-9A-Z]{3})_(?:[A-Z]+_)?(\d+)', audio_preview_format) | |
56 | if mobj: | |
57 | f.update({ | |
58 | 'abr': int(mobj.group(2)), | |
59 | 'ext': mobj.group(1).lower(), | |
60 | }) | |
61 | formats.append(f) | |
62 | ||
63 | for item in (try_get(episode, lambda x: x['audio']['items']) or []): | |
64 | item_url = item.get('url') | |
65 | if not (item_url and item.get('externallyHosted')): | |
66 | continue | |
67 | formats.append({ | |
68 | 'url': clean_podcast_url(item_url), | |
69 | 'vcodec': 'none', | |
70 | }) | |
71 | ||
72 | thumbnails = [] | |
73 | for source in (try_get(episode, lambda x: x['coverArt']['sources']) or []): | |
74 | source_url = source.get('url') | |
75 | if not source_url: | |
76 | continue | |
77 | thumbnails.append({ | |
78 | 'url': source_url, | |
79 | 'width': int_or_none(source.get('width')), | |
80 | 'height': int_or_none(source.get('height')), | |
81 | }) | |
82 | ||
83 | return { | |
84 | 'id': episode_id, | |
85 | 'title': title, | |
86 | 'formats': formats, | |
87 | 'thumbnails': thumbnails, | |
88 | 'description': strip_or_none(episode.get('description')), | |
89 | 'duration': float_or_none(try_get( | |
90 | episode, lambda x: x['duration']['totalMilliseconds']), 1000), | |
91 | 'release_date': unified_strdate(try_get( | |
92 | episode, lambda x: x['releaseDate']['isoString'])), | |
93 | 'series': series, | |
94 | } | |
95 | ||
a49e777d F |
96 | @classmethod |
97 | def _extract_embed_urls(cls, webpage): | |
98 | return re.findall( | |
99 | r'<iframe[^>]+src="(https?://open\.spotify.com/embed/[^"]+)"', | |
100 | webpage) | |
101 | ||
a820dc72 RA |
102 | |
103 | class SpotifyIE(SpotifyBaseIE): | |
104 | IE_NAME = 'spotify' | |
19a03940 | 105 | IE_DESC = 'Spotify episodes' |
a820dc72 | 106 | _VALID_URL = SpotifyBaseIE._VALID_URL_TEMPL % 'episode' |
a49e777d | 107 | _TESTS = [{ |
a820dc72 RA |
108 | 'url': 'https://open.spotify.com/episode/4Z7GAJ50bgctf6uclHlWKo', |
109 | 'md5': '74010a1e3fa4d9e1ab3aa7ad14e42d3b', | |
110 | 'info_dict': { | |
111 | 'id': '4Z7GAJ50bgctf6uclHlWKo', | |
112 | 'ext': 'mp3', | |
113 | 'title': 'From the archive: Why time management is ruining our lives', | |
114 | 'description': 'md5:b120d9c4ff4135b42aa9b6d9cde86935', | |
115 | 'duration': 2083.605, | |
116 | 'release_date': '20201217', | |
117 | 'series': "The Guardian's Audio Long Reads", | |
118 | } | |
a49e777d F |
119 | }, { |
120 | 'url': 'https://open.spotify.com/embed/episode/4TvCsKKs2thXmarHigWvXE?si=7eatS8AbQb6RxqO2raIuWA', | |
121 | 'only_matching': True, | |
122 | }] | |
a820dc72 RA |
123 | |
124 | def _real_extract(self, url): | |
125 | episode_id = self._match_id(url) | |
126 | episode = self._call_api('Episode', episode_id, { | |
127 | 'uri': 'spotify:episode:' + episode_id | |
128 | })['episode'] | |
129 | return self._extract_episode( | |
130 | episode, try_get(episode, lambda x: x['podcast']['name'])) | |
131 | ||
132 | ||
133 | class SpotifyShowIE(SpotifyBaseIE): | |
134 | IE_NAME = 'spotify:show' | |
19a03940 | 135 | IE_DESC = 'Spotify shows' |
a820dc72 RA |
136 | _VALID_URL = SpotifyBaseIE._VALID_URL_TEMPL % 'show' |
137 | _TEST = { | |
138 | 'url': 'https://open.spotify.com/show/4PM9Ke6l66IRNpottHKV9M', | |
139 | 'info_dict': { | |
140 | 'id': '4PM9Ke6l66IRNpottHKV9M', | |
141 | 'title': 'The Story from the Guardian', | |
142 | 'description': 'The Story podcast is dedicated to our finest audio documentaries, investigations and long form stories', | |
143 | }, | |
144 | 'playlist_mincount': 36, | |
145 | } | |
146 | ||
147 | def _real_extract(self, url): | |
148 | show_id = self._match_id(url) | |
149 | podcast = self._call_api('ShowEpisodes', show_id, { | |
150 | 'limit': 1000000000, | |
151 | 'offset': 0, | |
152 | 'uri': 'spotify:show:' + show_id, | |
153 | })['podcast'] | |
154 | podcast_name = podcast.get('name') | |
155 | ||
156 | entries = [] | |
157 | for item in (try_get(podcast, lambda x: x['episodes']['items']) or []): | |
158 | episode = item.get('episode') | |
159 | if not episode: | |
160 | continue | |
161 | entries.append(self._extract_episode(episode, podcast_name)) | |
162 | ||
163 | return self.playlist_result( | |
164 | entries, show_id, podcast_name, podcast.get('description')) |