aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_dl/extractor/hitbox.py
blob: eab2749ecdce8c83984ba1ab2540391141dcb255 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# coding: utf-8
from __future__ import unicode_literals
import re

from .common import InfoExtractor
from ..utils import (
    unified_strdate,
)


class HitboxIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/video/(?P<id>[0-9]+)'
    _TEST = {
        'url': 'http://www.hitbox.tv/video/203213',
        'info_dict': {
            'id': '203213',
            'title': 'hitbox @ gamescom, Sub Button Hype extended, Giveaway - hitbox News Update with Oxy',
            'alt_title': 'hitboxlive - Aug 9th #6',
            'description': '\n',
            'ext': 'mp4',
            'thumbnail': 're:^https?://.*\.jpg$',
            'duration': 215,
            'resolution': 'HD 720p',
            'uploader_id': 'hitboxlive',
            'view_count': int,
            'upload_date': '20140809',
            'categories': ['Live Show'],
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
    }

    def _extract_metadata(self, url, video_id):
        thumb_base = 'https://edge.sf.hitbox.tv'
        metadata = self._download_json(
            '%s/%s' % (url, video_id), video_id
        )

        date = 'media_live_since'
        media_type = 'livestream'
        if metadata.get('media_type') == 'video':
            media_type = 'video'
            date = 'media_date_added'

        video_meta = metadata.get(media_type, [])[0]
        title = video_meta.get('media_status')
        alt_title = video_meta.get('media_title')
        description = video_meta.get('media_description_md')
        duration = int(float(video_meta.get('media_duration')))
        uploader = video_meta.get('media_user_name')
        views = int(video_meta.get('media_views'))
        upload_date = unified_strdate(video_meta.get(date))
        categories = [video_meta.get('category_name')]
        thumbs = [
            {'url': thumb_base + video_meta.get('media_thumbnail'),
             'width': 320,
             'height': 180},
            {'url': thumb_base + video_meta.get('media_thumbnail_large'),
             'width': 768,
             'height': 432},
        ]

        return {
            'id': video_id,
            'title': title,
            'alt_title': alt_title,
            'description': description,
            'ext': 'mp4',
            'thumbnails': thumbs,
            'duration': duration,
            'uploader_id': uploader,
            'view_count': views,
            'upload_date': upload_date,
            'categories': categories,
        }

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

        metadata = self._extract_metadata(
            'https://www.hitbox.tv/api/media/video',
            video_id
        )

        player_config = self._download_json(
            'https://www.hitbox.tv/api/player/config/video/%s' % (video_id),
            video_id
        )

        clip = player_config.get('clip')
        video_url = clip.get('url')
        res = clip.get('bitrates', [])[0].get('label')

        metadata['resolution'] = res
        metadata['url'] = video_url
        metadata['protocol'] = 'm3u8'

        return metadata


class HitboxLiveIE(HitboxIE):
    _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/(?!video)(?P<id>.+)'
    _TEST = {
        'url': 'http://www.hitbox.tv/dimak',
        'info_dict': {
            'id': 'dimak',
            'ext': 'mp4',
            'description': str,
            'upload_date': str,
            'title': str,
            'uploader_id': 'Dimak',
        },
        'params': {
            # live
            'skip_download': True,
        },
    }

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

        metadata = self._extract_metadata(
            'https://www.hitbox.tv/api/media/live',
            video_id
        )

        player_config = self._download_json(
            'https://www.hitbox.tv/api/player/config/live/%s' % (video_id),
            video_id
        )

        formats = []
        cdns = player_config.get('cdns')
        servers = []
        for cdn in cdns:
            base_url = cdn.get('netConnectionUrl')
            host = re.search('.+\.([^\.]+\.[^\./]+)/.+', base_url).group(1)
            if base_url not in servers:
                servers.append(base_url)
                for stream in cdn.get('bitrates'):
                    label = stream.get('label')
                    if label != 'Auto':
                        formats.append({
                            'url': '%s/%s' % (base_url, stream.get('url')),
                            'ext': 'mp4',
                            'vbr': stream.get('bitrate'),
                            'resolution': label,
                            'rtmp_live': True,
                            'format_note': host,
                            'page_url': url,
                            'player_url': 'http://www.hitbox.tv/static/player/flowplayer/flowplayer.commercial-3.2.16.swf',
                        })

        self._sort_formats(formats)
        metadata['formats'] = formats
        metadata['is_live'] = True
        metadata['title'] = self._live_title(metadata.get('title'))
        return metadata