]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pbs.py
   1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
  15 class PBSIE(InfoExtractor
): 
  16     _VALID_URL 
= r
'''(?x)https?:// 
  19            video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? | 
  20            # Article with embedded player (or direct video) 
  21            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) | 
  23            video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/ 
  29             'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/', 
  30             'md5': 'ce1888486f0908d555a8093cac9a7362', 
  34                 'title': 'A More Perfect Union', 
  35                 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a', 
  40             'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/', 
  41             'md5': '143c98aa54a346738a3d78f54c925321', 
  45                 'title': 'Losing Iraq', 
  46                 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e', 
  51             'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/', 
  52             'md5': 'b19856d7f5351b17a5ab1dc6a64be633', 
  56                 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist', 
  57                 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28', 
  62             'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/', 
  63             'md5': 'c62859342be2a0358d6c9eb306595978', 
  67                 'description': 'md5:68d87ef760660eb564455eb30ca464fe', 
  68                 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full', 
  70                 'thumbnail': 're:^https?://.*\.jpg$', 
  74             'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html', 
  75             'md5': '908f3e5473a693b266b84e25e1cf9703', 
  78                 'display_id': 'killer-typhoon', 
  80                 'description': 'md5:c741d14e979fc53228c575894094f157', 
  81                 'title': 'Killer Typhoon', 
  83                 'thumbnail': 're:^https?://.*\.jpg$', 
  84                 'upload_date': '20140122', 
  88             'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/', 
  90                 'id': 'united-states-of-secrets', 
  96     def _extract_webpage(self
, url
): 
  97         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  99         presumptive_id 
= mobj
.group('presumptive_id') 
 100         display_id 
= presumptive_id
 
 102             webpage 
= self
._download
_webpage
(url
, display_id
) 
 104             upload_date 
= unified_strdate(self
._search
_regex
( 
 105                 r
'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"', 
 106                 webpage
, 'upload date', default
=None)) 
 108             # tabbed frontline videos 
 109             tabbed_videos 
= re
.findall( 
 110                 r
'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage
) 
 112                 return tabbed_videos
, presumptive_id
, upload_date
 
 115                 r
"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",  # frontline video embed 
 116                 r
'class="coveplayerid">([^<]+)<',                       # coveplayer 
 117                 r
'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>',  # jwplayer 
 120             media_id 
= self
._search
_regex
( 
 121                 MEDIA_ID_REGEXES
, webpage
, 'media ID', fatal
=False, default
=None) 
 123                 return media_id
, presumptive_id
, upload_date
 
 125             url 
= self
._search
_regex
( 
 126                 r
'<iframe\s+(?:class|id)=["\']partnerPlayer
["\'].*?\s+src=["\'](.*?
)["\']>', 
 127                 webpage, 'player URL') 
 128             mobj = re.match(self._VALID_URL, url) 
 130         player_id = mobj.group('player_id') 
 132             display_id = player_id 
 134             player_page = self._download_webpage( 
 135                 url, display_id, note='Downloading player page', 
 136                 errnote='Could not download player page') 
 137             video_id = self._search_regex( 
 138                 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID') 
 140             video_id = mobj.group('id') 
 141             display_id = video_id 
 143         return video_id, display_id, None 
 145     def _real_extract(self, url): 
 146         video_id, display_id, upload_date = self._extract_webpage(url) 
 148         if isinstance(video_id, list): 
 149             entries = [self.url_result( 
 150                 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id) 
 151                 for vid_id in video_id] 
 152             return self.playlist_result(entries, display_id) 
 154         info = self._download_json( 
 155             'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id, 
 159         for encoding_name in ('recommended_encoding', 'alternate_encoding'): 
 160             redirect = info.get(encoding_name) 
 163             redirect_url = redirect.get('url') 
 167             redirect_info = self._download_json( 
 168                 redirect_url + '?format=json', display_id, 
 169                 'Downloading %s video url info' % encoding_name) 
 171             if redirect_info['status'] == 'error': 
 172                 if redirect_info['http_code'] == 403: 
 174                         'The video is not available in your region due to ' 
 175                         'right restrictions') 
 177                     message = redirect_info['message'] 
 178                 raise ExtractorError(message, expected=True) 
 180             format_url = redirect_info.get('url') 
 184             if determine_ext(format_url) == 'm3u8': 
 185                 formats.extend(self._extract_m3u8_formats( 
 186                     format_url, display_id, 'mp4', preference=1, m3u8_id='hls')) 
 190                     'format_id': redirect.get('eeid'), 
 192         self._sort_formats(formats) 
 194         rating_str = info.get('rating') 
 195         if rating_str is not None: 
 196             rating_str = rating_str.rpartition('-')[2] 
 197         age_limit = US_RATINGS.get(rating_str) 
 201             'display_id': display_id, 
 202             'title': info['title'], 
 203             'description': info['program'].get('description'), 
 204             'thumbnail': info.get('image_url'), 
 205             'duration': int_or_none(info.get('duration')), 
 206             'age_limit': age_limit, 
 207             'upload_date': upload_date,