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.

268 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. 'age_limit': 10,
  91. },
  92. 'params': {
  93. 'skip_download': True, # requires ffmpeg
  94. },
  95. },
  96. {
  97. 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
  98. 'info_dict': {
  99. 'id': 'united-states-of-secrets',
  100. },
  101. 'playlist_count': 2,
  102. },
  103. {
  104. 'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
  105. 'info_dict': {
  106. 'id': '2280706814',
  107. 'display_id': 'player',
  108. 'ext': 'mp4',
  109. 'title': 'American Experience - Death and the Civil War',
  110. '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.',
  111. 'duration': 6705,
  112. 'thumbnail': 're:^https?://.*\.jpg$',
  113. },
  114. 'params': {
  115. 'skip_download': True, # requires ffmpeg
  116. },
  117. },
  118. {
  119. 'url': 'http://video.pbs.org/video/2365367186/',
  120. 'info_dict': {
  121. 'id': '2365367186',
  122. 'display_id': '2365367186',
  123. 'ext': 'mp4',
  124. 'title': 'To Catch A Comet - Full Episode',
  125. '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.',
  126. 'duration': 3342,
  127. 'thumbnail': 're:^https?://.*\.jpg$',
  128. },
  129. 'params': {
  130. 'skip_download': True, # requires ffmpeg
  131. },
  132. }
  133. ]
  134. def _extract_webpage(self, url):
  135. mobj = re.match(self._VALID_URL, url)
  136. presumptive_id = mobj.group('presumptive_id')
  137. display_id = presumptive_id
  138. if presumptive_id:
  139. webpage = self._download_webpage(url, display_id)
  140. upload_date = unified_strdate(self._search_regex(
  141. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  142. webpage, 'upload date', default=None))
  143. # tabbed frontline videos
  144. tabbed_videos = re.findall(
  145. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  146. if tabbed_videos:
  147. return tabbed_videos, presumptive_id, upload_date
  148. MEDIA_ID_REGEXES = [
  149. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  150. r'class="coveplayerid">([^<]+)<', # coveplayer
  151. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  152. ]
  153. media_id = self._search_regex(
  154. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  155. if media_id:
  156. return media_id, presumptive_id, upload_date
  157. url = self._search_regex(
  158. r'<iframe\s+[^>]*\s+src=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
  159. webpage, 'player URL')
  160. mobj = re.match(self._VALID_URL, url)
  161. player_id = mobj.group('player_id')
  162. if not display_id:
  163. display_id = player_id
  164. if player_id:
  165. player_page = self._download_webpage(
  166. url, display_id, note='Downloading player page',
  167. errnote='Could not download player page')
  168. video_id = self._search_regex(
  169. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  170. else:
  171. video_id = mobj.group('id')
  172. display_id = video_id
  173. return video_id, display_id, None
  174. def _real_extract(self, url):
  175. video_id, display_id, upload_date = self._extract_webpage(url)
  176. if isinstance(video_id, list):
  177. entries = [self.url_result(
  178. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  179. for vid_id in video_id]
  180. return self.playlist_result(entries, display_id)
  181. info = self._download_json(
  182. 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  183. display_id)
  184. formats = []
  185. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  186. redirect = info.get(encoding_name)
  187. if not redirect:
  188. continue
  189. redirect_url = redirect.get('url')
  190. if not redirect_url:
  191. continue
  192. redirect_info = self._download_json(
  193. redirect_url + '?format=json', display_id,
  194. 'Downloading %s video url info' % encoding_name)
  195. if redirect_info['status'] == 'error':
  196. if redirect_info['http_code'] == 403:
  197. message = (
  198. 'The video is not available in your region due to '
  199. 'right restrictions')
  200. else:
  201. message = redirect_info['message']
  202. raise ExtractorError(message, expected=True)
  203. format_url = redirect_info.get('url')
  204. if not format_url:
  205. continue
  206. if determine_ext(format_url) == 'm3u8':
  207. formats.extend(self._extract_m3u8_formats(
  208. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  209. else:
  210. formats.append({
  211. 'url': format_url,
  212. 'format_id': redirect.get('eeid'),
  213. })
  214. self._sort_formats(formats)
  215. rating_str = info.get('rating')
  216. if rating_str is not None:
  217. rating_str = rating_str.rpartition('-')[2]
  218. age_limit = US_RATINGS.get(rating_str)
  219. subtitles = {}
  220. closed_captions_url = info.get('closed_captions_url')
  221. if closed_captions_url:
  222. subtitles['en'] = [{
  223. 'ext': 'ttml',
  224. 'url': closed_captions_url,
  225. }]
  226. # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
  227. # Try turning it to 'program - title' naming scheme if possible
  228. alt_title = info.get('program', {}).get('title')
  229. if alt_title:
  230. info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
  231. return {
  232. 'id': video_id,
  233. 'display_id': display_id,
  234. 'title': info['title'],
  235. 'description': info['program'].get('description'),
  236. 'thumbnail': info.get('image_url'),
  237. 'duration': int_or_none(info.get('duration')),
  238. 'age_limit': age_limit,
  239. 'upload_date': upload_date,
  240. 'formats': formats,
  241. 'subtitles': subtitles,
  242. }