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.

90 lines
3.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. _TEST = {
  19. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  20. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  21. 'info_dict': {
  22. 'id': '2365006249',
  23. 'ext': 'mp4',
  24. 'title': 'A More Perfect Union',
  25. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  26. 'duration': 3190,
  27. },
  28. }
  29. def _extract_ids(self, url):
  30. mobj = re.match(self._VALID_URL, url)
  31. presumptive_id = mobj.group('presumptive_id')
  32. display_id = presumptive_id
  33. if presumptive_id:
  34. webpage = self._download_webpage(url, display_id)
  35. # frontline video embed
  36. media_id = self._search_regex(
  37. r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",
  38. webpage, 'frontline video ID', fatal=False, default=None)
  39. if media_id:
  40. return media_id, presumptive_id
  41. url = self._search_regex(
  42. r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  43. webpage, 'player URL')
  44. mobj = re.match(self._VALID_URL, url)
  45. player_id = mobj.group('player_id')
  46. if not display_id:
  47. display_id = player_id
  48. if player_id:
  49. player_page = self._download_webpage(
  50. url, display_id, note='Downloading player page',
  51. errnote='Could not download player page')
  52. video_id = self._search_regex(
  53. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  54. else:
  55. video_id = mobj.group('id')
  56. display_id = video_id
  57. return video_id, display_id
  58. def _real_extract(self, url):
  59. video_id, display_id = self._extract_ids(url)
  60. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  61. info = self._download_json(info_url, display_id)
  62. rating_str = info.get('rating')
  63. if rating_str is not None:
  64. rating_str = rating_str.rpartition('-')[2]
  65. age_limit = US_RATINGS.get(rating_str)
  66. return {
  67. 'id': video_id,
  68. 'title': info['title'],
  69. 'url': info['alternate_encoding']['url'],
  70. 'ext': 'mp4',
  71. 'description': info['program'].get('description'),
  72. 'thumbnail': info.get('image_url'),
  73. 'duration': info.get('duration'),
  74. 'age_limit': age_limit,
  75. }