1eebf5dfd48d31654f3612bbb058c0dd7aa9e030
[youtube-dl] / youtube_dl / extractor / azubu.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     float_or_none,
9     sanitized_Request,
10 )
11
12
13 class AzubuIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?azubu\.(?:tv|uol.com.br)/[^/]+#!/play/(?P<id>\d+)'
15     _TESTS = [
16         {
17             'url': 'http://www.azubu.tv/GSL#!/play/15575/2014-hot6-cup-last-big-match-ro8-day-1',
18             'md5': 'a88b42fcf844f29ad6035054bd9ecaf4',
19             'info_dict': {
20                 'id': '15575',
21                 'ext': 'mp4',
22                 'title': '2014 HOT6 CUP LAST BIG MATCH Ro8 Day 1',
23                 'description': 'md5:d06bdea27b8cc4388a90ad35b5c66c01',
24                 'thumbnail': 're:^https?://.*\.jpe?g',
25                 'timestamp': 1417523507.334,
26                 'upload_date': '20141202',
27                 'duration': 9988.7,
28                 'uploader': 'GSL',
29                 'uploader_id': 414310,
30                 'view_count': int,
31             },
32         },
33         {
34             'url': 'http://www.azubu.tv/FnaticTV#!/play/9344/-fnatic-at-worlds-2014:-toyz---%22i-love-rekkles,-he-has-amazing-mechanics%22-',
35             'md5': 'b72a871fe1d9f70bd7673769cdb3b925',
36             'info_dict': {
37                 'id': '9344',
38                 'ext': 'mp4',
39                 'title': 'Fnatic at Worlds 2014: Toyz - "I love Rekkles, he has amazing mechanics"',
40                 'description': 'md5:4a649737b5f6c8b5c5be543e88dc62af',
41                 'thumbnail': 're:^https?://.*\.jpe?g',
42                 'timestamp': 1410530893.320,
43                 'upload_date': '20140912',
44                 'duration': 172.385,
45                 'uploader': 'FnaticTV',
46                 'uploader_id': 272749,
47                 'view_count': int,
48             },
49             'skip': 'Channel offline',
50         },
51     ]
52
53     def _real_extract(self, url):
54         video_id = self._match_id(url)
55
56         data = self._download_json(
57             'http://www.azubu.tv/api/video/%s' % video_id, video_id)['data']
58
59         title = data['title'].strip()
60         description = data.get('description')
61         thumbnail = data.get('thumbnail')
62         view_count = data.get('view_count')
63         user = data.get('user', {})
64         uploader = user.get('username')
65         uploader_id = user.get('id')
66
67         stream_params = json.loads(data['stream_params'])
68
69         timestamp = float_or_none(stream_params.get('creationDate'), 1000)
70         duration = float_or_none(stream_params.get('length'), 1000)
71
72         renditions = stream_params.get('renditions') or []
73         video = stream_params.get('FLVFullLength') or stream_params.get('videoFullLength')
74         if video:
75             renditions.append(video)
76
77         if not renditions and not user.get('channel', {}).get('is_live', True):
78             raise ExtractorError('%s said: channel is offline.' % self.IE_NAME, expected=True)
79
80         formats = [{
81             'url': fmt['url'],
82             'width': fmt['frameWidth'],
83             'height': fmt['frameHeight'],
84             'vbr': float_or_none(fmt['encodingRate'], 1000),
85             'filesize': fmt['size'],
86             'vcodec': fmt['videoCodec'],
87             'container': fmt['videoContainer'],
88         } for fmt in renditions if fmt['url']]
89         self._sort_formats(formats)
90
91         return {
92             'id': video_id,
93             'title': title,
94             'description': description,
95             'thumbnail': thumbnail,
96             'timestamp': timestamp,
97             'duration': duration,
98             'uploader': uploader,
99             'uploader_id': uploader_id,
100             'view_count': view_count,
101             'formats': formats,
102         }
103
104
105 class AzubuLiveIE(InfoExtractor):
106     _VALID_URL = r'https?://(?:www\.)?azubu\.(?:tv|uol.com.br)/(?P<id>[^/]+)$'
107
108     _TESTS = [{
109         'url': 'http://www.azubu.tv/MarsTVMDLen',
110         'only_matching': True,
111     }, {
112         'url': 'http://azubu.uol.com.br/adolfz',
113         'only_matching': True,
114     }]
115
116     def _real_extract(self, url):
117         user = self._match_id(url)
118
119         info = self._download_json(
120             'http://api.azubu.tv/public/modules/last-video/{0}/info'.format(user),
121             user)['data']
122         if info['type'] != 'STREAM':
123             raise ExtractorError('{0} is not streaming live'.format(user), expected=True)
124
125         req = sanitized_Request(
126             'https://edge-elb.api.brightcove.com/playback/v1/accounts/3361910549001/videos/ref:' + info['reference_id'])
127         req.add_header('Accept', 'application/json;pk=BCpkADawqM1gvI0oGWg8dxQHlgT8HkdE2LnAlWAZkOlznO39bSZX726u4JqnDsK3MDXcO01JxXK2tZtJbgQChxgaFzEVdHRjaDoxaOu8hHOO8NYhwdxw9BzvgkvLUlpbDNUuDoc4E4wxDToV')
128         bc_info = self._download_json(req, user)
129         m3u8_url = next(source['src'] for source in bc_info['sources'] if source['container'] == 'M2TS')
130         formats = self._extract_m3u8_formats(m3u8_url, user, ext='mp4')
131         self._sort_formats(formats)
132
133         return {
134             'id': info['id'],
135             'title': self._live_title(info['title']),
136             'uploader_id': user,
137             'formats': formats,
138             'is_live': True,
139             'thumbnail': bc_info['poster'],
140         }