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.

77 lines
2.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. _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 _real_extract(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. url = self._search_regex(
  36. r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  37. webpage, 'player URL')
  38. mobj = re.match(self._VALID_URL, url)
  39. player_id = mobj.group('player_id')
  40. if not display_id:
  41. display_id = player_id
  42. if player_id:
  43. player_page = self._download_webpage(
  44. url, display_id, note='Downloading player page',
  45. errnote='Could not download player page')
  46. video_id = self._search_regex(
  47. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  48. else:
  49. video_id = mobj.group('id')
  50. display_id = video_id
  51. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  52. info = self._download_json(info_url, display_id)
  53. rating_str = info.get('rating')
  54. if rating_str is not None:
  55. rating_str = rating_str.rpartition('-')[2]
  56. age_limit = US_RATINGS.get(rating_str)
  57. return {
  58. 'id': video_id,
  59. 'title': info['title'],
  60. 'url': info['alternate_encoding']['url'],
  61. 'ext': 'mp4',
  62. 'description': info['program'].get('description'),
  63. 'thumbnail': info.get('image_url'),
  64. 'duration': info.get('duration'),
  65. 'age_limit': age_limit,
  66. }