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

Generated by cgit