Fix W504 and disable W503 (closes #20863)
[youtube-dl] / youtube_dl / extractor / blinkx.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..utils import (
7     remove_start,
8     int_or_none,
9 )
10
11
12 class BlinkxIE(InfoExtractor):
13     _VALID_URL = r'(?:https?://(?:www\.)blinkx\.com/#?ce/|blinkx:)(?P<id>[^?]+)'
14     IE_NAME = 'blinkx'
15
16     _TEST = {
17         'url': 'http://www.blinkx.com/ce/Da0Gw3xc5ucpNduzLuDDlv4WC9PuI4fDi1-t6Y3LyfdY2SZS5Urbvn-UPJvrvbo8LTKTc67Wu2rPKSQDJyZeeORCR8bYkhs8lI7eqddznH2ofh5WEEdjYXnoRtj7ByQwt7atMErmXIeYKPsSDuMAAqJDlQZ-3Ff4HJVeH_s3Gh8oQ',
18         'md5': '337cf7a344663ec79bf93a526a2e06c7',
19         'info_dict': {
20             'id': 'Da0Gw3xc',
21             'ext': 'mp4',
22             'title': 'No Daily Show for John Oliver; HBO Show Renewed - IGN News',
23             'uploader': 'IGN News',
24             'upload_date': '20150217',
25             'timestamp': 1424215740,
26             'description': 'HBO has renewed Last Week Tonight With John Oliver for two more seasons.',
27             'duration': 47.743333,
28         },
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         display_id = video_id[:8]
34
35         api_url = ('https://apib4.blinkx.com/api.php?action=play_video&'
36                    + 'video=%s' % video_id)
37         data_json = self._download_webpage(api_url, display_id)
38         data = json.loads(data_json)['api']['results'][0]
39         duration = None
40         thumbnails = []
41         formats = []
42         for m in data['media']:
43             if m['type'] == 'jpg':
44                 thumbnails.append({
45                     'url': m['link'],
46                     'width': int(m['w']),
47                     'height': int(m['h']),
48                 })
49             elif m['type'] == 'original':
50                 duration = float(m['d'])
51             elif m['type'] == 'youtube':
52                 yt_id = m['link']
53                 self.to_screen('Youtube video detected: %s' % yt_id)
54                 return self.url_result(yt_id, 'Youtube', video_id=yt_id)
55             elif m['type'] in ('flv', 'mp4'):
56                 vcodec = remove_start(m['vcodec'], 'ff')
57                 acodec = remove_start(m['acodec'], 'ff')
58                 vbr = int_or_none(m.get('vbr') or m.get('vbitrate'), 1000)
59                 abr = int_or_none(m.get('abr') or m.get('abitrate'), 1000)
60                 tbr = vbr + abr if vbr and abr else None
61                 format_id = '%s-%sk-%s' % (vcodec, tbr, m['w'])
62                 formats.append({
63                     'format_id': format_id,
64                     'url': m['link'],
65                     'vcodec': vcodec,
66                     'acodec': acodec,
67                     'abr': abr,
68                     'vbr': vbr,
69                     'tbr': tbr,
70                     'width': int_or_none(m.get('w')),
71                     'height': int_or_none(m.get('h')),
72                 })
73
74         self._sort_formats(formats)
75
76         return {
77             'id': display_id,
78             'fullid': video_id,
79             'title': data['title'],
80             'formats': formats,
81             'uploader': data['channel_name'],
82             'timestamp': data['pubdate_epoch'],
83             'description': data.get('description'),
84             'thumbnails': thumbnails,
85             'duration': duration,
86         }