]>
Commit | Line | Data |
---|---|---|
4259402c | 1 | import itertools |
9fd5ce0c PH |
2 | import re |
3 | ||
a504ced0 | 4 | from .common import InfoExtractor |
9fd5ce0c | 5 | |
49174788 S |
6 | from ..utils import ( |
7 | int_or_none, | |
4259402c | 8 | str_to_int, |
49174788 | 9 | try_get, |
cd3a3ff9 | 10 | url_or_none, |
4259402c | 11 | unified_strdate, |
12 | parse_duration, | |
49174788 | 13 | ) |
4ed3e510 | 14 | |
f853f859 | 15 | |
4259402c | 16 | class TedBaseIE(InfoExtractor): |
17 | _VALID_URL_BASE = r'https?://www\.ted\.com/(?:{type})(?:/lang/[^/#?]+)?/(?P<id>[\w-]+)' | |
18 | ||
19 | def _parse_playlist(self, playlist): | |
20 | for entry in try_get(playlist, lambda x: x['videos']['nodes'], list): | |
21 | if entry.get('__typename') == 'Video' and entry.get('canonicalUrl'): | |
22 | yield self.url_result(entry['canonicalUrl'], TedTalkIE.ie_key()) | |
23 | ||
24 | ||
25 | class TedTalkIE(TedBaseIE): | |
26 | _VALID_URL = TedBaseIE._VALID_URL_BASE.format(type='talks') | |
ac6c1048 | 27 | _TESTS = [{ |
4259402c | 28 | 'url': 'https://www.ted.com/talks/candace_parker_how_to_break_down_barriers_and_not_accept_limits', |
29 | 'md5': '47e82c666d9c3261d4fe74748a90aada', | |
9a984265 | 30 | 'info_dict': { |
4259402c | 31 | 'id': '86532', |
9a984265 | 32 | 'ext': 'mp4', |
4259402c | 33 | 'title': 'How to break down barriers and not accept limits', |
34 | 'description': 'md5:000707cece219d1e165b11550d612331', | |
cd3a3ff9 | 35 | 'view_count': int, |
4259402c | 36 | 'tags': ['personal growth', 'equality', 'activism', 'motivation', 'social change', 'sports'], |
37 | 'uploader': 'Candace Parker', | |
38 | 'duration': 676.0, | |
39 | 'upload_date': '20220114', | |
40 | 'release_date': '20211201', | |
41 | 'thumbnail': r're:http.*\.jpg', | |
9a984265 | 42 | }, |
ac6c1048 | 43 | }] |
9fd5ce0c | 44 | |
9fd5ce0c | 45 | def _real_extract(self, url): |
4259402c | 46 | display_id = self._match_id(url) |
47 | webpage = self._download_webpage(url, display_id) | |
48 | talk_info = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['videoData'] | |
49 | video_id = talk_info['id'] | |
50 | playerData = self._parse_json(talk_info.get('playerData'), video_id) | |
49174788 | 51 | |
11fa3d7f | 52 | http_url = None |
4259402c | 53 | formats, subtitles = [], {} |
54 | for format_id, resources in (playerData.get('resources') or {}).items(): | |
2a88a0c4 | 55 | if format_id == 'hls': |
4259402c | 56 | stream_url = url_or_none(try_get(resources, lambda x: x['stream'])) |
cd3a3ff9 S |
57 | if not stream_url: |
58 | continue | |
4259402c | 59 | m3u8_formats, m3u8_subs = self._extract_m3u8_formats_and_subtitles( |
60 | stream_url, video_id, 'mp4', m3u8_id=format_id, fatal=False) | |
61 | formats.extend(m3u8_formats) | |
62 | subtitles = self._merge_subtitles(subtitles, m3u8_subs) | |
63 | continue | |
64 | ||
65 | if not isinstance(resources, list): | |
66 | continue | |
67 | if format_id == 'h264': | |
68 | for resource in resources: | |
69 | h264_url = resource.get('file') | |
70 | if not h264_url: | |
2a88a0c4 | 71 | continue |
4259402c | 72 | bitrate = int_or_none(resource.get('bitrate')) |
73 | formats.append({ | |
74 | 'url': h264_url, | |
75 | 'format_id': '%s-%sk' % (format_id, bitrate), | |
76 | 'tbr': bitrate, | |
77 | }) | |
78 | if re.search(r'\d+k', h264_url): | |
79 | http_url = h264_url | |
80 | elif format_id == 'rtmp': | |
81 | streamer = talk_info.get('streamer') | |
82 | if not streamer: | |
83 | continue | |
84 | formats.extend({ | |
85 | 'format_id': '%s-%s' % (format_id, resource.get('name')), | |
86 | 'url': streamer, | |
87 | 'play_path': resource['file'], | |
88 | 'ext': 'flv', | |
89 | 'width': int_or_none(resource.get('width')), | |
90 | 'height': int_or_none(resource.get('height')), | |
91 | 'tbr': int_or_none(resource.get('bitrate')), | |
92 | } for resource in resources if resource.get('file')) | |
11fa3d7f | 93 | |
11fa3d7f | 94 | if http_url: |
4259402c | 95 | m3u8_formats = [f for f in formats if f.get('protocol') == 'm3u8' and f.get('vcodec') != 'none'] |
11fa3d7f | 96 | for m3u8_format in m3u8_formats: |
97 | bitrate = self._search_regex(r'(\d+k)', m3u8_format['url'], 'bitrate', default=None) | |
98 | if not bitrate: | |
99 | continue | |
cd3a3ff9 S |
100 | bitrate_url = re.sub(r'\d+k', bitrate, http_url) |
101 | if not self._is_valid_url( | |
4259402c | 102 | bitrate_url, video_id, '%s bitrate' % bitrate): |
cd3a3ff9 | 103 | continue |
11fa3d7f | 104 | f = m3u8_format.copy() |
105 | f.update({ | |
cd3a3ff9 | 106 | 'url': bitrate_url, |
11fa3d7f | 107 | 'format_id': m3u8_format['format_id'].replace('hls', 'http'), |
108 | 'protocol': 'http', | |
109 | }) | |
f28363ad RA |
110 | if f.get('acodec') == 'none': |
111 | del f['acodec'] | |
11fa3d7f | 112 | formats.append(f) |
66ee7b32 S |
113 | |
114 | audio_download = talk_info.get('audioDownload') | |
115 | if audio_download: | |
116 | formats.append({ | |
117 | 'url': audio_download, | |
118 | 'format_id': 'audio', | |
736785ab | 119 | 'vcodec': 'none', |
66ee7b32 S |
120 | }) |
121 | ||
14eb1ee1 | 122 | if not formats: |
4259402c | 123 | external = playerData.get('external') or {} |
124 | service = external.get('service') or '' | |
125 | ext_url = external.get('code') if service.lower() == 'youtube' else None | |
126 | return self.url_result(ext_url or external['uri']) | |
14eb1ee1 | 127 | |
4259402c | 128 | thumbnail = playerData.get('thumb') or self._og_search_property('image', webpage) |
129 | if thumbnail: | |
130 | # trim thumbnail resize parameters | |
131 | thumbnail = thumbnail.split('?')[0] | |
a9a3876d | 132 | |
463a9087 | 133 | return { |
a9a3876d | 134 | 'id': video_id, |
4259402c | 135 | 'title': talk_info.get('title') or self._og_search_title(webpage), |
136 | 'uploader': talk_info.get('presenterDisplayName'), | |
137 | 'thumbnail': thumbnail, | |
138 | 'description': talk_info.get('description') or self._og_search_description(webpage), | |
139 | 'subtitles': subtitles, | |
0d8cb1cc | 140 | 'formats': formats, |
4259402c | 141 | 'duration': talk_info.get('duration') or parse_duration(self._og_search_property('video:duration', webpage)), |
142 | 'view_count': str_to_int(talk_info.get('viewedCount')), | |
143 | 'upload_date': unified_strdate(talk_info.get('publishedAt')), | |
144 | 'release_date': unified_strdate(talk_info.get('recordedOn')), | |
145 | 'tags': try_get(playerData, lambda x: x['targeting']['tag'].split(',')), | |
0d8cb1cc PH |
146 | } |
147 | ||
ac6c1048 | 148 | |
4259402c | 149 | class TedSeriesIE(TedBaseIE): |
150 | _VALID_URL = fr'{TedBaseIE._VALID_URL_BASE.format(type=r"series")}(?:#season_(?P<season>\d+))?' | |
151 | _TESTS = [{ | |
152 | 'url': 'https://www.ted.com/series/small_thing_big_idea', | |
153 | 'info_dict': { | |
154 | 'id': '3', | |
155 | 'title': 'Small Thing Big Idea', | |
156 | 'series': 'Small Thing Big Idea', | |
157 | 'description': 'md5:6869ca52cec661aef72b3e9f7441c55c' | |
158 | }, | |
159 | 'playlist_mincount': 16, | |
160 | }, { | |
161 | 'url': 'https://www.ted.com/series/the_way_we_work#season_2', | |
162 | 'info_dict': { | |
163 | 'id': '8_2', | |
164 | 'title': 'The Way We Work Season 2', | |
165 | 'series': 'The Way We Work', | |
166 | 'description': 'md5:59469256e533e1a48c4aa926a382234c', | |
167 | 'season_number': 2 | |
168 | }, | |
169 | 'playlist_mincount': 8, | |
170 | }] | |
ac6c1048 | 171 | |
4259402c | 172 | def _real_extract(self, url): |
173 | display_id, season = self._match_valid_url(url).group('id', 'season') | |
174 | webpage = self._download_webpage(url, display_id, 'Downloading series webpage') | |
175 | info = self._search_nextjs_data(webpage, display_id)['props']['pageProps'] | |
ac6c1048 | 176 | |
4259402c | 177 | entries = itertools.chain.from_iterable( |
178 | self._parse_playlist(s) for s in info['seasons'] if season in [None, s.get('seasonNumber')]) | |
ac6c1048 | 179 | |
4259402c | 180 | series_id = try_get(info, lambda x: x['series']['id']) |
181 | series_name = try_get(info, lambda x: x['series']['name']) or self._og_search_title(webpage, fatal=False) | |
182 | ||
183 | return self.playlist_result( | |
184 | entries, | |
185 | f'{series_id}_{season}' if season and series_id else series_id, | |
186 | f'{series_name} Season {season}' if season else series_name, | |
187 | self._og_search_description(webpage), | |
188 | series=series_name, season_number=int_or_none(season)) | |
189 | ||
190 | ||
191 | class TedPlaylistIE(TedBaseIE): | |
192 | _VALID_URL = TedBaseIE._VALID_URL_BASE.format(type=r'playlists(?:/\d+)?') | |
193 | _TESTS = [{ | |
194 | 'url': 'https://www.ted.com/playlists/171/the_most_popular_talks_of_all', | |
195 | 'info_dict': { | |
196 | 'id': '171', | |
197 | 'title': 'The most popular talks of all time', | |
198 | 'description': 'md5:d2f22831dc86c7040e733a3cb3993d78' | |
199 | }, | |
200 | 'playlist_mincount': 25, | |
201 | }] | |
202 | ||
203 | def _real_extract(self, url): | |
204 | display_id = self._match_id(url) | |
205 | webpage = self._download_webpage(url, display_id) | |
206 | playlist = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['playlist'] | |
207 | ||
208 | return self.playlist_result( | |
209 | self._parse_playlist(playlist), playlist.get('id'), | |
210 | playlist.get('title') or self._og_search_title(webpage, default='').replace(' | TED Talks', '') or None, | |
211 | self._og_search_description(webpage)) | |
212 | ||
213 | ||
214 | class TedEmbedIE(InfoExtractor): | |
215 | _VALID_URL = r'https?://embed(?:-ssl)?\.ted\.com/' | |
bfd973ec | 216 | _EMBED_REGEX = [rf'<iframe[^>]+?src=(["\'])(?P<url>{_VALID_URL}.+?)\1'] |
4259402c | 217 | |
218 | _TESTS = [{ | |
219 | 'url': 'https://embed.ted.com/talks/janet_stovall_how_to_get_serious_about_diversity_and_inclusion_in_the_workplace', | |
220 | 'info_dict': { | |
221 | 'id': '21802', | |
222 | 'ext': 'mp4', | |
223 | 'title': 'How to get serious about diversity and inclusion in the workplace', | |
224 | 'description': 'md5:0978aafe396e05341f8ecc795d22189d', | |
225 | 'view_count': int, | |
226 | 'tags': list, | |
227 | 'uploader': 'Janet Stovall', | |
228 | 'duration': 664.0, | |
229 | 'upload_date': '20180822', | |
230 | 'release_date': '20180719', | |
231 | 'thumbnail': r're:http.*\.jpg', | |
232 | }, | |
233 | }] | |
234 | ||
4259402c | 235 | def _real_extract(self, url): |
236 | return self.url_result(re.sub(r'://embed(-ssl)?', '://www', url), TedTalkIE.ie_key()) |