Merge remote-tracking branch 'jaimeMF/load-info'
[youtube-dl] / youtube_dl / extractor / theplatform.py
1 import re
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import (
6     xpath_with_ns,
7 )
8
9 _x = lambda p: xpath_with_ns(p, {'smil': 'http://www.w3.org/2005/SMIL21/Language'})
10
11
12 class ThePlatformIE(InfoExtractor):
13     _VALID_URL = r'(?:https?://link\.theplatform\.com/s/[^/]+/|theplatform:)(?P<id>[^/\?]+)'
14
15     _TEST = {
16         # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/
17         u'url': u'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true',
18         u'info_dict': {
19             u'id': u'e9I_cZgTgIPd',
20             u'ext': u'flv',
21             u'title': u'Blackberry\'s big, bold Z30',
22             u'description': u'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.',
23             u'duration': 247,
24         },
25         u'params': {
26             # rtmp download
27             u'skip_download': True,
28         },
29     }
30
31     def _get_info(self, video_id):
32         smil_url = ('http://link.theplatform.com/s/dJ5BDC/{0}/meta.smil?'
33             'format=smil&mbr=true'.format(video_id))
34         meta = self._download_xml(smil_url, video_id)
35         info_url = 'http://link.theplatform.com/s/dJ5BDC/{0}?format=preview'.format(video_id)
36         info_json = self._download_webpage(info_url, video_id)
37         info = json.loads(info_json)
38
39         head = meta.find(_x('smil:head'))
40         body = meta.find(_x('smil:body'))
41         base_url = head.find(_x('smil:meta')).attrib['base']
42         switch = body.find(_x('smil:switch'))
43         formats = []
44         for f in switch.findall(_x('smil:video')):
45             attr = f.attrib
46             formats.append({
47                 'url': base_url,
48                 'play_path': 'mp4:' + attr['src'],
49                 'ext': 'flv',
50                 'width': int(attr['width']),
51                 'height': int(attr['height']),
52                 'vbr': int(attr['system-bitrate']),
53             })
54         formats.sort(key=lambda f: (f['height'], f['width'], f['vbr']))
55
56         return {
57             'id': video_id,
58             'title': info['title'],
59             'formats': formats,
60             'description': info['description'],
61             'thumbnail': info['defaultThumbnailUrl'],
62             'duration': info['duration']//1000,
63         }
64         
65     def _real_extract(self, url):
66         mobj = re.match(self._VALID_URL, url)
67         video_id = mobj.group('id')
68         return self._get_info(video_id)