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