apply ratelimit to f4m
[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     unified_strdate,
8     US_RATINGS,
9 )
10
11
12 class PBSIE(InfoExtractor):
13     _VALID_URL = r'''(?x)https?://
14         (?:
15            # Direct video URL
16            video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
17            # Article with embedded player (or direct video)
18            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
19            # Player
20            video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
21         )
22     '''
23
24     _TESTS = [
25         {
26             'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
27             'md5': 'ce1888486f0908d555a8093cac9a7362',
28             'info_dict': {
29                 'id': '2365006249',
30                 'ext': 'mp4',
31                 'title': 'A More Perfect Union',
32                 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
33                 'duration': 3190,
34             },
35         },
36         {
37             'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
38             'md5': '143c98aa54a346738a3d78f54c925321',
39             'info_dict': {
40                 'id': '2365297690',
41                 'ext': 'mp4',
42                 'title': 'Losing Iraq',
43                 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
44                 'duration': 5050,
45             },
46         },
47         {
48             'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
49             'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
50             'info_dict': {
51                 'id': '2201174722',
52                 'ext': 'mp4',
53                 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
54                 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
55                 'duration': 801,
56             },
57         },
58         {
59             'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
60             'md5': 'c62859342be2a0358d6c9eb306595978',
61             'info_dict': {
62                 'id': '2365297708',
63                 'ext': 'mp4',
64                 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
65                 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
66                 'duration': 6559,
67                 'thumbnail': 're:^https?://.*\.jpg$',
68             }
69         },
70         {
71             'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
72             'md5': '908f3e5473a693b266b84e25e1cf9703',
73             'info_dict': {
74                 'id': '2365160389',
75                 'display_id': 'killer-typhoon',
76                 'ext': 'mp4',
77                 'description': 'md5:c741d14e979fc53228c575894094f157',
78                 'title': 'Killer Typhoon',
79                 'duration': 3172,
80                 'thumbnail': 're:^https?://.*\.jpg$',
81                 'upload_date': '20140122',
82             }
83         }
84
85     ]
86
87     def _extract_webpage(self, url):
88         mobj = re.match(self._VALID_URL, url)
89
90         presumptive_id = mobj.group('presumptive_id')
91         display_id = presumptive_id
92         if presumptive_id:
93             webpage = self._download_webpage(url, display_id)
94
95             upload_date = unified_strdate(self._search_regex(
96                 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
97                 webpage, 'upload date', default=None))
98
99             MEDIA_ID_REGEXES = [
100                 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",  # frontline video embed
101                 r'class="coveplayerid">([^<]+)<',                       # coveplayer
102                 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>',  # jwplayer
103             ]
104
105             media_id = self._search_regex(
106                 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
107             if media_id:
108                 return media_id, presumptive_id, upload_date
109
110             url = self._search_regex(
111                 r'<iframe\s+(?:class|id)=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
112                 webpage, 'player URL')
113             mobj = re.match(self._VALID_URL, url)
114
115         player_id = mobj.group('player_id')
116         if not display_id:
117             display_id = player_id
118         if player_id:
119             player_page = self._download_webpage(
120                 url, display_id, note='Downloading player page',
121                 errnote='Could not download player page')
122             video_id = self._search_regex(
123                 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
124         else:
125             video_id = mobj.group('id')
126             display_id = video_id
127
128         return video_id, display_id, None
129
130     def _real_extract(self, url):
131         video_id, display_id, upload_date = self._extract_webpage(url)
132
133         info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
134         info = self._download_json(info_url, display_id)
135
136         rating_str = info.get('rating')
137         if rating_str is not None:
138             rating_str = rating_str.rpartition('-')[2]
139         age_limit = US_RATINGS.get(rating_str)
140
141         return {
142             'id': video_id,
143             'display_id': display_id,
144             'title': info['title'],
145             'url': info['alternate_encoding']['url'],
146             'ext': 'mp4',
147             'description': info['program'].get('description'),
148             'thumbnail': info.get('image_url'),
149             'duration': info.get('duration'),
150             'age_limit': age_limit,
151             'upload_date': upload_date,
152         }