2 from __future__ import unicode_literals
6 from .common import InfoExtractor
16 class PBSIE(InfoExtractor):
17 _VALID_URL = r'''(?x)https?://
20 video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
21 # Article with embedded player (or direct video)
22 (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
24 video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
30 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
31 'md5': 'ce1888486f0908d555a8093cac9a7362',
35 'title': 'Constitution USA with Peter Sagal - A More Perfect Union',
36 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
40 'skip_download': True, # requires ffmpeg
44 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
45 'md5': '143c98aa54a346738a3d78f54c925321',
49 'title': 'FRONTLINE - Losing Iraq',
50 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
54 'skip_download': True, # requires ffmpeg
58 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
59 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
63 'title': 'PBS NewsHour - Cyber Schools Gain Popularity, but Quality Questions Persist',
64 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
69 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
70 'md5': 'c62859342be2a0358d6c9eb306595978',
74 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
75 'title': 'Great Performances - Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
77 'thumbnail': 're:^https?://.*\.jpg$',
80 'skip_download': True, # requires ffmpeg
84 'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
85 'md5': '908f3e5473a693b266b84e25e1cf9703',
88 'display_id': 'killer-typhoon',
90 'description': 'md5:c741d14e979fc53228c575894094f157',
91 'title': 'NOVA - Killer Typhoon',
93 'thumbnail': 're:^https?://.*\.jpg$',
94 'upload_date': '20140122',
98 'skip_download': True, # requires ffmpeg
102 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
104 'id': 'united-states-of-secrets',
109 'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
112 'display_id': 'player',
114 'title': 'American Experience - Death and the Civil War',
115 '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.',
117 'thumbnail': 're:^https?://.*\.jpg$',
120 'skip_download': True, # requires ffmpeg
124 'url': 'http://video.pbs.org/video/2365367186/',
127 'display_id': '2365367186',
129 'title': 'To Catch A Comet - Full Episode',
130 '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.',
132 'thumbnail': 're:^https?://.*\.jpg$',
135 'skip_download': True, # requires ffmpeg
139 # Video embedded in iframe containing angle brackets as attribute's value (e.g.
140 # "<iframe style='position: absolute;<br />\ntop: 0; left: 0;' ...", see
141 # https://github.com/rg3/youtube-dl/issues/7059)
142 'url': 'http://www.pbs.org/food/features/a-chefs-life-season-3-episode-5-prickly-business/',
145 'display_id': 'a-chefs-life-season-3-episode-5-prickly-business',
147 'title': "A Chef's Life - Season 3, Ep. 5: Prickly Business",
148 'description': 'md5:61db2ddf27c9912f09c241014b118ed1',
150 'thumbnail': 're:^https?://.*\.jpg$',
153 'skip_download': True, # requires ffmpeg
158 def _extract_webpage(self, url):
159 mobj = re.match(self._VALID_URL, url)
161 presumptive_id = mobj.group('presumptive_id')
162 display_id = presumptive_id
164 webpage = self._download_webpage(url, display_id)
166 upload_date = unified_strdate(self._search_regex(
167 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
168 webpage, 'upload date', default=None))
170 # tabbed frontline videos
171 tabbed_videos = re.findall(
172 r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
174 return tabbed_videos, presumptive_id, upload_date
177 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
178 r'class="coveplayerid">([^<]+)<', # coveplayer
179 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
182 media_id = self._search_regex(
183 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
185 return media_id, presumptive_id, upload_date
187 url = self._search_regex(
188 r'(?s)<iframe[^>]+?(?:[a-z-]+?=["\'].*?["\'][^>]+?)*?\bsrc=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
189 webpage, 'player URL')
190 mobj = re.match(self._VALID_URL, url)
192 player_id = mobj.group('player_id')
194 display_id = player_id
196 player_page = self._download_webpage(
197 url, display_id, note='Downloading player page',
198 errnote='Could not download player page')
199 video_id = self._search_regex(
200 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
202 video_id = mobj.group('id')
203 display_id = video_id
205 return video_id, display_id, None
207 def _real_extract(self, url):
208 video_id, display_id, upload_date = self._extract_webpage(url)
210 if isinstance(video_id, list):
211 entries = [self.url_result(
212 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
213 for vid_id in video_id]
214 return self.playlist_result(entries, display_id)
216 info = self._download_json(
217 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
221 for encoding_name in ('recommended_encoding', 'alternate_encoding'):
222 redirect = info.get(encoding_name)
225 redirect_url = redirect.get('url')
229 redirect_info = self._download_json(
230 redirect_url + '?format=json', display_id,
231 'Downloading %s video url info' % encoding_name)
233 if redirect_info['status'] == 'error':
234 if redirect_info['http_code'] == 403:
236 'The video is not available in your region due to '
237 'right restrictions')
239 message = redirect_info['message']
240 raise ExtractorError(message, expected=True)
242 format_url = redirect_info.get('url')
246 if determine_ext(format_url) == 'm3u8':
247 formats.extend(self._extract_m3u8_formats(
248 format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
252 'format_id': redirect.get('eeid'),
254 self._sort_formats(formats)
256 rating_str = info.get('rating')
257 if rating_str is not None:
258 rating_str = rating_str.rpartition('-')[2]
259 age_limit = US_RATINGS.get(rating_str)
262 closed_captions_url = info.get('closed_captions_url')
263 if closed_captions_url:
266 'url': closed_captions_url,
269 # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
270 # Try turning it to 'program - title' naming scheme if possible
271 alt_title = info.get('program', {}).get('title')
273 info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
277 'display_id': display_id,
278 'title': info['title'],
279 'description': info['program'].get('description'),
280 'thumbnail': info.get('image_url'),
281 'duration': int_or_none(info.get('duration')),
282 'age_limit': age_limit,
283 'upload_date': upload_date,
285 'subtitles': subtitles,