]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pbs.py
Merge tag 'upstream/2014.12.01'
[youtubedl] / 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 'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
86 'info_dict': {
87 'id': 'united-states-of-secrets',
88 },
89 'playlist_count': 2,
90 }
91 ]
92
93 def _extract_webpage(self, url):
94 mobj = re.match(self._VALID_URL, url)
95
96 presumptive_id = mobj.group('presumptive_id')
97 display_id = presumptive_id
98 if presumptive_id:
99 webpage = self._download_webpage(url, display_id)
100
101 upload_date = unified_strdate(self._search_regex(
102 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
103 webpage, 'upload date', default=None))
104
105 # tabbed frontline videos
106 tabbed_videos = re.findall(
107 r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
108 if tabbed_videos:
109 return tabbed_videos, presumptive_id, upload_date
110
111 MEDIA_ID_REGEXES = [
112 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
113 r'class="coveplayerid">([^<]+)<', # coveplayer
114 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
115 ]
116
117 media_id = self._search_regex(
118 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
119 if media_id:
120 return media_id, presumptive_id, upload_date
121
122 url = self._search_regex(
123 r'<iframe\s+(?:class|id)=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
124 webpage, 'player URL')
125 mobj = re.match(self._VALID_URL, url)
126
127 player_id = mobj.group('player_id')
128 if not display_id:
129 display_id = player_id
130 if player_id:
131 player_page = self._download_webpage(
132 url, display_id, note='Downloading player page',
133 errnote='Could not download player page')
134 video_id = self._search_regex(
135 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
136 else:
137 video_id = mobj.group('id')
138 display_id = video_id
139
140 return video_id, display_id, None
141
142 def _real_extract(self, url):
143 video_id, display_id, upload_date = self._extract_webpage(url)
144
145 if isinstance(video_id, list):
146 entries = [self.url_result(
147 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
148 for vid_id in video_id]
149 return self.playlist_result(entries, display_id)
150
151 info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
152 info = self._download_json(info_url, display_id)
153
154 rating_str = info.get('rating')
155 if rating_str is not None:
156 rating_str = rating_str.rpartition('-')[2]
157 age_limit = US_RATINGS.get(rating_str)
158
159 return {
160 'id': video_id,
161 'display_id': display_id,
162 'title': info['title'],
163 'url': info['alternate_encoding']['url'],
164 'ext': 'mp4',
165 'description': info['program'].get('description'),
166 'thumbnail': info.get('image_url'),
167 'duration': info.get('duration'),
168 'age_limit': age_limit,
169 'upload_date': upload_date,
170 }