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.

117 lines
4.0 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. def _extract_ids(self, url):
  54. mobj = re.match(self._VALID_URL, url)
  55. presumptive_id = mobj.group('presumptive_id')
  56. display_id = presumptive_id
  57. if presumptive_id:
  58. webpage = self._download_webpage(url, display_id)
  59. MEDIA_ID_REGEXES = [
  60. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
  61. r'class="coveplayerid">([^<]+)<', # coveplayer
  62. ]
  63. media_id = self._search_regex(
  64. MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
  65. if media_id:
  66. return media_id, presumptive_id
  67. url = self._search_regex(
  68. r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  69. webpage, 'player URL')
  70. mobj = re.match(self._VALID_URL, url)
  71. player_id = mobj.group('player_id')
  72. if not display_id:
  73. display_id = player_id
  74. if player_id:
  75. player_page = self._download_webpage(
  76. url, display_id, note='Downloading player page',
  77. errnote='Could not download player page')
  78. video_id = self._search_regex(
  79. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  80. else:
  81. video_id = mobj.group('id')
  82. display_id = video_id
  83. return video_id, display_id
  84. def _real_extract(self, url):
  85. video_id, display_id = self._extract_ids(url)
  86. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  87. info = self._download_json(info_url, display_id)
  88. rating_str = info.get('rating')
  89. if rating_str is not None:
  90. rating_str = rating_str.rpartition('-')[2]
  91. age_limit = US_RATINGS.get(rating_str)
  92. return {
  93. 'id': video_id,
  94. 'title': info['title'],
  95. 'url': info['alternate_encoding']['url'],
  96. 'ext': 'mp4',
  97. 'description': info['program'].get('description'),
  98. 'thumbnail': info.get('image_url'),
  99. 'duration': info.get('duration'),
  100. 'age_limit': age_limit,
  101. }