]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/blinkx.py
Imported Upstream version 2013.12.23
[youtubedl] / 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 format_id = (u'%s-%sk-%s' %
65 (vcodec,
66 (int(m['vbr']) + int(m['abr'])) // 1000,
67 m['w']))
68 formats.append({
69 'format_id': format_id,
70 'url': m['link'],
71 'vcodec': vcodec,
72 'acodec': acodec,
73 'abr': int(m['abr']) // 1000,
74 'vbr': int(m['vbr']) // 1000,
75 'width': int(m['w']),
76 'height': int(m['h']),
77 })
78 formats.sort(key=lambda f: (f['width'], f['vbr'], f['abr']))
79
80 return {
81 'id': display_id,
82 'fullid': video_id,
83 'title': data['title'],
84 'formats': formats,
85 'uploader': data['channel_name'],
86 'upload_date': upload_date,
87 'description': data.get('description'),
88 'thumbnails': thumbnails,
89 'duration': duration,
90 }