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.

246 lines
8.9 KiB

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