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.

329 lines
13 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. strip_jsonp,
  10. unified_strdate,
  11. US_RATINGS,
  12. )
  13. class PBSIE(InfoExtractor):
  14. _VALID_URL = r'''(?x)https?://
  15. (?:
  16. # Direct video URL
  17. video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
  18. # Article with embedded player (or direct video)
  19. (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
  20. # Player
  21. video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
  22. )
  23. '''
  24. _TESTS = [
  25. {
  26. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  27. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  28. 'info_dict': {
  29. 'id': '2365006249',
  30. 'ext': 'mp4',
  31. 'title': 'Constitution USA with Peter Sagal - A More Perfect Union',
  32. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  33. 'duration': 3190,
  34. },
  35. 'params': {
  36. 'skip_download': True, # requires ffmpeg
  37. },
  38. },
  39. {
  40. 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
  41. 'md5': '143c98aa54a346738a3d78f54c925321',
  42. 'info_dict': {
  43. 'id': '2365297690',
  44. 'ext': 'mp4',
  45. 'title': 'FRONTLINE - Losing Iraq',
  46. 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
  47. 'duration': 5050,
  48. },
  49. 'params': {
  50. 'skip_download': True, # requires ffmpeg
  51. }
  52. },
  53. {
  54. 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
  55. 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
  56. 'info_dict': {
  57. 'id': '2201174722',
  58. 'ext': 'mp4',
  59. 'title': 'PBS NewsHour - Cyber Schools Gain Popularity, but Quality Questions Persist',
  60. 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
  61. 'duration': 801,
  62. },
  63. },
  64. {
  65. 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
  66. 'md5': 'c62859342be2a0358d6c9eb306595978',
  67. 'info_dict': {
  68. 'id': '2365297708',
  69. 'ext': 'mp4',
  70. 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
  71. 'title': 'Great Performances - Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
  72. 'duration': 6559,
  73. 'thumbnail': 're:^https?://.*\.jpg$',
  74. },
  75. 'params': {
  76. 'skip_download': True, # requires ffmpeg
  77. },
  78. },
  79. {
  80. 'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
  81. 'md5': '908f3e5473a693b266b84e25e1cf9703',
  82. 'info_dict': {
  83. 'id': '2365160389',
  84. 'display_id': 'killer-typhoon',
  85. 'ext': 'mp4',
  86. 'description': 'md5:c741d14e979fc53228c575894094f157',
  87. 'title': 'NOVA - Killer Typhoon',
  88. 'duration': 3172,
  89. 'thumbnail': 're:^https?://.*\.jpg$',
  90. 'upload_date': '20140122',
  91. 'age_limit': 10,
  92. },
  93. 'params': {
  94. 'skip_download': True, # requires ffmpeg
  95. },
  96. },
  97. {
  98. 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
  99. 'info_dict': {
  100. 'id': 'united-states-of-secrets',
  101. },
  102. 'playlist_count': 2,
  103. },
  104. {
  105. 'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
  106. 'info_dict': {
  107. 'id': '2276541483',
  108. 'display_id': 'player',
  109. 'ext': 'mp4',
  110. 'title': 'American Experience - Death and the Civil War, Chapter 1',
  111. '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.',
  112. 'duration': 682,
  113. 'thumbnail': 're:^https?://.*\.jpg$',
  114. },
  115. 'params': {
  116. 'skip_download': True, # requires ffmpeg
  117. },
  118. },
  119. {
  120. 'url': 'http://video.pbs.org/video/2365367186/',
  121. 'info_dict': {
  122. 'id': '2365367186',
  123. 'display_id': '2365367186',
  124. 'ext': 'mp4',
  125. 'title': 'To Catch A Comet - Full Episode',
  126. '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.',
  127. 'duration': 3342,
  128. 'thumbnail': 're:^https?://.*\.jpg$',
  129. },
  130. 'params': {
  131. 'skip_download': True, # requires ffmpeg
  132. },
  133. 'skip': 'Expired',
  134. },
  135. {
  136. # Video embedded in iframe containing angle brackets as attribute's value (e.g.
  137. # "<iframe style='position: absolute;<br />\ntop: 0; left: 0;' ...", see
  138. # https://github.com/rg3/youtube-dl/issues/7059)
  139. 'url': 'http://www.pbs.org/food/features/a-chefs-life-season-3-episode-5-prickly-business/',
  140. 'info_dict': {
  141. 'id': '2365546844',
  142. 'display_id': 'a-chefs-life-season-3-episode-5-prickly-business',
  143. 'ext': 'mp4',
  144. 'title': "A Chef's Life - Season 3, Ep. 5: Prickly Business",
  145. 'description': 'md5:61db2ddf27c9912f09c241014b118ed1',
  146. 'duration': 1480,
  147. 'thumbnail': 're:^https?://.*\.jpg$',
  148. },
  149. 'params': {
  150. 'skip_download': True, # requires ffmpeg
  151. },
  152. },
  153. {
  154. # Frontline video embedded via flp2012.js
  155. 'url': 'http://www.pbs.org/wgbh/pages/frontline/the-atomic-artists',
  156. 'info_dict': {
  157. 'id': '2070868960',
  158. 'display_id': 'the-atomic-artists',
  159. 'ext': 'mp4',
  160. 'title': 'FRONTLINE - The Atomic Artists',
  161. 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
  162. 'duration': 723,
  163. 'thumbnail': 're:^https?://.*\.jpg$',
  164. },
  165. 'params': {
  166. 'skip_download': True, # requires ffmpeg
  167. },
  168. }
  169. ]
  170. _ERRORS = {
  171. 101: 'We\'re sorry, but this video is not yet available.',
  172. 403: 'We\'re sorry, but this video is not available in your region due to right restrictions.',
  173. 404: 'We are experiencing technical difficulties that are preventing us from playing the video at this time. Please check back again soon.',
  174. 410: 'This video has expired and is no longer available for online streaming.',
  175. }
  176. def _extract_webpage(self, url):
  177. mobj = re.match(self._VALID_URL, url)
  178. presumptive_id = mobj.group('presumptive_id')
  179. display_id = presumptive_id
  180. if presumptive_id:
  181. webpage = self._download_webpage(url, display_id)
  182. upload_date = unified_strdate(self._search_regex(
  183. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  184. webpage, 'upload date', default=None))
  185. # tabbed frontline videos
  186. tabbed_videos = re.findall(
  187. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  188. if tabbed_videos:
  189. return tabbed_videos, presumptive_id, upload_date
  190. MEDIA_ID_REGEXES = [
  191. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  192. r'class="coveplayerid">([^<]+)<', # coveplayer
  193. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  194. ]
  195. media_id = self._search_regex(
  196. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  197. if media_id:
  198. return media_id, presumptive_id, upload_date
  199. # Fronline video embedded via flp
  200. video_id = self._search_regex(
  201. r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid', default=None)
  202. if video_id:
  203. # pkg_id calculation is reverse engineered from
  204. # http://www.pbs.org/wgbh/pages/frontline/js/flp2012.js
  205. prg_id = self._search_regex(
  206. r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid')[7:]
  207. if 'q' in prg_id:
  208. prg_id = prg_id.split('q')[1]
  209. prg_id = int(prg_id, 16)
  210. getdir = self._download_json(
  211. 'http://www.pbs.org/wgbh/pages/frontline/.json/getdir/getdir%d.json' % prg_id,
  212. presumptive_id, 'Downloading getdir JSON',
  213. transform_source=strip_jsonp)
  214. return getdir['mid'], presumptive_id, upload_date
  215. for iframe in re.findall(r'(?s)<iframe(.+?)></iframe>', webpage):
  216. url = self._search_regex(
  217. r'src=(["\'])(?P<url>.+?partnerplayer.+?)\1', iframe,
  218. 'player URL', default=None, group='url')
  219. if url:
  220. break
  221. mobj = re.match(self._VALID_URL, url)
  222. player_id = mobj.group('player_id')
  223. if not display_id:
  224. display_id = player_id
  225. if player_id:
  226. player_page = self._download_webpage(
  227. url, display_id, note='Downloading player page',
  228. errnote='Could not download player page')
  229. video_id = self._search_regex(
  230. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  231. else:
  232. video_id = mobj.group('id')
  233. display_id = video_id
  234. return video_id, display_id, None
  235. def _real_extract(self, url):
  236. video_id, display_id, upload_date = self._extract_webpage(url)
  237. if isinstance(video_id, list):
  238. entries = [self.url_result(
  239. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  240. for vid_id in video_id]
  241. return self.playlist_result(entries, display_id)
  242. info = self._download_json(
  243. 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  244. display_id)
  245. formats = []
  246. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  247. redirect = info.get(encoding_name)
  248. if not redirect:
  249. continue
  250. redirect_url = redirect.get('url')
  251. if not redirect_url:
  252. continue
  253. redirect_info = self._download_json(
  254. redirect_url + '?format=json', display_id,
  255. 'Downloading %s video url info' % encoding_name)
  256. if redirect_info['status'] == 'error':
  257. raise ExtractorError(
  258. '%s said: %s' % (
  259. self.IE_NAME,
  260. self._ERRORS.get(redirect_info['http_code'], redirect_info['message'])),
  261. expected=True)
  262. format_url = redirect_info.get('url')
  263. if not format_url:
  264. continue
  265. if determine_ext(format_url) == 'm3u8':
  266. formats.extend(self._extract_m3u8_formats(
  267. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  268. else:
  269. formats.append({
  270. 'url': format_url,
  271. 'format_id': redirect.get('eeid'),
  272. })
  273. self._sort_formats(formats)
  274. rating_str = info.get('rating')
  275. if rating_str is not None:
  276. rating_str = rating_str.rpartition('-')[2]
  277. age_limit = US_RATINGS.get(rating_str)
  278. subtitles = {}
  279. closed_captions_url = info.get('closed_captions_url')
  280. if closed_captions_url:
  281. subtitles['en'] = [{
  282. 'ext': 'ttml',
  283. 'url': closed_captions_url,
  284. }]
  285. # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
  286. # Try turning it to 'program - title' naming scheme if possible
  287. alt_title = info.get('program', {}).get('title')
  288. if alt_title:
  289. info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
  290. return {
  291. 'id': video_id,
  292. 'display_id': display_id,
  293. 'title': info['title'],
  294. 'description': info['program'].get('description'),
  295. 'thumbnail': info.get('image_url'),
  296. 'duration': int_or_none(info.get('duration')),
  297. 'age_limit': age_limit,
  298. 'upload_date': upload_date,
  299. 'formats': formats,
  300. 'subtitles': subtitles,
  301. }