[pbs] skip_download for m3u8 test cases
[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     ExtractorError,
8     determine_ext,
9     int_or_none,
10     unified_strdate,
11     US_RATINGS,
12 )
13
14
15 class PBSIE(InfoExtractor):
16     _VALID_URL = r'''(?x)https?://
17         (?:
18            # Direct video URL
19            video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
20            # Article with embedded player (or direct video)
21            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
22            # Player
23            video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
24         )
25     '''
26
27     _TESTS = [
28         {
29             'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
30             'md5': 'ce1888486f0908d555a8093cac9a7362',
31             'info_dict': {
32                 'id': '2365006249',
33                 'ext': 'mp4',
34                 'title': 'A More Perfect Union',
35                 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
36                 'duration': 3190,
37             },
38             'params': {
39                 'skip_download': True,  # requires ffmpeg
40             },
41         },
42         {
43             'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
44             'md5': '143c98aa54a346738a3d78f54c925321',
45             'info_dict': {
46                 'id': '2365297690',
47                 'ext': 'mp4',
48                 'title': 'Losing Iraq',
49                 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
50                 'duration': 5050,
51             },
52             'params': {
53                 'skip_download': True,  # requires ffmpeg
54             }
55         },
56         {
57             'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
58             'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
59             'info_dict': {
60                 'id': '2201174722',
61                 'ext': 'mp4',
62                 'title': 'Cyber Schools Gain Popularity, but Quality Questions Persist',
63                 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
64                 'duration': 801,
65             },
66         },
67         {
68             'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
69             'md5': 'c62859342be2a0358d6c9eb306595978',
70             'info_dict': {
71                 'id': '2365297708',
72                 'ext': 'mp4',
73                 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
74                 'title': 'Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
75                 'duration': 6559,
76                 'thumbnail': 're:^https?://.*\.jpg$',
77             },
78             'params': {
79                 'skip_download': True,  # requires ffmpeg
80             },
81         },
82         {
83             'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
84             'md5': '908f3e5473a693b266b84e25e1cf9703',
85             'info_dict': {
86                 'id': '2365160389',
87                 'display_id': 'killer-typhoon',
88                 'ext': 'mp4',
89                 'description': 'md5:c741d14e979fc53228c575894094f157',
90                 'title': 'Killer Typhoon',
91                 'duration': 3172,
92                 'thumbnail': 're:^https?://.*\.jpg$',
93                 'upload_date': '20140122',
94             },
95             'params': {
96                 'skip_download': True,  # requires ffmpeg
97             },
98         },
99         {
100             'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
101             'info_dict': {
102                 'id': 'united-states-of-secrets',
103             },
104             'playlist_count': 2,
105         },
106         {
107             'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
108             'info_dict': {
109                 'id': '2280706814',
110                 'display_id': 'player',
111                 'ext': 'mp4',
112                 'title': 'Death and the Civil War',
113                 'description': 'American Experience, TV’s most-watched history series, brings to life the compelling stories from our past that inform our understanding of the world today.',
114                 'duration': 6705,
115                 'thumbnail': 're:^https?://.*\.jpg$',
116             },
117             'params': {
118                 'skip_download': True,  # requires ffmpeg
119             },
120         }
121     ]
122
123     def _extract_webpage(self, url):
124         mobj = re.match(self._VALID_URL, url)
125
126         presumptive_id = mobj.group('presumptive_id')
127         display_id = presumptive_id
128         if presumptive_id:
129             webpage = self._download_webpage(url, display_id)
130
131             upload_date = unified_strdate(self._search_regex(
132                 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
133                 webpage, 'upload date', default=None))
134
135             # tabbed frontline videos
136             tabbed_videos = re.findall(
137                 r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
138             if tabbed_videos:
139                 return tabbed_videos, presumptive_id, upload_date
140
141             MEDIA_ID_REGEXES = [
142                 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",  # frontline video embed
143                 r'class="coveplayerid">([^<]+)<',                       # coveplayer
144                 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>',  # jwplayer
145             ]
146
147             media_id = self._search_regex(
148                 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
149             if media_id:
150                 return media_id, presumptive_id, upload_date
151
152             url = self._search_regex(
153                 r'<iframe\s+[^>]*\s+src=["\']([^\'"]+partnerplayer[^\'"]+)["\']',
154                 webpage, 'player URL')
155             mobj = re.match(self._VALID_URL, url)
156
157         player_id = mobj.group('player_id')
158         if not display_id:
159             display_id = player_id
160         if player_id:
161             player_page = self._download_webpage(
162                 url, display_id, note='Downloading player page',
163                 errnote='Could not download player page')
164             video_id = self._search_regex(
165                 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
166         else:
167             video_id = mobj.group('id')
168             display_id = video_id
169
170         return video_id, display_id, None
171
172     def _real_extract(self, url):
173         video_id, display_id, upload_date = self._extract_webpage(url)
174
175         if isinstance(video_id, list):
176             entries = [self.url_result(
177                 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
178                 for vid_id in video_id]
179             return self.playlist_result(entries, display_id)
180
181         info = self._download_json(
182             'http://video.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
183             display_id)
184
185         formats = []
186         for encoding_name in ('recommended_encoding', 'alternate_encoding'):
187             redirect = info.get(encoding_name)
188             if not redirect:
189                 continue
190             redirect_url = redirect.get('url')
191             if not redirect_url:
192                 continue
193
194             redirect_info = self._download_json(
195                 redirect_url + '?format=json', display_id,
196                 'Downloading %s video url info' % encoding_name)
197
198             if redirect_info['status'] == 'error':
199                 if redirect_info['http_code'] == 403:
200                     message = (
201                         'The video is not available in your region due to '
202                         'right restrictions')
203                 else:
204                     message = redirect_info['message']
205                 raise ExtractorError(message, expected=True)
206
207             format_url = redirect_info.get('url')
208             if not format_url:
209                 continue
210
211             if determine_ext(format_url) == 'm3u8':
212                 formats.extend(self._extract_m3u8_formats(
213                     format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
214             else:
215                 formats.append({
216                     'url': format_url,
217                     'format_id': redirect.get('eeid'),
218                 })
219         self._sort_formats(formats)
220
221         rating_str = info.get('rating')
222         if rating_str is not None:
223             rating_str = rating_str.rpartition('-')[2]
224         age_limit = US_RATINGS.get(rating_str)
225
226         return {
227             'id': video_id,
228             'display_id': display_id,
229             'title': info['title'],
230             'description': info['program'].get('description'),
231             'thumbnail': info.get('image_url'),
232             'duration': int_or_none(info.get('duration')),
233             'age_limit': age_limit,
234             'upload_date': upload_date,
235             'formats': formats,
236         }