]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/brightcove.py
71e3c7883338154eea0c3d369a0fdd0bee828e26
[youtubedl] / youtube_dl / extractor / brightcove.py
1 import re
2 import json
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7 compat_urllib_parse,
8 find_xpath_attr,
9 compat_urlparse,
10 )
11
12 class BrightcoveIE(InfoExtractor):
13 _VALID_URL = r'https?://.*brightcove\.com/(services|viewer).*\?(?P<query>.*)'
14 _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
15 _PLAYLIST_URL_TEMPLATE = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s'
16
17 # There is a test for Brigtcove in GenericIE, that way we test both the download
18 # and the detection of videos, and we don't have to find an URL that is always valid
19
20 @classmethod
21 def _build_brighcove_url(cls, object_str):
22 """
23 Build a Brightcove url from a xml string containing
24 <object class="BrightcoveExperience">{params}</object>
25 """
26 object_doc = xml.etree.ElementTree.fromstring(object_str)
27 assert u'BrightcoveExperience' in object_doc.attrib['class']
28 params = {'flashID': object_doc.attrib['id'],
29 'playerID': find_xpath_attr(object_doc, './param', 'name', 'playerID').attrib['value'],
30 }
31 playerKey = find_xpath_attr(object_doc, './param', 'name', 'playerKey')
32 # Not all pages define this value
33 if playerKey is not None:
34 params['playerKey'] = playerKey.attrib['value']
35 videoPlayer = find_xpath_attr(object_doc, './param', 'name', '@videoPlayer')
36 if videoPlayer is not None:
37 params['@videoPlayer'] = videoPlayer.attrib['value']
38 data = compat_urllib_parse.urlencode(params)
39 return cls._FEDERATED_URL_TEMPLATE % data
40
41 def _real_extract(self, url):
42 mobj = re.match(self._VALID_URL, url)
43 query_str = mobj.group('query')
44 query = compat_urlparse.parse_qs(query_str)
45
46 videoPlayer = query.get('@videoPlayer')
47 if videoPlayer:
48 return self._get_video_info(videoPlayer[0], query_str)
49 else:
50 player_key = query['playerKey']
51 return self._get_playlist_info(player_key[0])
52
53 def _get_video_info(self, video_id, query):
54 request_url = self._FEDERATED_URL_TEMPLATE % query
55 webpage = self._download_webpage(request_url, video_id)
56
57 self.report_extraction(video_id)
58 info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
59 info = json.loads(info)['data']
60 video_info = info['programmedContent']['videoPlayer']['mediaDTO']
61
62 return self._extract_video_info(video_info)
63
64 def _get_playlist_info(self, player_key):
65 playlist_info = self._download_webpage(self._PLAYLIST_URL_TEMPLATE % player_key,
66 player_key, u'Downloading playlist information')
67
68 playlist_info = json.loads(playlist_info)['videoList']
69 videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
70
71 return self.playlist_result(videos, playlist_id=playlist_info['id'],
72 playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
73
74 def _extract_video_info(self, video_info):
75 renditions = video_info['renditions']
76 renditions = sorted(renditions, key=lambda r: r['size'])
77 best_format = renditions[-1]
78
79 return {'id': video_info['id'],
80 'title': video_info['displayName'],
81 'url': best_format['defaultURL'],
82 'ext': 'mp4',
83 'description': video_info.get('shortDescription'),
84 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
85 'uploader': video_info.get('publisherName'),
86 }