You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.8 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. unified_strdate,
  7. US_RATINGS,
  8. )
  9. class PBSIE(InfoExtractor):
  10. _VALID_URL = r'''(?x)https?://
  11. (?:
  12. # Direct video URL
  13. video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
  14. # Article with embedded player (or direct video)
  15. (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
  16. # Player
  17. video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
  18. )
  19. '''
  20. _TESTS = [
  21. {
  22. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  23. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  24. 'info_dict': {
  25. 'id': '2365006249',
  26. 'ext': 'mp4',
  27. 'title': 'A More Perfect Union',
  28. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  29. 'duration': 3190,
  30. },
  31. },
  32. {
  33. 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
  34. 'md5': '143c98aa54a346738a3d78f54c925321',
  35. 'info_dict': {
  36. 'id': '2365297690',
  37. 'ext': 'mp4',
  38. 'title': 'Losing Iraq',
  39. 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
  40. 'duration': 5050,
  41. },
  42. },
  43. {
  44. 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
  45. 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
  46. 'info_dict': {
  47. 'id': '2201174722',
  48. 'ext': 'mp4',
  49. 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
  50. 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
  51. 'duration': 801,
  52. },
  53. },
  54. {
  55. 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
  56. 'md5': 'c62859342be2a0358d6c9eb306595978',
  57. 'info_dict': {
  58. 'id': '2365297708',
  59. 'ext': 'mp4',
  60. 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
  61. 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
  62. 'duration': 6559,
  63. 'thumbnail': 're:^https?://.*\.jpg$',
  64. }
  65. },
  66. {
  67. 'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
  68. 'md5': '908f3e5473a693b266b84e25e1cf9703',
  69. 'info_dict': {
  70. 'id': '2365160389',
  71. 'display_id': 'killer-typhoon',
  72. 'ext': 'mp4',
  73. 'description': 'md5:c741d14e979fc53228c575894094f157',
  74. 'title': 'Killer Typhoon',
  75. 'duration': 3172,
  76. 'thumbnail': 're:^https?://.*\.jpg$',
  77. 'upload_date': '20140122',
  78. }
  79. },
  80. {
  81. 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
  82. 'info_dict': {
  83. 'id': 'united-states-of-secrets',
  84. },
  85. 'playlist_count': 2,
  86. }
  87. ]
  88. def _extract_webpage(self, url):
  89. mobj = re.match(self._VALID_URL, url)
  90. presumptive_id = mobj.group('presumptive_id')
  91. display_id = presumptive_id
  92. if presumptive_id:
  93. webpage = self._download_webpage(url, display_id)
  94. upload_date = unified_strdate(self._search_regex(
  95. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  96. webpage, 'upload date', default=None))
  97. # tabbed frontline videos
  98. tabbed_videos = re.findall(
  99. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  100. if tabbed_videos:
  101. return tabbed_videos, presumptive_id, upload_date
  102. MEDIA_ID_REGEXES = [
  103. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  104. r'class="coveplayerid">([^<]+)<', # coveplayer
  105. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  106. ]
  107. media_id = self._search_regex(
  108. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  109. if media_id:
  110. return media_id, presumptive_id, upload_date
  111. url = self._search_regex(
  112. r'<iframe\s+(?:class|id)=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  113. webpage, 'player URL')
  114. mobj = re.match(self._VALID_URL, url)
  115. player_id = mobj.group('player_id')
  116. if not display_id:
  117. display_id = player_id
  118. if player_id:
  119. player_page = self._download_webpage(
  120. url, display_id, note='Downloading player page',
  121. errnote='Could not download player page')
  122. video_id = self._search_regex(
  123. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  124. else:
  125. video_id = mobj.group('id')
  126. display_id = video_id
  127. return video_id, display_id, None
  128. def _real_extract(self, url):
  129. video_id, display_id, upload_date = self._extract_webpage(url)
  130. if isinstance(video_id, list):
  131. entries = [self.url_result(
  132. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  133. for vid_id in video_id]
  134. return self.playlist_result(entries, display_id)
  135. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  136. info = self._download_json(info_url, display_id)
  137. redirect_url = info['alternate_encoding']['url']
  138. redirect_info = self._download_json(
  139. redirect_url + '?format=json', display_id,
  140. 'Downloading video url info')
  141. if redirect_info['status'] == 'error':
  142. if redirect_info['http_code'] == 403:
  143. message = (
  144. 'The video is not available in your region due to '
  145. 'right restrictions')
  146. else:
  147. message = redirect_info['message']
  148. raise ExtractorError(message, expected=True)
  149. rating_str = info.get('rating')
  150. if rating_str is not None:
  151. rating_str = rating_str.rpartition('-')[2]
  152. age_limit = US_RATINGS.get(rating_str)
  153. return {
  154. 'id': video_id,
  155. 'display_id': display_id,
  156. 'title': info['title'],
  157. 'url': redirect_info['url'],
  158. 'ext': 'mp4',
  159. 'description': info['program'].get('description'),
  160. 'thumbnail': info.get('image_url'),
  161. 'duration': info.get('duration'),
  162. 'age_limit': age_limit,
  163. 'upload_date': upload_date,
  164. }