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.

267 lines
10 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': 'Constitution USA with Peter Sagal - 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': 'FRONTLINE - 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': 'PBS NewsHour - 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': 'Great Performances - 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': 'NOVA - 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': 'American Experience - 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. 'url': 'http://video.pbs.org/video/2365367186/',
  119. 'info_dict': {
  120. 'id': '2365367186',
  121. 'display_id': '2365367186',
  122. 'ext': 'mp4',
  123. 'title': 'To Catch A Comet - Full Episode',
  124. 'description': 'On November 12, 2014, billions of kilometers from Earth, spacecraft orbiter Rosetta and lander Philae did what no other had dared to attempt \u2014 land on the volatile surface of a comet as it zooms around the sun at 67,000 km/hr. The European Space Agency hopes this mission can help peer into our past and unlock secrets of our origins.',
  125. 'duration': 3342,
  126. 'thumbnail': 're:^https?://.*\.jpg$',
  127. },
  128. 'params': {
  129. 'skip_download': True, # requires ffmpeg
  130. },
  131. }
  132. ]
  133. def _extract_webpage(self, url):
  134. mobj = re.match(self._VALID_URL, url)
  135. presumptive_id = mobj.group('presumptive_id')
  136. display_id = presumptive_id
  137. if presumptive_id:
  138. webpage = self._download_webpage(url, display_id)
  139. upload_date = unified_strdate(self._search_regex(
  140. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  141. webpage, 'upload date', default=None))
  142. # tabbed frontline videos
  143. tabbed_videos = re.findall(
  144. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  145. if tabbed_videos:
  146. return tabbed_videos, presumptive_id, upload_date
  147. MEDIA_ID_REGEXES = [
  148. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  149. r'class="coveplayerid">([^<]+)<', # coveplayer
  150. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  151. ]
  152. media_id = self._search_regex(
  153. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  154. if media_id:
  155. return media_id, presumptive_id, upload_date
  156. url = self._search_regex(
  157. r'<iframe\s+[^>]*\s+src=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
  158. webpage, 'player URL')
  159. mobj = re.match(self._VALID_URL, url)
  160. player_id = mobj.group('player_id')
  161. if not display_id:
  162. display_id = player_id
  163. if player_id:
  164. player_page = self._download_webpage(
  165. url, display_id, note='Downloading player page',
  166. errnote='Could not download player page')
  167. video_id = self._search_regex(
  168. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  169. else:
  170. video_id = mobj.group('id')
  171. display_id = video_id
  172. return video_id, display_id, None
  173. def _real_extract(self, url):
  174. video_id, display_id, upload_date = self._extract_webpage(url)
  175. if isinstance(video_id, list):
  176. entries = [self.url_result(
  177. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  178. for vid_id in video_id]
  179. return self.playlist_result(entries, display_id)
  180. info = self._download_json(
  181. 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  182. display_id)
  183. formats = []
  184. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  185. redirect = info.get(encoding_name)
  186. if not redirect:
  187. continue
  188. redirect_url = redirect.get('url')
  189. if not redirect_url:
  190. continue
  191. redirect_info = self._download_json(
  192. redirect_url + '?format=json', display_id,
  193. 'Downloading %s video url info' % encoding_name)
  194. if redirect_info['status'] == 'error':
  195. if redirect_info['http_code'] == 403:
  196. message = (
  197. 'The video is not available in your region due to '
  198. 'right restrictions')
  199. else:
  200. message = redirect_info['message']
  201. raise ExtractorError(message, expected=True)
  202. format_url = redirect_info.get('url')
  203. if not format_url:
  204. continue
  205. if determine_ext(format_url) == 'm3u8':
  206. formats.extend(self._extract_m3u8_formats(
  207. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  208. else:
  209. formats.append({
  210. 'url': format_url,
  211. 'format_id': redirect.get('eeid'),
  212. })
  213. self._sort_formats(formats)
  214. rating_str = info.get('rating')
  215. if rating_str is not None:
  216. rating_str = rating_str.rpartition('-')[2]
  217. age_limit = US_RATINGS.get(rating_str)
  218. subtitles = {}
  219. closed_captions_url = info.get('closed_captions_url')
  220. if closed_captions_url:
  221. subtitles['en'] = [{
  222. 'ext': 'ttml',
  223. 'url': closed_captions_url,
  224. }]
  225. # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
  226. # Try turning it to 'program - title' naming scheme if possible
  227. alt_title = info.get('program', {}).get('title')
  228. if alt_title:
  229. info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
  230. return {
  231. 'id': video_id,
  232. 'display_id': display_id,
  233. 'title': info['title'],
  234. 'description': info['program'].get('description'),
  235. 'thumbnail': info.get('image_url'),
  236. 'duration': int_or_none(info.get('duration')),
  237. 'age_limit': age_limit,
  238. 'upload_date': upload_date,
  239. 'formats': formats,
  240. 'subtitles': subtitles,
  241. }