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.

129 lines
4.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. US_RATINGS,
  6. )
  7. class PBSIE(InfoExtractor):
  8. _VALID_URL = r'''(?x)https?://
  9. (?:
  10. # Direct video URL
  11. video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
  12. # Article with embedded player
  13. (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+)/?(?:$|[?\#]) |
  14. # Player
  15. video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
  16. )
  17. '''
  18. _TESTS = [
  19. {
  20. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  21. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  22. 'info_dict': {
  23. 'id': '2365006249',
  24. 'ext': 'mp4',
  25. 'title': 'A More Perfect Union',
  26. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  27. 'duration': 3190,
  28. },
  29. },
  30. {
  31. 'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
  32. 'md5': '143c98aa54a346738a3d78f54c925321',
  33. 'info_dict': {
  34. 'id': '2365297690',
  35. 'ext': 'mp4',
  36. 'title': 'Losing Iraq',
  37. 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
  38. 'duration': 5050,
  39. },
  40. },
  41. {
  42. 'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
  43. 'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
  44. 'info_dict': {
  45. 'id': '2201174722',
  46. 'ext': 'mp4',
  47. 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
  48. 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
  49. 'duration': 801,
  50. },
  51. },
  52. {
  53. 'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
  54. 'md5': 'c62859342be2a0358d6c9eb306595978',
  55. 'info_dict': {
  56. 'id': '2365297708',
  57. 'ext': 'mp4',
  58. 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
  59. 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
  60. 'duration': 6559,
  61. 'thumbnail': 're:^https?://.*\.jpg$',
  62. }
  63. }
  64. ]
  65. def _extract_ids(self, url):
  66. mobj = re.match(self._VALID_URL, url)
  67. presumptive_id = mobj.group('presumptive_id')
  68. display_id = presumptive_id
  69. if presumptive_id:
  70. webpage = self._download_webpage(url, display_id)
  71. MEDIA_ID_REGEXES = [
  72. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  73. r'class="coveplayerid">([^<]+)<', # coveplayer
  74. ]
  75. media_id = self._search_regex(
  76. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  77. if media_id:
  78. return media_id, presumptive_id
  79. url = self._search_regex(
  80. r'<iframe\s+(?:class|id)=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  81. webpage, 'player URL')
  82. mobj = re.match(self._VALID_URL, url)
  83. player_id = mobj.group('player_id')
  84. if not display_id:
  85. display_id = player_id
  86. if player_id:
  87. player_page = self._download_webpage(
  88. url, display_id, note='Downloading player page',
  89. errnote='Could not download player page')
  90. video_id = self._search_regex(
  91. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  92. else:
  93. video_id = mobj.group('id')
  94. display_id = video_id
  95. return video_id, display_id
  96. def _real_extract(self, url):
  97. video_id, display_id = self._extract_ids(url)
  98. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  99. info = self._download_json(info_url, display_id)
  100. rating_str = info.get('rating')
  101. if rating_str is not None:
  102. rating_str = rating_str.rpartition('-')[2]
  103. age_limit = US_RATINGS.get(rating_str)
  104. return {
  105. 'id': video_id,
  106. 'title': info['title'],
  107. 'url': info['alternate_encoding']['url'],
  108. 'ext': 'mp4',
  109. 'description': info['program'].get('description'),
  110. 'thumbnail': info.get('image_url'),
  111. 'duration': info.get('duration'),
  112. 'age_limit': age_limit,
  113. }