]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pbs.py
Imported Upstream version 2014.08.05
[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 US_RATINGS,
8 )
9
10
11 class PBSIE(InfoExtractor):
12 _VALID_URL = r'''(?x)https?://
13 (?:
14 # Direct video URL
15 video\.pbs\.org/(?:viralplayer|video)/(?P<id>[0-9]+)/? |
16 # Article with embedded player
17 (?:www\.)?pbs\.org/(?:[^/]+/){2,5}(?P<presumptive_id>[^/]+)/?(?:$|[?\#]) |
18 # Player
19 video\.pbs\.org/(?:widget/)?partnerplayer/(?P<player_id>[^/]+)/
20 )
21 '''
22
23 _TEST = {
24 'url': 'http://www.pbs.org/tpt/constitution-usa-peter-sagal/watch/a-more-perfect-union/',
25 'md5': 'ce1888486f0908d555a8093cac9a7362',
26 'info_dict': {
27 'id': '2365006249',
28 'ext': 'mp4',
29 'title': 'A More Perfect Union',
30 'description': 'md5:ba0c207295339c8d6eced00b7c363c6a',
31 'duration': 3190,
32 },
33 }
34
35 def _extract_ids(self, url):
36 mobj = re.match(self._VALID_URL, url)
37
38 presumptive_id = mobj.group('presumptive_id')
39 display_id = presumptive_id
40 if presumptive_id:
41 webpage = self._download_webpage(url, display_id)
42
43 # frontline video embed
44 media_id = self._search_regex(
45 r"div\s*:\s*'videoembed'\s*,\s*mediaid\s*:\s*'(\d+)'",
46 webpage, 'frontline video ID', fatal=False, default=None)
47 if media_id:
48 return media_id, presumptive_id
49
50 url = self._search_regex(
51 r'<iframe\s+id=["\']partnerPlayer["\'].*?\s+src=["\'](.*?)["\']>',
52 webpage, 'player URL')
53 mobj = re.match(self._VALID_URL, url)
54
55 player_id = mobj.group('player_id')
56 if not display_id:
57 display_id = player_id
58 if player_id:
59 player_page = self._download_webpage(
60 url, display_id, note='Downloading player page',
61 errnote='Could not download player page')
62 video_id = self._search_regex(
63 r'<div\s+id="video_([0-9]+)"', player_page, 'video ID')
64 else:
65 video_id = mobj.group('id')
66 display_id = video_id
67
68 return video_id, display_id
69
70 def _real_extract(self, url):
71 video_id, display_id = self._extract_ids(url)
72
73 info_url = 'http://video.pbs.org/videoInfo/%s?format=json' % video_id
74 info = self._download_json(info_url, display_id)
75
76 rating_str = info.get('rating')
77 if rating_str is not None:
78 rating_str = rating_str.rpartition('-')[2]
79 age_limit = US_RATINGS.get(rating_str)
80
81 return {
82 'id': video_id,
83 'title': info['title'],
84 'url': info['alternate_encoding']['url'],
85 'ext': 'mp4',
86 'description': info['program'].get('description'),
87 'thumbnail': info.get('image_url'),
88 'duration': info.get('duration'),
89 'age_limit': age_limit,
90 }