Merge pull request #2461 from niebles/master
[youtube-dl] / youtube_dl / extractor / theplatform.py
1 import re
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     xpath_with_ns,
8 )
9
10 _x = lambda p: xpath_with_ns(p, {'smil': 'http://www.w3.org/2005/SMIL21/Language'})
11
12
13 class ThePlatformIE(InfoExtractor):
14     _VALID_URL = r'''(?x)
15         (?:https?://(?:link|player)\.theplatform\.com/[sp]/[^/]+/
16            (?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/)?
17          |theplatform:)(?P<id>[^/\?&]+)'''
18
19     _TEST = {
20         # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/
21         u'url': u'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true',
22         u'info_dict': {
23             u'id': u'e9I_cZgTgIPd',
24             u'ext': u'flv',
25             u'title': u'Blackberry\'s big, bold Z30',
26             u'description': u'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.',
27             u'duration': 247,
28         },
29         u'params': {
30             # rtmp download
31             u'skip_download': True,
32         },
33     }
34
35     def _get_info(self, video_id, smil_url):
36         meta = self._download_xml(smil_url, video_id)
37
38         try:
39             error_msg = next(
40                 n.attrib['abstract']
41                 for n in meta.findall(_x('.//smil:ref'))
42                 if n.attrib.get('title') == u'Geographic Restriction')
43         except StopIteration:
44             pass
45         else:
46             raise ExtractorError(error_msg, expected=True)
47
48         info_url = 'http://link.theplatform.com/s/dJ5BDC/{0}?format=preview'.format(video_id)
49         info_json = self._download_webpage(info_url, video_id)
50         info = json.loads(info_json)
51
52         head = meta.find(_x('smil:head'))
53         body = meta.find(_x('smil:body'))
54
55         f4m_node = body.find(_x('smil:seq/smil:video'))
56         if f4m_node is not None:
57             f4m_url = f4m_node.attrib['src']
58             if 'manifest.f4m?' not in f4m_url:
59                 f4m_url += '?'
60             # the parameters are from syfy.com, other sites may use others,
61             # they also work for nbc.com
62             f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
63             formats = [{
64                 'ext': 'flv',
65                 'url': f4m_url,
66             }]
67         else:
68             base_url = head.find(_x('smil:meta')).attrib['base']
69             switch = body.find(_x('smil:switch'))
70             formats = []
71             for f in switch.findall(_x('smil:video')):
72                 attr = f.attrib
73                 width = int(attr['width'])
74                 height = int(attr['height'])
75                 vbr = int(attr['system-bitrate']) // 1000
76                 format_id = '%dx%d_%dk' % (width, height, vbr)
77                 formats.append({
78                     'format_id': format_id,
79                     'url': base_url,
80                     'play_path': 'mp4:' + attr['src'],
81                     'ext': 'flv',
82                     'width': width,
83                     'height': height,
84                     'vbr': vbr,
85                 })
86             self._sort_formats(formats)
87
88         return {
89             'id': video_id,
90             'title': info['title'],
91             'formats': formats,
92             'description': info['description'],
93             'thumbnail': info['defaultThumbnailUrl'],
94             'duration': info['duration']//1000,
95         }
96         
97     def _real_extract(self, url):
98         mobj = re.match(self._VALID_URL, url)
99         video_id = mobj.group('id')
100         if mobj.group('config'):
101             config_url = url+ '&form=json'
102             config_url = config_url.replace('swf/', 'config/')
103             config_url = config_url.replace('onsite/', 'onsite/config/')
104             config_json = self._download_webpage(config_url, video_id, u'Downloading config')
105             config = json.loads(config_json)
106             smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m'
107         else:
108             smil_url = ('http://link.theplatform.com/s/dJ5BDC/{0}/meta.smil?'
109                 'format=smil&mbr=true'.format(video_id))
110         return self._get_info(video_id, smil_url)