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.

291 lines
12 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': '2276541483',
  107. 'display_id': 'player',
  108. 'ext': 'mp4',
  109. 'title': 'American Experience - Death and the Civil War, Chapter 1',
  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': 682,
  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. 'skip': 'Expired',
  133. },
  134. {
  135. # Video embedded in iframe containing angle brackets as attribute's value (e.g.
  136. # "<iframe style='position: absolute;<br />\ntop: 0; left: 0;' ...", see
  137. # https://github.com/rg3/youtube-dl/issues/7059)
  138. 'url': 'http://www.pbs.org/food/features/a-chefs-life-season-3-episode-5-prickly-business/',
  139. 'info_dict': {
  140. 'id': '2365546844',
  141. 'display_id': 'a-chefs-life-season-3-episode-5-prickly-business',
  142. 'ext': 'mp4',
  143. 'title': "A Chef's Life - Season 3, Ep. 5: Prickly Business",
  144. 'description': 'md5:61db2ddf27c9912f09c241014b118ed1',
  145. 'duration': 1480,
  146. 'thumbnail': 're:^https?://.*\.jpg$',
  147. },
  148. 'params': {
  149. 'skip_download': True, # requires ffmpeg
  150. },
  151. }
  152. ]
  153. _ERRORS = {
  154. 101: 'We\'re sorry, but this video is not yet available.',
  155. 403: 'We\'re sorry, but this video is not available in your region due to right restrictions.',
  156. 404: 'We are experiencing technical difficulties that are preventing us from playing the video at this time. Please check back again soon.',
  157. 410: 'This video has expired and is no longer available for online streaming.',
  158. }
  159. def _extract_webpage(self, url):
  160. mobj = re.match(self._VALID_URL, url)
  161. presumptive_id = mobj.group('presumptive_id')
  162. display_id = presumptive_id
  163. if presumptive_id:
  164. webpage = self._download_webpage(url, display_id)
  165. upload_date = unified_strdate(self._search_regex(
  166. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  167. webpage, 'upload date', default=None))
  168. # tabbed frontline videos
  169. tabbed_videos = re.findall(
  170. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  171. if tabbed_videos:
  172. return tabbed_videos, presumptive_id, upload_date
  173. MEDIA_ID_REGEXES = [
  174. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  175. r'class="coveplayerid">([^<]+)<', # coveplayer
  176. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  177. ]
  178. media_id = self._search_regex(
  179. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  180. if media_id:
  181. return media_id, presumptive_id, upload_date
  182. url = self._search_regex(
  183. r'(?s)<iframe[^>]+?(?:[a-z-]+?=["\'].*?["\'][^>]+?)*?\bsrc=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
  184. webpage, 'player URL')
  185. mobj = re.match(self._VALID_URL, url)
  186. player_id = mobj.group('player_id')
  187. if not display_id:
  188. display_id = player_id
  189. if player_id:
  190. player_page = self._download_webpage(
  191. url, display_id, note='Downloading player page',
  192. errnote='Could not download player page')
  193. video_id = self._search_regex(
  194. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  195. else:
  196. video_id = mobj.group('id')
  197. display_id = video_id
  198. return video_id, display_id, None
  199. def _real_extract(self, url):
  200. video_id, display_id, upload_date = self._extract_webpage(url)
  201. if isinstance(video_id, list):
  202. entries = [self.url_result(
  203. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  204. for vid_id in video_id]
  205. return self.playlist_result(entries, display_id)
  206. info = self._download_json(
  207. 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  208. display_id)
  209. formats = []
  210. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  211. redirect = info.get(encoding_name)
  212. if not redirect:
  213. continue
  214. redirect_url = redirect.get('url')
  215. if not redirect_url:
  216. continue
  217. redirect_info = self._download_json(
  218. redirect_url + '?format=json', display_id,
  219. 'Downloading %s video url info' % encoding_name)
  220. if redirect_info['status'] == 'error':
  221. raise ExtractorError(
  222. '%s said: %s' % (
  223. self.IE_NAME,
  224. self._ERRORS.get(redirect_info['http_code'], redirect_info['message'])),
  225. expected=True)
  226. format_url = redirect_info.get('url')
  227. if not format_url:
  228. continue
  229. if determine_ext(format_url) == 'm3u8':
  230. formats.extend(self._extract_m3u8_formats(
  231. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  232. else:
  233. formats.append({
  234. 'url': format_url,
  235. 'format_id': redirect.get('eeid'),
  236. })
  237. self._sort_formats(formats)
  238. rating_str = info.get('rating')
  239. if rating_str is not None:
  240. rating_str = rating_str.rpartition('-')[2]
  241. age_limit = US_RATINGS.get(rating_str)
  242. subtitles = {}
  243. closed_captions_url = info.get('closed_captions_url')
  244. if closed_captions_url:
  245. subtitles['en'] = [{
  246. 'ext': 'ttml',
  247. 'url': closed_captions_url,
  248. }]
  249. # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
  250. # Try turning it to 'program - title' naming scheme if possible
  251. alt_title = info.get('program', {}).get('title')
  252. if alt_title:
  253. info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
  254. return {
  255. 'id': video_id,
  256. 'display_id': display_id,
  257. 'title': info['title'],
  258. 'description': info['program'].get('description'),
  259. 'thumbnail': info.get('image_url'),
  260. 'duration': int_or_none(info.get('duration')),
  261. 'age_limit': age_limit,
  262. 'upload_date': upload_date,
  263. 'formats': formats,
  264. 'subtitles': subtitles,
  265. }