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