pbs.py 8.3 KB

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