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.

333 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|player)\.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. 'url': 'http://player.pbs.org/widget/partnerplayer/2365297708/?start=0&end=0&chapterbar=false&endscreen=false&topbar=true',
  171. 'only_matching': True,
  172. }
  173. ]
  174. _ERRORS = {
  175. 101: 'We\'re sorry, but this video is not yet available.',
  176. 403: 'We\'re sorry, but this video is not available in your region due to right restrictions.',
  177. 404: 'We are experiencing technical difficulties that are preventing us from playing the video at this time. Please check back again soon.',
  178. 410: 'This video has expired and is no longer available for online streaming.',
  179. }
  180. def _extract_webpage(self, url):
  181. mobj = re.match(self._VALID_URL, url)
  182. presumptive_id = mobj.group('presumptive_id')
  183. display_id = presumptive_id
  184. if presumptive_id:
  185. webpage = self._download_webpage(url, display_id)
  186. upload_date = unified_strdate(self._search_regex(
  187. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  188. webpage, 'upload date', default=None))
  189. # tabbed frontline videos
  190. tabbed_videos = re.findall(
  191. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  192. if tabbed_videos:
  193. return tabbed_videos, presumptive_id, upload_date
  194. MEDIA_ID_REGEXES = [
  195. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  196. r'class="coveplayerid">([^<]+)<', # coveplayer
  197. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  198. ]
  199. media_id = self._search_regex(
  200. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  201. if media_id:
  202. return media_id, presumptive_id, upload_date
  203. # Fronline video embedded via flp
  204. video_id = self._search_regex(
  205. r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid', default=None)
  206. if video_id:
  207. # pkg_id calculation is reverse engineered from
  208. # http://www.pbs.org/wgbh/pages/frontline/js/flp2012.js
  209. prg_id = self._search_regex(
  210. r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid')[7:]
  211. if 'q' in prg_id:
  212. prg_id = prg_id.split('q')[1]
  213. prg_id = int(prg_id, 16)
  214. getdir = self._download_json(
  215. 'http://www.pbs.org/wgbh/pages/frontline/.json/getdir/getdir%d.json' % prg_id,
  216. presumptive_id, 'Downloading getdir JSON',
  217. transform_source=strip_jsonp)
  218. return getdir['mid'], presumptive_id, upload_date
  219. for iframe in re.findall(r'(?s)<iframe(.+?)></iframe>', webpage):
  220. url = self._search_regex(
  221. r'src=(["\'])(?P<url>.+?partnerplayer.+?)\1', iframe,
  222. 'player URL', default=None, group='url')
  223. if url:
  224. break
  225. mobj = re.match(self._VALID_URL, url)
  226. player_id = mobj.group('player_id')
  227. if not display_id:
  228. display_id = player_id
  229. if player_id:
  230. player_page = self._download_webpage(
  231. url, display_id, note='Downloading player page',
  232. errnote='Could not download player page')
  233. video_id = self._search_regex(
  234. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  235. else:
  236. video_id = mobj.group('id')
  237. display_id = video_id
  238. return video_id, display_id, None
  239. def _real_extract(self, url):
  240. video_id, display_id, upload_date = self._extract_webpage(url)
  241. if isinstance(video_id, list):
  242. entries = [self.url_result(
  243. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  244. for vid_id in video_id]
  245. return self.playlist_result(entries, display_id)
  246. info = self._download_json(
  247. 'http://player.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  248. display_id)
  249. formats = []
  250. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  251. redirect = info.get(encoding_name)
  252. if not redirect:
  253. continue
  254. redirect_url = redirect.get('url')
  255. if not redirect_url:
  256. continue
  257. redirect_info = self._download_json(
  258. redirect_url + '?format=json', display_id,
  259. 'Downloading %s video url info' % encoding_name)
  260. if redirect_info['status'] == 'error':
  261. raise ExtractorError(
  262. '%s said: %s' % (
  263. self.IE_NAME,
  264. self._ERRORS.get(redirect_info['http_code'], redirect_info['message'])),
  265. expected=True)
  266. format_url = redirect_info.get('url')
  267. if not format_url:
  268. continue
  269. if determine_ext(format_url) == 'm3u8':
  270. formats.extend(self._extract_m3u8_formats(
  271. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  272. else:
  273. formats.append({
  274. 'url': format_url,
  275. 'format_id': redirect.get('eeid'),
  276. })
  277. self._sort_formats(formats)
  278. rating_str = info.get('rating')
  279. if rating_str is not None:
  280. rating_str = rating_str.rpartition('-')[2]
  281. age_limit = US_RATINGS.get(rating_str)
  282. subtitles = {}
  283. closed_captions_url = info.get('closed_captions_url')
  284. if closed_captions_url:
  285. subtitles['en'] = [{
  286. 'ext': 'ttml',
  287. 'url': closed_captions_url,
  288. }]
  289. # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
  290. # Try turning it to 'program - title' naming scheme if possible
  291. alt_title = info.get('program', {}).get('title')
  292. if alt_title:
  293. info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
  294. return {
  295. 'id': video_id,
  296. 'display_id': display_id,
  297. 'title': info['title'],
  298. 'description': info['program'].get('description'),
  299. 'thumbnail': info.get('image_url'),
  300. 'duration': int_or_none(info.get('duration')),
  301. 'age_limit': age_limit,
  302. 'upload_date': upload_date,
  303. 'formats': formats,
  304. 'subtitles': subtitles,
  305. }