2 from __future__ import unicode_literals
6 from .common import InfoExtractor
17 class PBSIE(InfoExtractor):
18 _VALID_URL = r'''(?x)https?://
21 video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
22 # Article with embedded player (or direct video)
23 (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
25 (?:video|player)\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
31 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
32 'md5': 'ce1888486f0908d555a8093cac9a7362',
36 'title': 'Constitution USA with Peter Sagal - A More Perfect Union',
37 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
41 'skip_download': True, # requires ffmpeg
45 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
46 'md5': '143c98aa54a346738a3d78f54c925321',
50 'title': 'FRONTLINE - Losing Iraq',
51 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
55 'skip_download': True, # requires ffmpeg
59 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
60 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
64 'title': 'PBS NewsHour - Cyber Schools Gain Popularity, but Quality Questions Persist',
65 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
70 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
71 'md5': 'c62859342be2a0358d6c9eb306595978',
75 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
76 'title': 'Great Performances - Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
78 'thumbnail': 're:^https?://.*\.jpg$',
81 'skip_download': True, # requires ffmpeg
85 'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
86 'md5': '908f3e5473a693b266b84e25e1cf9703',
89 'display_id': 'killer-typhoon',
91 'description': 'md5:c741d14e979fc53228c575894094f157',
92 'title': 'NOVA - Killer Typhoon',
94 'thumbnail': 're:^https?://.*\.jpg$',
95 'upload_date': '20140122',
99 'skip_download': True, # requires ffmpeg
103 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
105 'id': 'united-states-of-secrets',
110 'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
113 'display_id': 'player',
115 'title': 'American Experience - Death and the Civil War, Chapter 1',
116 'description': 'American Experience, TV’s most-watched history series, brings to life the compelling stories from our past that inform our understanding of the world today.',
118 'thumbnail': 're:^https?://.*\.jpg$',
121 'skip_download': True, # requires ffmpeg
125 'url': 'http://video.pbs.org/video/2365367186/',
128 'display_id': '2365367186',
130 'title': 'To Catch A Comet - Full Episode',
131 'description': 'On November 12, 2014, billions of kilometers from Earth, spacecraft orbiter Rosetta and lander Philae did what no other had dared to attempt \u2014 land on the volatile surface of a comet as it zooms around the sun at 67,000 km/hr. The European Space Agency hopes this mission can help peer into our past and unlock secrets of our origins.',
133 'thumbnail': 're:^https?://.*\.jpg$',
136 'skip_download': True, # requires ffmpeg
141 # Video embedded in iframe containing angle brackets as attribute's value (e.g.
142 # "<iframe style='position: absolute;<br />\ntop: 0; left: 0;' ...", see
143 # https://github.com/rg3/youtube-dl/issues/7059)
144 'url': 'http://www.pbs.org/food/features/a-chefs-life-season-3-episode-5-prickly-business/',
147 'display_id': 'a-chefs-life-season-3-episode-5-prickly-business',
149 'title': "A Chef's Life - Season 3, Ep. 5: Prickly Business",
150 'description': 'md5:61db2ddf27c9912f09c241014b118ed1',
152 'thumbnail': 're:^https?://.*\.jpg$',
155 'skip_download': True, # requires ffmpeg
159 # Frontline video embedded via flp2012.js
160 'url': 'http://www.pbs.org/wgbh/pages/frontline/the-atomic-artists',
163 'display_id': 'the-atomic-artists',
165 'title': 'FRONTLINE - The Atomic Artists',
166 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
168 'thumbnail': 're:^https?://.*\.jpg$',
171 'skip_download': True, # requires ffmpeg
175 'url': 'http://player.pbs.org/widget/partnerplayer/2365297708/?start=0&end=0&chapterbar=false&endscreen=false&topbar=true',
176 'only_matching': True,
180 101: 'We\'re sorry, but this video is not yet available.',
181 403: 'We\'re sorry, but this video is not available in your region due to right restrictions.',
182 404: 'We are experiencing technical difficulties that are preventing us from playing the video at this time. Please check back again soon.',
183 410: 'This video has expired and is no longer available for online streaming.',
186 def _extract_webpage(self, url):
187 mobj = re.match(self._VALID_URL, url)
189 presumptive_id = mobj.group('presumptive_id')
190 display_id = presumptive_id
192 webpage = self._download_webpage(url, display_id)
194 upload_date = unified_strdate(self._search_regex(
195 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
196 webpage, 'upload date', default=None))
198 # tabbed frontline videos
199 tabbed_videos = re.findall(
200 r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
202 return tabbed_videos, presumptive_id, upload_date
205 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
206 r'class="coveplayerid">([^<]+)<', # coveplayer
207 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
210 media_id = self._search_regex(
211 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
213 return media_id, presumptive_id, upload_date
215 # Fronline video embedded via flp
216 video_id = self._search_regex(
217 r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid', default=None)
219 # pkg_id calculation is reverse engineered from
220 # http://www.pbs.org/wgbh/pages/frontline/js/flp2012.js
221 prg_id = self._search_regex(
222 r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid')[7:]
224 prg_id = prg_id.split('q')[1]
225 prg_id = int(prg_id, 16)
226 getdir = self._download_json(
227 'http://www.pbs.org/wgbh/pages/frontline/.json/getdir/getdir%d.json' % prg_id,
228 presumptive_id, 'Downloading getdir JSON',
229 transform_source=strip_jsonp)
230 return getdir['mid'], presumptive_id, upload_date
232 for iframe in re.findall(r'(?s)<iframe(.+?)></iframe>', webpage):
233 url = self._search_regex(
234 r'src=(["\'])(?P<url>.+?partnerplayer.+?)\1', iframe,
235 'player URL', default=None, group='url')
239 mobj = re.match(self._VALID_URL, url)
241 player_id = mobj.group('player_id')
243 display_id = player_id
245 player_page = self._download_webpage(
246 url, display_id, note='Downloading player page',
247 errnote='Could not download player page')
248 video_id = self._search_regex(
249 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
251 video_id = mobj.group('id')
252 display_id = video_id
254 return video_id, display_id, None
256 def _real_extract(self, url):
257 video_id, display_id, upload_date = self._extract_webpage(url)
259 if isinstance(video_id, list):
260 entries = [self.url_result(
261 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
262 for vid_id in video_id]
263 return self.playlist_result(entries, display_id)
265 info = self._download_json(
266 'http://player.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
270 for encoding_name in ('recommended_encoding', 'alternate_encoding'):
271 redirect = info.get(encoding_name)
274 redirect_url = redirect.get('url')
278 redirect_info = self._download_json(
279 redirect_url + '?format=json', display_id,
280 'Downloading %s video url info' % encoding_name)
282 if redirect_info['status'] == 'error':
283 raise ExtractorError(
286 self._ERRORS.get(redirect_info['http_code'], redirect_info['message'])),
289 format_url = redirect_info.get('url')
293 if determine_ext(format_url) == 'm3u8':
294 formats.extend(self._extract_m3u8_formats(
295 format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
299 'format_id': redirect.get('eeid'),
301 self._sort_formats(formats)
303 rating_str = info.get('rating')
304 if rating_str is not None:
305 rating_str = rating_str.rpartition('-')[2]
306 age_limit = US_RATINGS.get(rating_str)
309 closed_captions_url = info.get('closed_captions_url')
310 if closed_captions_url:
313 'url': closed_captions_url,
316 # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
317 # Try turning it to 'program - title' naming scheme if possible
318 alt_title = info.get('program', {}).get('title')
320 info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
324 'display_id': display_id,
325 'title': info['title'],
326 'description': info['program'].get('description'),
327 'thumbnail': info.get('image_url'),
328 'duration': int_or_none(info.get('duration')),
329 'age_limit': age_limit,
330 'upload_date': upload_date,
332 'subtitles': subtitles,