]>
Commit | Line | Data |
---|---|---|
bc2ca1bb | 1 | import re |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..utils import ( | |
5 | clean_podcast_url, | |
6 | int_or_none, | |
7 | parse_iso8601, | |
8 | strip_or_none, | |
9 | try_get, | |
10 | urlencode_postdata, | |
11 | ) | |
12 | ||
13 | ||
14 | class SimplecastBaseIE(InfoExtractor): | |
15 | _UUID_REGEX = r'[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}' | |
16 | _API_BASE = 'https://api.simplecast.com/' | |
17 | ||
18 | def _call_api(self, path_tmpl, video_id): | |
19 | return self._download_json( | |
20 | self._API_BASE + path_tmpl % video_id, video_id) | |
21 | ||
22 | def _call_search_api(self, resource, resource_id, resource_url): | |
23 | return self._download_json( | |
24 | 'https://api.simplecast.com/%ss/search' % resource, resource_id, | |
25 | data=urlencode_postdata({'url': resource_url})) | |
26 | ||
27 | def _parse_episode(self, episode): | |
28 | episode_id = episode['id'] | |
29 | title = episode['title'].strip() | |
30 | audio_file = episode.get('audio_file') or {} | |
31 | audio_file_url = audio_file.get('url') or episode.get('audio_file_url') or episode['enclosure_url'] | |
32 | ||
33 | season = episode.get('season') or {} | |
34 | season_href = season.get('href') | |
35 | season_id = None | |
36 | if season_href: | |
37 | season_id = self._search_regex( | |
38 | r'https?://api.simplecast.com/seasons/(%s)' % self._UUID_REGEX, | |
39 | season_href, 'season id', default=None) | |
40 | ||
41 | webpage_url = episode.get('episode_url') | |
42 | channel_url = None | |
43 | if webpage_url: | |
44 | channel_url = self._search_regex( | |
45 | r'(https?://[^/]+\.simplecast\.com)', | |
46 | webpage_url, 'channel url', default=None) | |
47 | ||
48 | return { | |
49 | 'id': episode_id, | |
50 | 'display_id': episode.get('slug'), | |
51 | 'title': title, | |
52 | 'url': clean_podcast_url(audio_file_url), | |
53 | 'webpage_url': webpage_url, | |
54 | 'channel_url': channel_url, | |
55 | 'series': try_get(episode, lambda x: x['podcast']['title']), | |
56 | 'season_number': int_or_none(season.get('number')), | |
57 | 'season_id': season_id, | |
58 | 'thumbnail': episode.get('image_url'), | |
59 | 'episode_id': episode_id, | |
60 | 'episode_number': int_or_none(episode.get('number')), | |
61 | 'description': strip_or_none(episode.get('description')), | |
62 | 'timestamp': parse_iso8601(episode.get('published_at')), | |
63 | 'duration': int_or_none(episode.get('duration')), | |
64 | 'filesize': int_or_none(audio_file.get('size') or episode.get('audio_file_size')), | |
65 | } | |
66 | ||
67 | ||
68 | class SimplecastIE(SimplecastBaseIE): | |
69 | IE_NAME = 'simplecast' | |
70 | _VALID_URL = r'https?://(?:api\.simplecast\.com/episodes|player\.simplecast\.com)/(?P<id>%s)' % SimplecastBaseIE._UUID_REGEX | |
71 | _COMMON_TEST_INFO = { | |
72 | 'display_id': 'errant-signal-chris-franklin-new-wave-video-essays', | |
73 | 'id': 'b6dc49a2-9404-4853-9aa9-9cfc097be876', | |
74 | 'ext': 'mp3', | |
75 | 'title': 'Errant Signal - Chris Franklin & New Wave Video Essays', | |
76 | 'episode_number': 1, | |
77 | 'episode_id': 'b6dc49a2-9404-4853-9aa9-9cfc097be876', | |
78 | 'description': 'md5:34752789d3d2702e2d2c975fbd14f357', | |
79 | 'season_number': 1, | |
80 | 'season_id': 'e23df0da-bae4-4531-8bbf-71364a88dc13', | |
81 | 'series': 'The RE:BIND.io Podcast', | |
82 | 'duration': 5343, | |
83 | 'timestamp': 1580979475, | |
84 | 'upload_date': '20200206', | |
85 | 'webpage_url': r're:^https?://the-re-bind-io-podcast\.simplecast\.com/episodes/errant-signal-chris-franklin-new-wave-video-essays', | |
86 | 'channel_url': r're:^https?://the-re-bind-io-podcast\.simplecast\.com$', | |
87 | } | |
88 | _TESTS = [{ | |
89 | 'url': 'https://api.simplecast.com/episodes/b6dc49a2-9404-4853-9aa9-9cfc097be876', | |
90 | 'md5': '8c93be7be54251bf29ee97464eabe61c', | |
91 | 'info_dict': _COMMON_TEST_INFO, | |
92 | }, { | |
93 | 'url': 'https://player.simplecast.com/b6dc49a2-9404-4853-9aa9-9cfc097be876', | |
94 | 'only_matching': True, | |
95 | }] | |
96 | ||
97 | @staticmethod | |
98 | def _extract_urls(webpage): | |
99 | return re.findall( | |
100 | r'''(?x)<iframe[^>]+src=["\'] | |
101 | ( | |
102 | https?://(?:embed\.simplecast\.com/[0-9a-f]{8}| | |
103 | player\.simplecast\.com/%s | |
104 | ))''' % SimplecastBaseIE._UUID_REGEX, webpage) | |
105 | ||
106 | def _real_extract(self, url): | |
107 | episode_id = self._match_id(url) | |
108 | episode = self._call_api('episodes/%s', episode_id) | |
109 | return self._parse_episode(episode) | |
110 | ||
111 | ||
112 | class SimplecastEpisodeIE(SimplecastBaseIE): | |
113 | IE_NAME = 'simplecast:episode' | |
114 | _VALID_URL = r'https?://(?!api\.)[^/]+\.simplecast\.com/episodes/(?P<id>[^/?&#]+)' | |
115 | _TEST = { | |
116 | 'url': 'https://the-re-bind-io-podcast.simplecast.com/episodes/errant-signal-chris-franklin-new-wave-video-essays', | |
117 | 'md5': '8c93be7be54251bf29ee97464eabe61c', | |
118 | 'info_dict': SimplecastIE._COMMON_TEST_INFO, | |
119 | } | |
120 | ||
121 | def _real_extract(self, url): | |
5ad28e7f | 122 | mobj = self._match_valid_url(url) |
bc2ca1bb | 123 | episode = self._call_search_api( |
124 | 'episode', mobj.group(1), mobj.group(0)) | |
125 | return self._parse_episode(episode) | |
126 | ||
127 | ||
128 | class SimplecastPodcastIE(SimplecastBaseIE): | |
129 | IE_NAME = 'simplecast:podcast' | |
130 | _VALID_URL = r'https?://(?!(?:api|cdn|embed|feeds|player)\.)(?P<id>[^/]+)\.simplecast\.com(?!/episodes/[^/?&#]+)' | |
131 | _TESTS = [{ | |
132 | 'url': 'https://the-re-bind-io-podcast.simplecast.com', | |
133 | 'playlist_mincount': 33, | |
134 | 'info_dict': { | |
135 | 'id': '07d28d26-7522-42eb-8c53-2bdcfc81c43c', | |
136 | 'title': 'The RE:BIND.io Podcast', | |
137 | }, | |
138 | }, { | |
139 | 'url': 'https://the-re-bind-io-podcast.simplecast.com/episodes', | |
140 | 'only_matching': True, | |
141 | }] | |
142 | ||
143 | def _real_extract(self, url): | |
144 | subdomain = self._match_id(url) | |
145 | site = self._call_search_api('site', subdomain, url) | |
146 | podcast = site['podcast'] | |
147 | podcast_id = podcast['id'] | |
148 | podcast_title = podcast.get('title') | |
149 | ||
150 | def entries(): | |
151 | episodes = self._call_api('podcasts/%s/episodes', podcast_id) | |
152 | for episode in (episodes.get('collection') or []): | |
153 | info = self._parse_episode(episode) | |
154 | info['series'] = podcast_title | |
155 | yield info | |
156 | ||
157 | return self.playlist_result(entries(), podcast_id, podcast_title) |