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.

170 lines
6.2 KiB

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