[pbs] Extract coveplayerid (Closes #3522)
[youtube-dl] / youtube_dl / extractor / pbs.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     US_RATINGS,
8 )
9
10
11 class PBSIE(InfoExtractor):
12     _VALID_URL = r'''(?x)https?://
13         (?:
14             # Direct video URL
15             video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
16             # Article with embedded player
17            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+)/?(?:$|[?\#]) |
18            # Player
19            video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
20         )
21     '''
22
23     _TESTS = [
24         {
25             'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
26             'md5': 'ce1888486f0908d555a8093cac9a7362',
27             'info_dict': {
28                 'id': '2365006249',
29                 'ext': 'mp4',
30                 'title': 'A More Perfect Union',
31                 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
32                 'duration': 3190,
33             },
34         },
35         {
36             'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
37             'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
38             'info_dict': {
39                 'id': '2201174722',
40                 'ext': 'mp4',
41                 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
42                 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
43                 'duration': 801,
44             },
45         },
46     ]
47
48     def _extract_ids(self, url):
49         mobj = re.match(self._VALID_URL, url)
50
51         presumptive_id = mobj.group('presumptive_id')
52         display_id = presumptive_id
53         if presumptive_id:
54             webpage = self._download_webpage(url, display_id)
55
56             MEDIA_ID_REGEXES = [
57                 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",  # frontline video embed
58                 r'class="coveplayerid">([^<]+)<',                       # coveplayer
59             ]
60
61             media_id = self._search_regex(
62                 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
63             if media_id:
64                 return media_id, presumptive_id
65
66             url = self._search_regex(
67                 r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
68                 webpage, 'player URL')
69             mobj = re.match(self._VALID_URL, url)
70
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
84         return video_id, display_id
85
86     def _real_extract(self, url):
87         video_id, display_id = self._extract_ids(url)
88
89         info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
90         info = self._download_json(info_url, display_id)
91
92         rating_str = info.get('rating')
93         if rating_str is not None:
94             rating_str = rating_str.rpartition('-')[2]
95         age_limit = US_RATINGS.get(rating_str)
96
97         return {
98             'id': video_id,
99             'title': info['title'],
100             'url': info['alternate_encoding']['url'],
101             'ext': 'mp4',
102             'description': info['program'].get('description'),
103             'thumbnail': info.get('image_url'),
104             'duration': info.get('duration'),
105             'age_limit': age_limit,
106         }