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.

69 lines
2.3 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. class PBSIE(InfoExtractor):
  6. _VALID_URL = r'''(?x)https?://
  7. (?:
  8. # Direct video URL
  9. video\.pbs\.org/video/(?P<id>[0-9]+)/? |
  10. # Article with embedded player
  11. (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+)/?(?:$|[?\#]) |
  12. # Player
  13. video\.pbs\.org/partnerplayer/(?P<player_id>[^/]+)/
  14. )
  15. '''
  16. _TEST = {
  17. 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
  18. 'md5': 'ce1888486f0908d555a8093cac9a7362',
  19. 'info_dict': {
  20. 'id': '2365006249',
  21. 'ext': 'mp4',
  22. 'title': 'A More Perfect Union',
  23. 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
  24. 'duration': 3190,
  25. },
  26. }
  27. def _real_extract(self, url):
  28. mobj = re.match(self._VALID_URL, url)
  29. presumptive_id = mobj.group('presumptive_id')
  30. display_id = presumptive_id
  31. if presumptive_id:
  32. webpage = self._download_webpage(url, display_id)
  33. url = self._search_regex(
  34. r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
  35. webpage, 'player URL')
  36. mobj = re.match(self._VALID_URL, url)
  37. player_id = mobj.group('player_id')
  38. if not display_id:
  39. display_id = player_id
  40. if player_id:
  41. player_page = self._download_webpage(
  42. url, display_id, note='Downloading player page',
  43. errnote='Could not download player page')
  44. video_id = self._search_regex(
  45. r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
  46. else:
  47. video_id = mobj.group('id')
  48. display_id = video_id
  49. info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
  50. info = self._download_json(info_url, display_id)
  51. return {
  52. 'id': video_id,
  53. 'title': info['title'],
  54. 'url': info['alternate_encoding']['url'],
  55. 'ext': 'mp4',
  56. 'description': info['program'].get('description'),
  57. 'thumbnail': info.get('image_url'),
  58. 'duration': info.get('duration'),
  59. }