Merge remote-tracking branch 'origin/master'
[youtube-dl] / youtube_dl / extractor / pbs.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7
8
9 class PBSIE(InfoExtractor):
10     _VALID_URL = r'''(?x)https?://
11         (?:
12             # Direct video URL
13             video\.pbs\.org/video/(?P<id>[0-9]+)/? |
14             # Article with embedded player
15            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+)/?(?:$|[?\#]) |
16            # Player
17            video\.pbs\.org/partnerplayer/(?P<player_id>[^/]+)/
18         )
19     '''
20
21     _TEST = {
22         'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
23         'md5': 'ce1888486f0908d555a8093cac9a7362',
24         'info_dict': {
25             'id': '2365006249',
26             'ext': 'mp4',
27             'title': 'A More Perfect Union',
28             'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
29             'duration': 3190,
30         },
31     }
32
33     def _real_extract(self, url):
34         mobj = re.match(self._VALID_URL, url)
35
36         presumptive_id = mobj.group('presumptive_id')
37         display_id = presumptive_id
38         if presumptive_id:
39             webpage = self._download_webpage(url, display_id)
40             url = self._search_regex(
41                 r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
42                 webpage, 'player URL')
43             mobj = re.match(self._VALID_URL, url)
44
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
58         info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
59         info = self._download_json(info_url, display_id)
60
61         return {
62             'id': video_id,
63             'title': info['title'],
64             'url': info['alternate_encoding']['url'],
65             'ext': 'mp4',
66             'description': info['program'].get('description'),
67             'thumbnail': info.get('image_url'),
68             'duration': info.get('duration'),
69         }