]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/hitbox.py
[hitbox] Sort formats
[yt-dlp.git] / youtube_dl / extractor / hitbox.py
CommitLineData
da3f7fb7 1# coding: utf-8
2from __future__ import unicode_literals
0c0a70f4 3
e3947e2b 4import re
da3f7fb7 5
6from .common import InfoExtractor
7from ..utils import (
0c0a70f4
S
8 clean_html,
9 parse_iso8601,
10 float_or_none,
11 int_or_none,
12 compat_str,
65939eff 13 determine_ext,
da3f7fb7 14)
15
16
17class HitboxIE(InfoExtractor):
0c0a70f4 18 IE_NAME = 'hitbox'
da3f7fb7 19 _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/video/(?P<id>[0-9]+)'
e3947e2b 20 _TEST = {
da3f7fb7 21 'url': 'http://www.hitbox.tv/video/203213',
22 'info_dict': {
23 'id': '203213',
24 'title': 'hitbox @ gamescom, Sub Button Hype extended, Giveaway - hitbox News Update with Oxy',
25 'alt_title': 'hitboxlive - Aug 9th #6',
0c0a70f4 26 'description': '',
da3f7fb7 27 'ext': 'mp4',
28 'thumbnail': 're:^https?://.*\.jpg$',
0c0a70f4 29 'duration': 215.1666,
da3f7fb7 30 'resolution': 'HD 720p',
0c0a70f4 31 'uploader': 'hitboxlive',
da3f7fb7 32 'view_count': int,
0c0a70f4 33 'timestamp': 1407576133,
da3f7fb7 34 'upload_date': '20140809',
35 'categories': ['Live Show'],
36 },
37 'params': {
38 # m3u8 download
39 'skip_download': True,
40 },
e3947e2b 41 }
da3f7fb7 42
e3947e2b 43 def _extract_metadata(self, url, video_id):
da3f7fb7 44 thumb_base = 'https://edge.sf.hitbox.tv'
45 metadata = self._download_json(
0c0a70f4 46 '%s/%s' % (url, video_id), video_id)
da3f7fb7 47
e3947e2b 48 date = 'media_live_since'
49 media_type = 'livestream'
50 if metadata.get('media_type') == 'video':
51 media_type = 'video'
52 date = 'media_date_added'
53
54 video_meta = metadata.get(media_type, [])[0]
da3f7fb7 55 title = video_meta.get('media_status')
56 alt_title = video_meta.get('media_title')
0c0a70f4
S
57 description = clean_html(
58 video_meta.get('media_description') or
59 video_meta.get('media_description_md'))
60 duration = float_or_none(video_meta.get('media_duration'))
da3f7fb7 61 uploader = video_meta.get('media_user_name')
0c0a70f4
S
62 views = int_or_none(video_meta.get('media_views'))
63 timestamp = parse_iso8601(video_meta.get(date), ' ')
da3f7fb7 64 categories = [video_meta.get('category_name')]
65 thumbs = [
66 {'url': thumb_base + video_meta.get('media_thumbnail'),
67 'width': 320,
68 'height': 180},
69 {'url': thumb_base + video_meta.get('media_thumbnail_large'),
70 'width': 768,
71 'height': 432},
72 ]
73
da3f7fb7 74 return {
75 'id': video_id,
76 'title': title,
77 'alt_title': alt_title,
78 'description': description,
da3f7fb7 79 'ext': 'mp4',
80 'thumbnails': thumbs,
81 'duration': duration,
0c0a70f4 82 'uploader': uploader,
da3f7fb7 83 'view_count': views,
0c0a70f4 84 'timestamp': timestamp,
da3f7fb7 85 'categories': categories,
da3f7fb7 86 }
e3947e2b 87
88 def _real_extract(self, url):
89 video_id = self._match_id(url)
90
91 metadata = self._extract_metadata(
92 'https://www.hitbox.tv/api/media/video',
0c0a70f4 93 video_id)
e3947e2b 94
95 player_config = self._download_json(
0c0a70f4
S
96 'https://www.hitbox.tv/api/player/config/video/%s' % video_id,
97 video_id)
e3947e2b 98
bc94bd51
S
99 formats = []
100 for video in player_config['clip']['bitrates']:
101 label = video.get('label')
102 if label == 'Auto':
103 continue
104 video_url = video.get('url')
105 if not video_url:
106 continue
107 bitrate = int_or_none(video.get('bitrate'))
108 if determine_ext(video_url) == 'm3u8':
109 if not video_url.startswith('http'):
110 continue
111 formats.append({
112 'url': video_url,
113 'ext': 'mp4',
114 'tbr': bitrate,
115 'format_note': label,
116 'protocol': 'm3u8_native',
117 })
118 else:
119 formats.append({
120 'url': video_url,
121 'tbr': bitrate,
122 'format_note': label,
123 })
29492f33 124 self._sort_formats(formats)
bc94bd51
S
125
126 metadata['formats'] = formats
e3947e2b 127
128 return metadata
129
130
131class HitboxLiveIE(HitboxIE):
0c0a70f4 132 IE_NAME = 'hitbox:live'
e3947e2b 133 _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/(?!video)(?P<id>.+)'
134 _TEST = {
135 'url': 'http://www.hitbox.tv/dimak',
136 'info_dict': {
137 'id': 'dimak',
138 'ext': 'mp4',
0c0a70f4
S
139 'description': 'md5:c9f80fa4410bc588d7faa40003fc7d0e',
140 'timestamp': int,
141 'upload_date': compat_str,
142 'title': compat_str,
143 'uploader': 'Dimak',
e3947e2b 144 },
145 'params': {
146 # live
147 'skip_download': True,
148 },
149 }
150
151 def _real_extract(self, url):
152 video_id = self._match_id(url)
153
154 metadata = self._extract_metadata(
155 'https://www.hitbox.tv/api/media/live',
0c0a70f4 156 video_id)
e3947e2b 157
158 player_config = self._download_json(
0c0a70f4
S
159 'https://www.hitbox.tv/api/player/config/live/%s' % video_id,
160 video_id)
e3947e2b 161
162 formats = []
163 cdns = player_config.get('cdns')
164 servers = []
165 for cdn in cdns:
166 base_url = cdn.get('netConnectionUrl')
167 host = re.search('.+\.([^\.]+\.[^\./]+)/.+', base_url).group(1)
168 if base_url not in servers:
169 servers.append(base_url)
170 for stream in cdn.get('bitrates'):
171 label = stream.get('label')
65939eff
S
172 if label == 'Auto':
173 continue
174 stream_url = stream.get('url')
175 if not stream_url:
176 continue
177 bitrate = int_or_none(stream.get('bitrate'))
178 if stream.get('provider') == 'hls' or determine_ext(stream_url) == 'm3u8':
179 if not stream_url.startswith('http'):
180 continue
e3947e2b 181 formats.append({
65939eff 182 'url': stream_url,
e3947e2b 183 'ext': 'mp4',
65939eff
S
184 'tbr': bitrate,
185 'format_note': label,
186 'rtmp_live': True,
187 })
188 else:
189 formats.append({
190 'url': '%s/%s' % (base_url, stream_url),
191 'ext': 'mp4',
192 'tbr': bitrate,
e3947e2b 193 'rtmp_live': True,
194 'format_note': host,
195 'page_url': url,
196 'player_url': 'http://www.hitbox.tv/static/player/flowplayer/flowplayer.commercial-3.2.16.swf',
197 })
198
199 self._sort_formats(formats)
200 metadata['formats'] = formats
201 metadata['is_live'] = True
202 metadata['title'] = self._live_title(metadata.get('title'))
203 return metadata