[pbs] Clean up stations list from duplicates
[youtube-dl] / youtube_dl / extractor / pbs.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     determine_ext,
10     int_or_none,
11     strip_jsonp,
12     unified_strdate,
13     US_RATINGS,
14 )
15
16
17 class PBSIE(InfoExtractor):
18     _STATIONS = (
19         ('aptv.org', 'APT - Alabama Public Television (WBIQ)'),  # http://aptv.org/
20         ('gpb.org', 'GPB/Georgia Public Broadcasting (WGTV)'),  # http://www.gpb.org/
21         ('mpbonline.org', 'Mississippi Public Broadcasting (WMPN)'),  # http://www.mpbonline.org
22         ('wnpt.org', 'Nashville Public Television (WNPT)'),  # http://www.wnpt.org
23         ('wfsu.org', 'WFSU-TV (WFSU)'),  # http://wfsu.org/
24         ('wsre.org', 'WSRE (WSRE)'),  # http://www.wsre.org
25         ('wtcitv.org', 'WTCI (WTCI)'),  # http://www.wtcitv.org
26         ('pba.org', 'WPBA/Channel 30 (WPBA)'),  # http://pba.org/
27         ('alaskapublic.org', 'Alaska Public Media (KAKM)'),  # http://alaskapublic.org/kakm
28         ('kuac.org', 'KUAC (KUAC)'),  # http://kuac.org/kuac-tv/
29         ('ktoo.org', '360 North (KTOO)'),  # http://www.ktoo.org/
30         ('azpm.org', 'KUAT 6 (KUAT)'),  # http://www.azpm.org/
31         ('azpbs.org', 'Arizona PBS (KAET)'),  # http://www.azpbs.org
32         ('newmexicopbs.org', 'KNME-TV/Channel 5 (KNME)'),  # http://www.newmexicopbs.org/
33         ('vegaspbs.org', 'Vegas PBS (KLVX)'),  # http://vegaspbs.org/
34         ('aetn.org', 'AETN/ARKANSAS ETV NETWORK (KETS)'),  # http://www.aetn.org/
35         ('ket.org', 'KET (WKLE)'),  # http://www.ket.org/
36         ('wkno.org', 'WKNO/Channel 10 (WKNO)'),  # http://www.wkno.org/
37         ('lpb.org', 'LPB/LOUISIANA PUBLIC BROADCASTING (WLPB)'),  # http://www.lpb.org/
38         ('oeta.tv', 'OETA (KETA)'),  # http://www.oeta.tv
39         ('optv.org', 'Ozarks Public Television (KOZK)'),  # http://www.optv.org/
40         ('wsiu.org', 'WSIU Public Broadcasting (WSIU)'),  # http://www.wsiu.org/
41         ('keet.org', 'KEET TV (KEET)'),  # http://www.keet.org
42         ('kixe.org', 'KIXE/Channel 9 (KIXE)'),  # http://kixe.org/
43         ('kpbs.org', 'KPBS San Diego (KPBS)'),  # http://www.kpbs.org/
44         ('kqed.org', 'KQED (KQED)'),  # http://www.kqed.org
45         ('kvie.org', 'KVIE Public Television (KVIE)'),  # http://www.kvie.org
46         ('pbssocal.org', 'PBS SoCal/KOCE (KOCE)'),  # http://www.pbssocal.org/
47         ('valleypbs.org', 'ValleyPBS (KVPT)'),  # http://www.valleypbs.org/
48         ('cptv.org', 'CONNECTICUT PUBLIC TELEVISION (WEDH)'),  # http://cptv.org
49         ('knpb.org', 'KNPB Channel 5 (KNPB)'),  # http://www.knpb.org/
50         ('soptv.org', 'SOPTV (KSYS)'),  # http://www.soptv.org
51         ('klcs.org', 'KLCS/Channel 58 (KLCS)'),  # http://www.klcs.org
52         ('krcb.org', 'KRCB Television & Radio (KRCB)'),  # http://www.krcb.org
53         ('kvcr.org', 'KVCR TV/DT/FM :: Vision for the Future (KVCR)'),  # http://kvcr.org
54         ('rmpbs.org', 'Rocky Mountain PBS (KRMA)'),  # http://www.rmpbs.org
55         ('kenw.org', 'KENW-TV3 (KENW)'),  # http://www.kenw.org
56         ('kued.org', 'KUED Channel 7 (KUED)'),  # http://www.kued.org
57         ('wyomingpbs.org', 'Wyoming PBS (KCWC)'),  # http://www.wyomingpbs.org
58         ('cpt12.org', 'Colorado Public Television / KBDI 12 (KBDI)'),  # http://www.cpt12.org/
59         ('kbyutv.org', 'KBYU-TV (KBYU)'),  # http://www.kbyutv.org/
60         ('thirteen.org', 'Thirteen/WNET New York (WNET)'),  # http://www.thirteen.org
61         ('wgbh.org', 'WGBH/Channel 2 (WGBH)'),  # http://wgbh.org
62         ('wgby.org', 'WGBY (WGBY)'),  # http://www.wgby.org
63         ('njtvonline.org', 'NJTV Public Media NJ (WNJT)'),  # http://www.njtvonline.org/
64         ('ripbs.org', 'Rhode Island PBS (WSBE)'),  # http://www.ripbs.org/home/
65         ('wliw.org', 'WLIW21 (WLIW)'),  # http://www.wliw.org/
66         ('mpt.org', 'mpt/Maryland Public Television (WMPB)'),  # http://www.mpt.org
67         ('weta.org', 'WETA Television and Radio (WETA)'),  # http://www.weta.org
68         ('whyy.org', 'WHYY (WHYY)'),  # http://www.whyy.org
69         ('wlvt.org', 'PBS 39 (WLVT)'),  # http://www.wlvt.org/
70         ('wvpt.net', 'WVPT - Your Source for PBS and More! (WVPT)'),  # http://www.wvpt.net
71         ('whut.org', 'Howard University Television (WHUT)'),  # http://www.whut.org
72         ('wedu.org', 'WEDU PBS (WEDU)'),  # http://www.wedu.org
73         ('wgcu.org', 'WGCU Public Media (WGCU)'),  # http://www.wgcu.org/
74         ('wjct.org', 'WJCT Public Broadcasting (WJCT)'),  # http://www.wjct.org
75         ('wpbt2.org', 'WPBT2 (WPBT)'),  # http://www.wpbt2.org
76         ('wucftv.org', 'WUCF TV (WUCF)'),  # http://wucftv.org
77         ('wuft.org', 'WUFT/Channel 5 (WUFT)'),  # http://www.wuft.org
78         ('wxel.org', 'WXEL/Channel 42 (WXEL)'),  # http://www.wxel.org/home/
79         ('wlrn.org', 'WLRN/Channel 17 (WLRN)'),  # http://www.wlrn.org/
80         ('wusf.org', 'WUSF Public Broadcasting (WUSF)'),  # http://wusf.org/
81         ('scetv.org', 'ETV (WRLK)'),  # http://www.scetv.org
82         ('unctv.org', 'UNC-TV (WUNC)'),  # http://www.unctv.org/
83         ('pbsguam.org', 'PBS Guam (KGTF)'),  # http://www.pbsguam.org/
84         ('pbshawaii.org', 'PBS Hawaii - Oceanic Cable Channel 10 (KHET)'),  # http://www.pbshawaii.org/
85         ('idahoptv.org', 'Idaho Public Television (KAID)'),  # http://idahoptv.org
86         ('ksps.org', 'KSPS (KSPS)'),  # http://www.ksps.org/home/
87         ('opb.org', 'OPB (KOPB)'),  # http://www.opb.org
88         ('kwsu.org', 'KWSU/Channel 10 & KTNW/Channel 31 (KWSU)'),  # http://www.kwsu.org
89         ('illinois.edu', 'WILL-TV (WILL)'),  # http://will.illinois.edu/
90         ('wsec.tv', 'Network Knowledge - WSEC/Springfield (WSEC)'),  # http://www.wsec.tv
91         ('wttw.com', 'WTTW11 (WTTW)'),  # http://www.wttw.com/
92         ('wtvp.org', 'WTVP & WTVP.org, Public Media for Central Illinois (WTVP)'),  # http://www.wtvp.org/
93         ('iptv.org', 'Iowa Public Television/IPTV (KDIN)'),  # http://www.iptv.org/
94         ('ninenet.org', 'Nine Network (KETC)'),  # http://www.ninenet.org
95         ('wfwa.org', 'PBS39 Fort Wayne (WFWA)'),  # http://wfwa.org/
96         ('wfyi.org', 'WFYI Indianapolis (WFYI)'),  # http://www.wfyi.org
97         ('mptv.org', 'Milwaukee Public Television (WMVS)'),  # http://www.mptv.org
98         ('wnin.org', 'WNIN (WNIN)'),  # http://www.wnin.org/
99         ('wnit.org', 'WNIT Public Television (WNIT)'),  # http://www.wnit.org/
100         ('wpt.org', 'WPT (WPNE)'),  # http://www.wpt.org/
101         ('wvut.org', 'WVUT/Channel 22 (WVUT)'),  # http://wvut.org/
102         ('weiu.net', 'WEIU/Channel 51 (WEIU)'),  # http://www.weiu.net
103         ('wqpt.org', 'WQPT-TV (WQPT)'),  # http://www.wqpt.org
104         ('wycc.org', 'WYCC PBS Chicago (WYCC)'),  # http://www.wycc.org
105         ('lakeshorepublicmedia.org', 'Lakeshore Public Television (WYIN)'),  # http://lakeshorepublicmedia.org/
106         ('wipb.org', 'WIPB-TV (WIPB)'),  # http://wipb.org
107         ('indianapublicmedia.org', 'WTIU (WTIU)'),  # http://indianapublicmedia.org/tv/
108         ('cetconnect.org', 'CET  (WCET)'),  # http://www.cetconnect.org
109         ('thinktv.org', 'ThinkTVNetwork (WPTD)'),  # http://www.thinktv.org
110         ('wbgu.org', 'WBGU-TV (WBGU)'),  # http://wbgu.org
111         ('wgvu.org', 'WGVU TV (WGVU)'),  # http://www.wgvu.org/
112         ('netnebraska.org', 'NET1 (KUON)'),  # http://netnebraska.org
113         ('pioneer.org', 'Pioneer Public Television (KWCM)'),  # http://www.pioneer.org
114         ('sdpb.org', 'SDPB Television (KUSD)'),  # http://www.sdpb.org
115         ('tpt.org', 'TPT (KTCA)'),  # http://www.tpt.org
116         ('ksmq.org', 'KSMQ (KSMQ)'),  # http://www.ksmq.org/
117         ('kpts.org', 'KPTS/Channel 8 (KPTS)'),  # http://www.kpts.org/
118         ('ktwu.org', 'KTWU/Channel 11 (KTWU)'),  # http://ktwu.org
119         ('shptv.org', 'Smoky Hills Public Television (KOOD)'),  # http://www.shptv.org
120         ('kcpt.org', 'KCPT Kansas City Public Television (KCPT)'),  # http://kcpt.org/
121         ('blueridgepbs.org', 'Blue Ridge PBS (WBRA)'),  # http://www.blueridgepbs.org/
122         ('easttennesseepbs.org', 'East Tennessee PBS (WSJK)'),  # http://easttennesseepbs.org
123         ('wcte.org', 'WCTE-TV (WCTE)'),  # http://www.wcte.org
124         ('wljt.org', 'WLJT, Channel 11 (WLJT)'),  # http://wljt.org/
125         ('wosu.org', 'WOSU TV (WOSU)'),  # http://wosu.org/
126         ('woub.org', 'WOUB/WOUC (WOUB)'),  # http://woub.org/tv/index.php?section=5
127         ('wvpublic.org', 'WVPB (WVPB)'),  # http://wvpublic.org/
128         ('wkyupbs.org', 'WKYU-PBS (WKYU)'),  # http://www.wkyupbs.org
129         ('wyes.org', 'WYES-TV/New Orleans (WYES)'),  # http://www.wyes.org
130         ('kera.org', 'KERA 13 (KERA)'),  # http://www.kera.org/
131         ('mpbn.net', 'MPBN (WCBB)'),  # http://www.mpbn.net/
132         ('mountainlake.org', 'Mountain Lake PBS (WCFE)'),  # http://www.mountainlake.org/
133         ('nhptv.org', 'NHPTV (WENH)'),  # http://nhptv.org/
134         ('vpt.org', 'Vermont PBS (WETK)'),  # http://www.vpt.org
135         ('witf.org', 'witf (WITF)'),  # http://www.witf.org
136         ('wqed.org', 'WQED Multimedia (WQED)'),  # http://www.wqed.org/
137         ('wmht.org', 'WMHT Educational Telecommunications (WMHT)'),  # http://www.wmht.org/home/
138         ('deltabroadcasting.org', 'Q-TV (WDCQ)'),  # http://www.deltabroadcasting.org
139         ('dptv.org', 'WTVS Detroit Public TV (WTVS)'),  # http://www.dptv.org/
140         ('wcmu.org', 'CMU Public Television (WCMU)'),  # http://www.wcmu.org
141         ('wkar.org', 'WKAR-TV (WKAR)'),  # http://wkar.org/
142         ('nmu.edu', 'WNMU-TV Public TV 13 (WNMU)'),  # http://wnmutv.nmu.edu
143         ('wdse.org', 'WDSE - WRPT (WDSE)'),  # http://www.wdse.org/
144         ('wgte.org', 'WGTE TV (WGTE)'),  # http://www.wgte.org
145         ('lakelandptv.org', 'Lakeland Public Television (KAWE)'),  # http://www.lakelandptv.org
146         ('prairiepublic.org', 'PRAIRIE PUBLIC (KFME)'),  # http://www.prairiepublic.org/
147         ('kmos.org', 'KMOS-TV - Channels 6.1, 6.2 and 6.3 (KMOS)'),  # http://www.kmos.org/
148         ('montanapbs.org', 'MontanaPBS (KUSM)'),  # http://montanapbs.org
149         ('krwg.org', 'KRWG/Channel 22 (KRWG)'),  # http://www.krwg.org
150         ('panhandlepbs.org', 'KACV (KACV)'),  # http://www.panhandlepbs.org/home/
151         ('kcostv.org', 'KCOS/Channel 13 (KCOS)'),  # www.kcostv.org
152         ('wcny.org', 'WCNY/Channel 24 (WCNY)'),  # http://www.wcny.org
153         ('wned.org', 'WNED (WNED)'),  # http://www.wned.org/
154         ('wpbstv.org', 'WPBS (WPBS)'),  # http://www.wpbstv.org
155         ('wskg.org', 'WSKG Public TV (WSKG)'),  # http://wskg.org
156         ('wxxi.org', 'WXXI (WXXI)'),  # http://wxxi.org
157         ('wpsu.org', 'WPSU (WPSU)'),  # http://www.wpsu.org
158         ('wqln.org', 'WQLN/Channel 54 (WQLN)'),  # http://www.wqln.org
159         ('wvia.org', 'WVIA Public Media Studios (WVIA)'),  # http://www.wvia.org/
160         ('wtvi.org', 'WTVI (WTVI)'),  # http://www.wtvi.org/
161         ('whro.org', 'WHRO (WHRO)'),  # http://whro.org
162         ('WesternReservePublicMedia.org', 'Western Reserve PBS (WNEO)'),  # http://www.WesternReservePublicMedia.org/
163         ('wviz.org', 'WVIZ/PBS ideastream (WVIZ)'),  # http://www.wviz.org/
164         ('kcts9.org', 'KCTS 9 (KCTS)'),  # http://kcts9.org/
165         ('basinpbs.org', 'Basin PBS (KPBT)'),  # http://www.basinpbs.org
166         ('houstonpublicmedia.org', 'KUHT / Channel 8 (KUHT)'),  # http://www.houstonpublicmedia.org/
167         ('tamu.edu', 'KAMU - TV (KAMU)'),  # http://KAMU.tamu.edu
168         ('kedt.org', 'KEDT/Channel 16 (KEDT)'),  # http://www.kedt.org
169         ('klrn.org', 'KLRN (KLRN)'),  # http://www.klrn.org
170         ('klru.org', 'KLRU (KLRU)'),  # http://www.klru.org
171         ('kmbh.org', 'KMBH-TV (KMBH)'),  # http://www.kmbh.org
172         ('knct.org', 'KNCT (KNCT)'),  # http://www.knct.org
173         ('ktxt.org', 'KTTZ-TV (KTXT)'),  # http://www.ktxt.org
174         ('wtjx.org', 'WTJX Channel 12 (WTJX)'),  # http://www.wtjx.org/
175         ('ideastations.org', 'WCVE PBS (WCVE)'),  # http://ideastations.org/
176         ('kbtc.org', 'KBTC Public Television (KBTC)'),  # http://kbtc.org
177     )
178
179     IE_NAME = 'pbs'
180     IE_DESC = 'Public Broadcasting Service (PBS) and member stations: %s' % ', '.join(list(zip(*_STATIONS))[1])
181
182     _VALID_URL = r'''(?x)https?://
183         (?:
184            # Direct video URL
185            (?:videos?|watch)\.(?:%s)/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
186            # Article with embedded player (or direct video)
187            (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+?)(?:\.html)?/?(?:$|[?\#]) |
188            # Player
189            (?:video|player)\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
190         )
191     ''' % '|'.join(list(zip(*_STATIONS))[0])
192
193     _TESTS = [
194         {
195             'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
196             'md5': 'ce1888486f0908d555a8093cac9a7362',
197             'info_dict': {
198                 'id': '2365006249',
199                 'ext': 'mp4',
200                 'title': 'Constitution USA with Peter Sagal - A More Perfect Union',
201                 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
202                 'duration': 3190,
203             },
204             'params': {
205                 'skip_download': True,  # requires ffmpeg
206             },
207         },
208         {
209             'url': 'http://www.pbs.org/wgbh/pages/frontline/losing-iraq/',
210             'md5': '143c98aa54a346738a3d78f54c925321',
211             'info_dict': {
212                 'id': '2365297690',
213                 'ext': 'mp4',
214                 'title': 'FRONTLINE - Losing Iraq',
215                 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
216                 'duration': 5050,
217             },
218             'params': {
219                 'skip_download': True,  # requires ffmpeg
220             }
221         },
222         {
223             'url': 'http://www.pbs.org/newshour/bb/education-jan-june12-cyberschools_02-23/',
224             'md5': 'b19856d7f5351b17a5ab1dc6a64be633',
225             'info_dict': {
226                 'id': '2201174722',
227                 'ext': 'mp4',
228                 'title': 'PBS NewsHour - Cyber Schools Gain Popularity, but Quality Questions Persist',
229                 'description': 'md5:5871c15cba347c1b3d28ac47a73c7c28',
230                 'duration': 801,
231             },
232         },
233         {
234             'url': 'http://www.pbs.org/wnet/gperf/dudamel-conducts-verdi-requiem-hollywood-bowl-full-episode/3374/',
235             'md5': 'c62859342be2a0358d6c9eb306595978',
236             'info_dict': {
237                 'id': '2365297708',
238                 'ext': 'mp4',
239                 'description': 'md5:68d87ef760660eb564455eb30ca464fe',
240                 'title': 'Great Performances - Dudamel Conducts Verdi Requiem at the Hollywood Bowl - Full',
241                 'duration': 6559,
242                 'thumbnail': 're:^https?://.*\.jpg$',
243             },
244             'params': {
245                 'skip_download': True,  # requires ffmpeg
246             },
247         },
248         {
249             'url': 'http://www.pbs.org/wgbh/nova/earth/killer-typhoon.html',
250             'md5': '908f3e5473a693b266b84e25e1cf9703',
251             'info_dict': {
252                 'id': '2365160389',
253                 'display_id': 'killer-typhoon',
254                 'ext': 'mp4',
255                 'description': 'md5:c741d14e979fc53228c575894094f157',
256                 'title': 'NOVA - Killer Typhoon',
257                 'duration': 3172,
258                 'thumbnail': 're:^https?://.*\.jpg$',
259                 'upload_date': '20140122',
260                 'age_limit': 10,
261             },
262             'params': {
263                 'skip_download': True,  # requires ffmpeg
264             },
265         },
266         {
267             'url': 'http://www.pbs.org/wgbh/pages/frontline/united-states-of-secrets/',
268             'info_dict': {
269                 'id': 'united-states-of-secrets',
270             },
271             'playlist_count': 2,
272         },
273         {
274             'url': 'http://www.pbs.org/wgbh/americanexperience/films/death/player/',
275             'info_dict': {
276                 'id': '2276541483',
277                 'display_id': 'player',
278                 'ext': 'mp4',
279                 'title': 'American Experience - Death and the Civil War, Chapter 1',
280                 '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.',
281                 'duration': 682,
282                 'thumbnail': 're:^https?://.*\.jpg$',
283             },
284             'params': {
285                 'skip_download': True,  # requires ffmpeg
286             },
287         },
288         {
289             'url': 'http://video.pbs.org/video/2365367186/',
290             'info_dict': {
291                 'id': '2365367186',
292                 'display_id': '2365367186',
293                 'ext': 'mp4',
294                 'title': 'To Catch A Comet - Full Episode',
295                 'description': 'On November 12, 2014, billions of kilometers from Earth, spacecraft orbiter Rosetta and lander Philae did what no other had dared to attempt \u2014 land on the volatile surface of a comet as it zooms around the sun at 67,000 km/hr. The European Space Agency hopes this mission can help peer into our past and unlock secrets of our origins.',
296                 'duration': 3342,
297                 'thumbnail': 're:^https?://.*\.jpg$',
298             },
299             'params': {
300                 'skip_download': True,  # requires ffmpeg
301             },
302             'skip': 'Expired',
303         },
304         {
305             # Video embedded in iframe containing angle brackets as attribute's value (e.g.
306             # "<iframe style='position: absolute;<br />\ntop: 0; left: 0;' ...", see
307             # https://github.com/rg3/youtube-dl/issues/7059)
308             'url': 'http://www.pbs.org/food/features/a-chefs-life-season-3-episode-5-prickly-business/',
309             'info_dict': {
310                 'id': '2365546844',
311                 'display_id': 'a-chefs-life-season-3-episode-5-prickly-business',
312                 'ext': 'mp4',
313                 'title': "A Chef's Life - Season 3, Ep. 5: Prickly Business",
314                 'description': 'md5:61db2ddf27c9912f09c241014b118ed1',
315                 'duration': 1480,
316                 'thumbnail': 're:^https?://.*\.jpg$',
317             },
318             'params': {
319                 'skip_download': True,  # requires ffmpeg
320             },
321         },
322         {
323             # Frontline video embedded via flp2012.js
324             'url': 'http://www.pbs.org/wgbh/pages/frontline/the-atomic-artists',
325             'info_dict': {
326                 'id': '2070868960',
327                 'display_id': 'the-atomic-artists',
328                 'ext': 'mp4',
329                 'title': 'FRONTLINE - The Atomic Artists',
330                 'description': 'md5:f5bfbefadf421e8bb8647602011caf8e',
331                 'duration': 723,
332                 'thumbnail': 're:^https?://.*\.jpg$',
333             },
334             'params': {
335                 'skip_download': True,  # requires ffmpeg
336             },
337         },
338         {
339             'url': 'http://player.pbs.org/widget/partnerplayer/2365297708/?start=0&end=0&chapterbar=false&endscreen=false&topbar=true',
340             'only_matching': True,
341         },
342         {
343             'url': 'http://watch.knpb.org/video/2365616055/',
344             'only_matching': True,
345         }
346     ]
347     _ERRORS = {
348         101: 'We\'re sorry, but this video is not yet available.',
349         403: 'We\'re sorry, but this video is not available in your region due to right restrictions.',
350         404: 'We are experiencing technical difficulties that are preventing us from playing the video at this time. Please check back again soon.',
351         410: 'This video has expired and is no longer available for online streaming.',
352     }
353
354     def _extract_webpage(self, url):
355         mobj = re.match(self._VALID_URL, url)
356
357         presumptive_id = mobj.group('presumptive_id')
358         display_id = presumptive_id
359         if presumptive_id:
360             webpage = self._download_webpage(url, display_id)
361
362             upload_date = unified_strdate(self._search_regex(
363                 r'<input type="hidden" id="air_date_[0-9]+" value="([^"]+)"',
364                 webpage, 'upload date', default=None))
365
366             # tabbed frontline videos
367             tabbed_videos = re.findall(
368                 r'<div[^>]+class="videotab[^"]*"[^>]+vid="(\d+)"', webpage)
369             if tabbed_videos:
370                 return tabbed_videos, presumptive_id, upload_date
371
372             MEDIA_ID_REGEXES = [
373                 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",  # frontline video embed
374                 r'class="coveplayerid">([^<]+)<',                       # coveplayer
375                 r'<section[^>]+data-coveid="(\d+)"',                    # coveplayer from http://www.pbs.org/wgbh/frontline/film/real-csi/
376                 r'<input type="hidden" id="pbs_video_id_[0-9]+" value="([0-9]+)"/>',  # jwplayer
377             ]
378
379             media_id = self._search_regex(
380                 MEDIA_ID_REGEXES, webpage, 'media ID', fatal=False, default=None)
381             if media_id:
382                 return media_id, presumptive_id, upload_date
383
384             # Fronline video embedded via flp
385             video_id = self._search_regex(
386                 r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid', default=None)
387             if video_id:
388                 # pkg_id calculation is reverse engineered from
389                 # http://www.pbs.org/wgbh/pages/frontline/js/flp2012.js
390                 prg_id = self._search_regex(
391                     r'videoid\s*:\s*"([\d+a-z]{7,})"', webpage, 'videoid')[7:]
392                 if 'q' in prg_id:
393                     prg_id = prg_id.split('q')[1]
394                 prg_id = int(prg_id, 16)
395                 getdir = self._download_json(
396                     'http://www.pbs.org/wgbh/pages/frontline/.json/getdir/getdir%d.json' % prg_id,
397                     presumptive_id, 'Downloading getdir JSON',
398                     transform_source=strip_jsonp)
399                 return getdir['mid'], presumptive_id, upload_date
400
401             for iframe in re.findall(r'(?s)<iframe(.+?)></iframe>', webpage):
402                 url = self._search_regex(
403                     r'src=(["\'])(?P<url>.+?partnerplayer.+?)\1', iframe,
404                     'player URL', default=None, group='url')
405                 if url:
406                     break
407
408             mobj = re.match(self._VALID_URL, url)
409
410         player_id = mobj.group('player_id')
411         if not display_id:
412             display_id = player_id
413         if player_id:
414             player_page = self._download_webpage(
415                 url, display_id, note='Downloading player page',
416                 errnote='Could not download player page')
417             video_id = self._search_regex(
418                 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
419         else:
420             video_id = mobj.group('id')
421             display_id = video_id
422
423         return video_id, display_id, None
424
425     def _real_extract(self, url):
426         video_id, display_id, upload_date = self._extract_webpage(url)
427
428         if isinstance(video_id, list):
429             entries = [self.url_result(
430                 'http://video.pbs.org/video/%s' % vid_id, 'PBS', vid_id)
431                 for vid_id in video_id]
432             return self.playlist_result(entries, display_id)
433
434         info = self._download_json(
435             'http://player.pbs.org/videoInfo/%s?format=json&type=partner' % video_id,
436             display_id)
437
438         formats = []
439         for encoding_name in ('recommended_encoding', 'alternate_encoding'):
440             redirect = info.get(encoding_name)
441             if not redirect:
442                 continue
443             redirect_url = redirect.get('url')
444             if not redirect_url:
445                 continue
446
447             redirect_info = self._download_json(
448                 redirect_url + '?format=json', display_id,
449                 'Downloading %s video url info' % encoding_name)
450
451             if redirect_info['status'] == 'error':
452                 raise ExtractorError(
453                     '%s said: %s' % (
454                         self.IE_NAME,
455                         self._ERRORS.get(redirect_info['http_code'], redirect_info['message'])),
456                     expected=True)
457
458             format_url = redirect_info.get('url')
459             if not format_url:
460                 continue
461
462             if determine_ext(format_url) == 'm3u8':
463                 formats.extend(self._extract_m3u8_formats(
464                     format_url, display_id, 'mp4', preference=1, m3u8_id='hls'))
465             else:
466                 formats.append({
467                     'url': format_url,
468                     'format_id': redirect.get('eeid'),
469                 })
470         self._sort_formats(formats)
471
472         rating_str = info.get('rating')
473         if rating_str is not None:
474             rating_str = rating_str.rpartition('-')[2]
475         age_limit = US_RATINGS.get(rating_str)
476
477         subtitles = {}
478         closed_captions_url = info.get('closed_captions_url')
479         if closed_captions_url:
480             subtitles['en'] = [{
481                 'ext': 'ttml',
482                 'url': closed_captions_url,
483             }]
484
485         # info['title'] is often incomplete (e.g. 'Full Episode', 'Episode 5', etc)
486         # Try turning it to 'program - title' naming scheme if possible
487         alt_title = info.get('program', {}).get('title')
488         if alt_title:
489             info['title'] = alt_title + ' - ' + re.sub(r'^' + alt_title + '[\s\-:]+', '', info['title'])
490
491         return {
492             'id': video_id,
493             'display_id': display_id,
494             'title': info['title'],
495             'description': info['program'].get('description'),
496             'thumbnail': info.get('image_url'),
497             'duration': int_or_none(info.get('duration')),
498             'age_limit': age_limit,
499             'upload_date': upload_date,
500             'formats': formats,
501             'subtitles': subtitles,
502         }