]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/pbs.py
[comcarcoff] Add unicode_literals declaration
[yt-dlp.git] / youtube_dl / extractor / pbs.py
CommitLineData
22e7f1a6
PH
1from __future__ import unicode_literals
2
9779b63b 3import re
9779b63b
JMF
4
5from .common import InfoExtractor
a1a530b0 6from ..utils import (
27aede90 7 unified_strdate,
a1a530b0
PH
8 US_RATINGS,
9)
9779b63b
JMF
10
11
12class PBSIE(InfoExtractor):
22e7f1a6
PH
13 _VALID_URL = r'''(?x)https?://
14 (?:
27aede90
PH
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)?/?(?:$|[?\#]) |
22e7f1a6 19 # Player
773c0b4b 20 video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
22e7f1a6
PH
21 )
22 '''
9779b63b 23
4d9bd478
S
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 },
9779b63b 35 },
cd6b4836
S
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 },
4d9bd478
S
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 },
756b046f
PH
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 }
27aede90
PH
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 }
9bbec552
S
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,
756b046f 90 }
4d9bd478 91 ]
9779b63b 92
27aede90 93 def _extract_webpage(self, url):
9779b63b 94 mobj = re.match(self._VALID_URL, url)
22e7f1a6
PH
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)
8d7d9d34 100
27aede90
PH
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
9bbec552
S
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
4d9bd478
S
111 MEDIA_ID_REGEXES = [
112 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'", # frontline video embed
113 r'class="coveplayerid">([^<]+)<', # coveplayer
27aede90 114 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>', # jwplayer
4d9bd478
S
115 ]
116
8d7d9d34 117 media_id = self._search_regex(
4d9bd478 118 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
8d7d9d34 119 if media_id:
27aede90 120 return media_id, presumptive_id, upload_date
8d7d9d34 121
22e7f1a6 122 url = self._search_regex(
756b046f 123 r'<iframe\s+(?:class|id)=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
22e7f1a6
PH
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
27aede90 140 return video_id, display_id, None
8d7d9d34
S
141
142 def _real_extract(self, url):
27aede90 143 video_id, display_id, upload_date = self._extract_webpage(url)
8d7d9d34 144
9bbec552
S
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
9779b63b 151 info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
22e7f1a6
PH
152 info = self._download_json(info_url, display_id)
153
a1a530b0
PH
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
22e7f1a6
PH
159 return {
160 'id': video_id,
27aede90 161 'display_id': display_id,
22e7f1a6
PH
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'),
a1a530b0 168 'age_limit': age_limit,
27aede90 169 'upload_date': upload_date,
22e7f1a6 170 }