aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/azubu.py
blob: efa624de1cbfddb741a7f8114059165d4e099095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import unicode_literals

import json

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    float_or_none,
    sanitized_Request,
)


class AzubuIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?azubu\.tv/[^/]+#!/play/(?P<id>\d+)'
    _TESTS = [
        {
            'url': 'http://www.azubu.tv/GSL#!/play/15575/2014-hot6-cup-last-big-match-ro8-day-1',
            'md5': 'a88b42fcf844f29ad6035054bd9ecaf4',
            'info_dict': {
                'id': '15575',
                'ext': 'mp4',
                'title': '2014 HOT6 CUP LAST BIG MATCH Ro8 Day 1',
                'description': 'md5:d06bdea27b8cc4388a90ad35b5c66c01',
                'thumbnail': 're:^https?://.*\.jpe?g',
                'timestamp': 1417523507.334,
                'upload_date': '20141202',
                'duration': 9988.7,
                'uploader': 'GSL',
                'uploader_id': 414310,
                'view_count': int,
            },
        },
        {
            'url': 'http://www.azubu.tv/FnaticTV#!/play/9344/-fnatic-at-worlds-2014:-toyz---%22i-love-rekkles,-he-has-amazing-mechanics%22-',
            'md5': 'b72a871fe1d9f70bd7673769cdb3b925',
            'info_dict': {
                'id': '9344',
                'ext': 'mp4',
                'title': 'Fnatic at Worlds 2014: Toyz - "I love Rekkles, he has amazing mechanics"',
                'description': 'md5:4a649737b5f6c8b5c5be543e88dc62af',
                'thumbnail': 're:^https?://.*\.jpe?g',
                'timestamp': 1410530893.320,
                'upload_date': '20140912',
                'duration': 172.385,
                'uploader': 'FnaticTV',
                'uploader_id': 272749,
                'view_count': int,
            },
        },
    ]

    def _real_extract(self, url):
        video_id = self._match_id(url)

        data = self._download_json(
            'http://www.azubu.tv/api/video/%s' % video_id, video_id)['data']

        title = data['title'].strip()
        description = data['description']
        thumbnail = data['thumbnail']
        view_count = data['view_count']
        uploader = data['user']['username']
        uploader_id = data['user']['id']

        stream_params = json.loads(data['stream_params'])

        timestamp = float_or_none(stream_params['creationDate'], 1000)
        duration = float_or_none(stream_params['length'], 1000)

        renditions = stream_params.get('renditions') or []
        video = stream_params.get('FLVFullLength') or stream_params.get('videoFullLength')
        if video:
            renditions.append(video)

        formats = [{
            'url': fmt['url'],
            'width': fmt['frameWidth'],
            'height': fmt['frameHeight'],
            'vbr': float_or_none(fmt['encodingRate'], 1000),
            'filesize': fmt['size'],
            'vcodec': fmt['videoCodec'],
            'container': fmt['videoContainer'],
        } for fmt in renditions if fmt['url']]
        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'description': description,
            'thumbnail': thumbnail,
            'timestamp': timestamp,
            'duration': duration,
            'uploader': uploader,
            'uploader_id': uploader_id,
            'view_count': view_count,
            'formats': formats,
        }


class AzubuLiveIE(InfoExtractor):
    _VALID_URL = r'https?://www.azubu.tv/(?P<id>[^/]+)$'

    _TEST = {
        'url': 'http://www.azubu.tv/MarsTVMDLen',
        'only_matching': True,
    }

    def _real_extract(self, url):
        user = self._match_id(url)

        info = self._download_json(
            'http://api.azubu.tv/public/modules/last-video/{0}/info'.format(user),
            user)['data']
        if info['type'] != 'STREAM':
            raise ExtractorError('{0} is not streaming live'.format(user), expected=True)

        req = sanitized_Request(
            'https://edge-elb.api.brightcove.com/playback/v1/accounts/3361910549001/videos/ref:' + info['reference_id'])
        req.add_header('Accept', 'application/json;pk=BCpkADawqM1gvI0oGWg8dxQHlgT8HkdE2LnAlWAZkOlznO39bSZX726u4JqnDsK3MDXcO01JxXK2tZtJbgQChxgaFzEVdHRjaDoxaOu8hHOO8NYhwdxw9BzvgkvLUlpbDNUuDoc4E4wxDToV')
        bc_info = self._download_json(req, user)
        m3u8_url = next(source['src'] for source in bc_info['sources'] if source['container'] == 'M2TS')
        formats = self._extract_m3u8_formats(m3u8_url, user, ext='mp4')
        self._sort_formats(formats)

        return {
            'id': info['id'],
            'title': self._live_title(info['title']),
            'uploader_id': user,
            'formats': formats,
            'is_live': True,
            'thumbnail': bc_info['poster'],
        }