]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/brightcove.py
Change the ie_name of YoutubeSearchDateIE
[yt-dlp.git] / youtube_dl / extractor / brightcove.py
CommitLineData
592882aa
JMF
1# encoding: utf-8
2
fbaaad49
JMF
3import re
4import json
cfe50f04 5import xml.etree.ElementTree
fbaaad49
JMF
6
7from .common import InfoExtractor
cfe50f04
JMF
8from ..utils import (
9 compat_urllib_parse,
45ff2d51 10 find_xpath_attr,
6543f0dc 11 compat_urlparse,
eeb165e6 12 compat_str,
dd5bcdc4 13 compat_urllib_request,
592882aa
JMF
14
15 ExtractorError,
cfe50f04 16)
fbaaad49 17
dd5bcdc4 18
fbaaad49 19class BrightcoveIE(InfoExtractor):
abb285fb 20 _VALID_URL = r'https?://.*brightcove\.com/(services|viewer).*\?(?P<query>.*)'
cfe50f04 21 _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
abb285fb 22 _PLAYLIST_URL_TEMPLATE = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s'
592882aa
JMF
23
24 _TESTS = [
25 {
4de1994b
JMF
26 # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
27 u'url': u'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
592882aa 28 u'file': u'2371591881001.mp4',
f52f01b5 29 u'md5': u'8eccab865181d29ec2958f32a6a754f5',
592882aa
JMF
30 u'note': u'Test Brightcove downloads and detection in GenericIE',
31 u'info_dict': {
32 u'title': u'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
33 u'uploader': u'8TV',
34 u'description': u'md5:a950cc4285c43e44d763d036710cd9cd',
35 }
36 },
37 {
4de1994b
JMF
38 # From http://medianetwork.oracle.com/video/player/1785452137001
39 u'url': u'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
592882aa
JMF
40 u'file': u'1785452137001.flv',
41 u'info_dict': {
42 u'title': u'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
43 u'description': u'John Rose speaks at the JVM Language Summit, August 1, 2012.',
44 u'uploader': u'Oracle',
45 },
46 },
fc4a0c2a
JMF
47 {
48 # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/
49 u'url': u'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001',
50 u'info_dict': {
51 u'id': u'2750934548001',
52 u'ext': u'mp4',
53 u'title': u'This Bracelet Acts as a Personal Thermostat',
54 u'description': u'md5:547b78c64f4112766ccf4e151c20b6a0',
55 u'uploader': u'Mashable',
56 },
57 },
592882aa 58 ]
cfe50f04
JMF
59
60 @classmethod
61 def _build_brighcove_url(cls, object_str):
62 """
63 Build a Brightcove url from a xml string containing
64 <object class="BrightcoveExperience">{params}</object>
65 """
46e28a84
PH
66
67 # Fix up some stupid HTML, see https://github.com/rg3/youtube-dl/issues/1553
68 object_str = re.sub(r'(<param name="[^"]+" value="[^"]+")>',
69 lambda m: m.group(1) + '/>', object_str)
2d0efe70
PH
70 # Fix up some stupid XML, see https://github.com/rg3/youtube-dl/issues/1608
71 object_str = object_str.replace(u'<--', u'<!--')
46e28a84 72
cfe50f04 73 object_doc = xml.etree.ElementTree.fromstring(object_str)
117adb0f 74 assert u'BrightcoveExperience' in object_doc.attrib['class']
cfe50f04 75 params = {'flashID': object_doc.attrib['id'],
5de3ece2 76 'playerID': find_xpath_attr(object_doc, './param', 'name', 'playerID').attrib['value'],
cfe50f04 77 }
36de0a0e 78 def find_param(name):
d214fdb8
JMF
79 node = find_xpath_attr(object_doc, './param', 'name', name)
80 if node is not None:
81 return node.attrib['value']
82 return None
36de0a0e 83 playerKey = find_param('playerKey')
cfe50f04
JMF
84 # Not all pages define this value
85 if playerKey is not None:
d214fdb8 86 params['playerKey'] = playerKey
36de0a0e
JMF
87 # The three fields hold the id of the video
88 videoPlayer = find_param('@videoPlayer') or find_param('videoId') or find_param('videoID')
abb285fb 89 if videoPlayer is not None:
d214fdb8 90 params['@videoPlayer'] = videoPlayer
36de0a0e 91 linkBase = find_param('linkBaseURL')
dd5bcdc4 92 if linkBase is not None:
d214fdb8 93 params['linkBaseURL'] = linkBase
cfe50f04
JMF
94 data = compat_urllib_parse.urlencode(params)
95 return cls._FEDERATED_URL_TEMPLATE % data
fbaaad49 96
eeb165e6
JMF
97 @classmethod
98 def _extract_brightcove_url(cls, webpage):
99 """Try to extract the brightcove url from the wepbage, returns None
100 if it can't be found
101 """
102 m_brightcove = re.search(
103 r'<object[^>]+?class=([\'"])[^>]*?BrightcoveExperience.*?\1.+?</object>',
104 webpage, re.DOTALL)
105 if m_brightcove is not None:
106 return cls._build_brighcove_url(m_brightcove.group())
107 else:
108 return None
109
fbaaad49 110 def _real_extract(self, url):
51040b72
JMF
111 # Change the 'videoId' and others field to '@videoPlayer'
112 url = re.sub(r'(?<=[?&])(videoI(d|D)|bctid)', '%40videoPlayer', url)
113 # Change bckey (used by bcove.me urls) to playerKey
114 url = re.sub(r'(?<=[?&])bckey', 'playerKey', url)
fbaaad49 115 mobj = re.match(self._VALID_URL, url)
6543f0dc
JMF
116 query_str = mobj.group('query')
117 query = compat_urlparse.parse_qs(query_str)
fbaaad49 118
6543f0dc
JMF
119 videoPlayer = query.get('@videoPlayer')
120 if videoPlayer:
dd5bcdc4 121 return self._get_video_info(videoPlayer[0], query_str, query)
abb285fb 122 else:
6543f0dc
JMF
123 player_key = query['playerKey']
124 return self._get_playlist_info(player_key[0])
abb285fb 125
dd5bcdc4
JMF
126 def _get_video_info(self, video_id, query_str, query):
127 request_url = self._FEDERATED_URL_TEMPLATE % query_str
128 req = compat_urllib_request.Request(request_url)
129 linkBase = query.get('linkBaseURL')
130 if linkBase is not None:
131 req.add_header('Referer', linkBase[0])
132 webpage = self._download_webpage(req, video_id)
fbaaad49
JMF
133
134 self.report_extraction(video_id)
135 info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
136 info = json.loads(info)['data']
137 video_info = info['programmedContent']['videoPlayer']['mediaDTO']
abb285fb
JMF
138
139 return self._extract_video_info(video_info)
140
141 def _get_playlist_info(self, player_key):
142 playlist_info = self._download_webpage(self._PLAYLIST_URL_TEMPLATE % player_key,
143 player_key, u'Downloading playlist information')
144
59145479
PH
145 json_data = json.loads(playlist_info)
146 if 'videoList' not in json_data:
147 raise ExtractorError(u'Empty playlist')
148 playlist_info = json_data['videoList']
abb285fb
JMF
149 videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
150
151 return self.playlist_result(videos, playlist_id=playlist_info['id'],
152 playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
153
154 def _extract_video_info(self, video_info):
592882aa 155 info = {
eeb165e6 156 'id': compat_str(video_info['id']),
592882aa
JMF
157 'title': video_info['displayName'],
158 'description': video_info.get('shortDescription'),
159 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
160 'uploader': video_info.get('publisherName'),
161 }
abb285fb 162
592882aa
JMF
163 renditions = video_info.get('renditions')
164 if renditions:
165 renditions = sorted(renditions, key=lambda r: r['size'])
b0759f0c
JMF
166 info['formats'] = [{
167 'url': rend['defaultURL'],
168 'height': rend.get('frameHeight'),
169 'width': rend.get('frameWidth'),
170 } for rend in renditions]
592882aa
JMF
171 elif video_info.get('FLVFullLengthURL') is not None:
172 info.update({
173 'url': video_info['FLVFullLengthURL'],
592882aa
JMF
174 })
175 else:
176 raise ExtractorError(u'Unable to extract video url for %s' % info['id'])
177 return info