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.

209 lines
7.6 KiB

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