pbs.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. determine_ext,
  7. int_or_none,
  8. unified_strdate,
  9. US_RATINGS,
  10. )
  11. class PBSIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)https?://
  13. (?:
  14. # Direct video URL
  15. video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
  16. # Article with embedded player (or direct video)
  17. (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
  18. # Player
  19. video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
  20. )
  21. '''
  22. _TESTS = [
  23. {
  24. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  25. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  26. 'info_dict': {
  27. 'id': '2365006249',
  28. 'ext': 'mp4',
  29. 'title': 'A More Perfect Union',
  30. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  31. 'duration': 3190,
  32. },
  33. 'params': {
  34. 'skip_download': True, # requires ffmpeg
  35. },
  36. },
  37. {
  38. 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
  39. 'md5': '143c98aa54a346738a3d78f54c925321',
  40. 'info_dict': {
  41. 'id': '2365297690',
  42. 'ext': 'mp4',
  43. 'title': 'Losing Iraq',
  44. 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
  45. 'duration': 5050,
  46. },
  47. 'params': {
  48. 'skip_download': True, # requires ffmpeg
  49. }
  50. },
  51. {
  52. 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
  53. 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
  54. 'info_dict': {
  55. 'id': '2201174722',
  56. 'ext': 'mp4',
  57. 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
  58. 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
  59. 'duration': 801,
  60. },
  61. },
  62. {
  63. 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
  64. 'md5': 'c62859342be2a0358d6c9eb306595978',
  65. 'info_dict': {
  66. 'id': '2365297708',
  67. 'ext': 'mp4',
  68. 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
  69. 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
  70. 'duration': 6559,
  71. 'thumbnail': 're:^https?://.*\.jpg$',
  72. },
  73. 'params': {
  74. 'skip_download': True, # requires ffmpeg
  75. },
  76. },
  77. {
  78. 'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
  79. 'md5': '908f3e5473a693b266b84e25e1cf9703',
  80. 'info_dict': {
  81. 'id': '2365160389',
  82. 'display_id': 'killer-typhoon',
  83. 'ext': 'mp4',
  84. 'description': 'md5:c741d14e979fc53228c575894094f157',
  85. 'title': 'Killer Typhoon',
  86. 'duration': 3172,
  87. 'thumbnail': 're:^https?://.*\.jpg$',
  88. 'upload_date': '20140122',
  89. },
  90. 'params': {
  91. 'skip_download': True, # requires ffmpeg
  92. },
  93. },
  94. {
  95. 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
  96. 'info_dict': {
  97. 'id': 'united-states-of-secrets',
  98. },
  99. 'playlist_count': 2,
  100. },
  101. {
  102. 'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
  103. 'info_dict': {
  104. 'id': '2280706814',
  105. 'display_id': 'player',
  106. 'ext': 'mp4',
  107. 'title': 'Death and the Civil War',
  108. '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.',
  109. 'duration': 6705,
  110. 'thumbnail': 're:^https?://.*\.jpg$',
  111. },
  112. 'params': {
  113. 'skip_download': True, # requires ffmpeg
  114. },
  115. }
  116. ]
  117. def _extract_webpage(self, url):
  118. mobj = re.match(self._VALID_URL, url)
  119. presumptive_id = mobj.group('presumptive_id')
  120. display_id = presumptive_id
  121. if presumptive_id:
  122. webpage = self._download_webpage(url, display_id)
  123. upload_date = unified_strdate(self._search_regex(
  124. r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
  125. webpage, 'upload date', default=None))
  126. # tabbed frontline videos
  127. tabbed_videos = re.findall(
  128. r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
  129. if tabbed_videos:
  130. return tabbed_videos, presumptive_id, upload_date
  131. MEDIA_ID_REGEXES = [
  132. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  133. r'class="coveplayerid">([^<]+)<', # coveplayer
  134. r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
  135. ]
  136. media_id = self._search_regex(
  137. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  138. if media_id:
  139. return media_id, presumptive_id, upload_date
  140. url = self._search_regex(
  141. r'<iframe\s+[^>]*\s+src=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
  142. webpage, 'player URL')
  143. mobj = re.match(self._VALID_URL, url)
  144. player_id = mobj.group('player_id')
  145. if not display_id:
  146. display_id = player_id
  147. if player_id:
  148. player_page = self._download_webpage(
  149. url, display_id, note='Downloading player page',
  150. errnote='Could not download player page')
  151. video_id = self._search_regex(
  152. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  153. else:
  154. video_id = mobj.group('id')
  155. display_id = video_id
  156. return video_id, display_id, None
  157. def _real_extract(self, url):
  158. video_id, display_id, upload_date = self._extract_webpage(url)
  159. if isinstance(video_id, list):
  160. entries = [self.url_result(
  161. 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
  162. for vid_id in video_id]
  163. return self.playlist_result(entries, display_id)
  164. info = self._download_json(
  165. 'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
  166. display_id)
  167. formats = []
  168. for encoding_name in ('recommended_encoding', 'alternate_encoding'):
  169. redirect = info.get(encoding_name)
  170. if not redirect:
  171. continue
  172. redirect_url = redirect.get('url')
  173. if not redirect_url:
  174. continue
  175. redirect_info = self._download_json(
  176. redirect_url + '?format=json', display_id,
  177. 'Downloading %s video url info' % encoding_name)
  178. if redirect_info['status'] == 'error':
  179. if redirect_info['http_code'] == 403:
  180. message = (
  181. 'The video is not available in your region due to '
  182. 'right restrictions')
  183. else:
  184. message = redirect_info['message']
  185. raise ExtractorError(message, expected=True)
  186. format_url = redirect_info.get('url')
  187. if not format_url:
  188. continue
  189. if determine_ext(format_url) == 'm3u8':
  190. formats.extend(self._extract_m3u8_formats(
  191. format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
  192. else:
  193. formats.append({
  194. 'url': format_url,
  195. 'format_id': redirect.get('eeid'),
  196. })
  197. self._sort_formats(formats)
  198. rating_str = info.get('rating')
  199. if rating_str is not None:
  200. rating_str = rating_str.rpartition('-')[2]
  201. age_limit = US_RATINGS.get(rating_str)
  202. return {
  203. 'id': video_id,
  204. 'display_id': display_id,
  205. 'title': info['title'],
  206. 'description': info['program'].get('description'),
  207. 'thumbnail': info.get('image_url'),
  208. 'duration': int_or_none(info.get('duration')),
  209. 'age_limit': age_limit,
  210. 'upload_date': upload_date,
  211. 'formats': formats,
  212. }