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