[blinkx] Use centralized format sorting
[youtube-dl] / youtube_dl / extractor / blinkx.py
1 import datetime
2 import json
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     remove_start,
8 )
9
10
11 class BlinkxIE(InfoExtractor):
12     _VALID_URL = r'^(?:https?://(?:www\.)blinkx\.com/#?ce/|blinkx:)(?P<id>[^?]+)'
13     _IE_NAME = u'blinkx'
14
15     _TEST = {
16         u'url': u'http://www.blinkx.com/ce/8aQUy7GVFYgFzpKhT0oqsilwOGFRVXk3R1ZGWWdGenBLaFQwb3FzaWx3OGFRVXk3R1ZGWWdGenB',
17         u'file': u'8aQUy7GV.mp4',
18         u'md5': u'2e9a07364af40163a908edbf10bb2492',
19         u'info_dict': {
20             u"title": u"Police Car Rolls Away",
21             u"uploader": u"stupidvideos.com",
22             u"upload_date": u"20131215",
23             u"description": u"A police car gently rolls away from a fight. Maybe it felt weird being around a confrontation and just had to get out of there!",
24             u"duration": 14.886,
25             u"thumbnails": [{
26                 "width": 100,
27                 "height": 76,
28                 "url": "http://cdn.blinkx.com/stream/b/41/StupidVideos/20131215/1873969261/1873969261_tn_0.jpg",
29             }],
30         },
31     }
32
33     def _real_extract(self, url):
34         m = re.match(self._VALID_URL, url)
35         video_id = m.group('id')
36         display_id = video_id[:8]
37
38         api_url = (u'https://apib4.blinkx.com/api.php?action=play_video&' +
39                    u'video=%s' % video_id)
40         data_json = self._download_webpage(api_url, display_id)
41         data = json.loads(data_json)['api']['results'][0]
42         dt = datetime.datetime.fromtimestamp(data['pubdate_epoch'])
43         upload_date = dt.strftime('%Y%m%d')
44
45         duration = None
46         thumbnails = []
47         formats = []
48         for m in data['media']:
49             if m['type'] == 'jpg':
50                 thumbnails.append({
51                     'url': m['link'],
52                     'width': int(m['w']),
53                     'height': int(m['h']),
54                 })
55             elif m['type'] == 'original':
56                 duration = m['d']
57             elif m['type'] == 'youtube':
58                 yt_id = m['link']
59                 self.to_screen(u'Youtube video detected: %s' % yt_id)
60                 return self.url_result(yt_id, 'Youtube', video_id=yt_id)
61             elif m['type'] in ('flv', 'mp4'):
62                 vcodec = remove_start(m['vcodec'], 'ff')
63                 acodec = remove_start(m['acodec'], 'ff')
64                 tbr = (int(m['vbr']) + int(m['abr'])) // 1000
65                 format_id = (u'%s-%sk-%s' %
66                              (vcodec,
67                               tbr,
68                               m['w']))
69                 formats.append({
70                     'format_id': format_id,
71                     'url': m['link'],
72                     'vcodec': vcodec,
73                     'acodec': acodec,
74                     'abr': int(m['abr']) // 1000,
75                     'vbr': int(m['vbr']) // 1000,
76                     'tbr': tbr,
77                     'width': int(m['w']),
78                     'height': int(m['h']),
79                 })
80
81         self._sort_formats(formats)
82
83         return {
84             'id': display_id,
85             'fullid': video_id,
86             'title': data['title'],
87             'formats': formats,
88             'uploader': data['channel_name'],
89             'upload_date': upload_date,
90             'description': data.get('description'),
91             'thumbnails': thumbnails,
92             'duration': duration,
93         }