]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hitbox.py
[ie/box] Fix formats extraction (#8649)
[yt-dlp.git] / yt_dlp / extractor / hitbox.py
CommitLineData
e3947e2b 1import re
da3f7fb7 2
3from .common import InfoExtractor
14f25df2 4from ..compat import compat_str
da3f7fb7 5from ..utils import (
0c0a70f4 6 clean_html,
14f25df2 7 determine_ext,
0c0a70f4
S
8 float_or_none,
9 int_or_none,
14f25df2 10 parse_iso8601,
da3f7fb7 11)
12
13
14class HitboxIE(InfoExtractor):
0c0a70f4 15 IE_NAME = 'hitbox'
5f6fbcea
S
16 _VALID_URL = r'https?://(?:www\.)?(?:hitbox|smashcast)\.tv/(?:[^/]+/)*videos?/(?P<id>[0-9]+)'
17 _TESTS = [{
da3f7fb7 18 'url': 'http://www.hitbox.tv/video/203213',
19 'info_dict': {
20 'id': '203213',
21 'title': 'hitbox @ gamescom, Sub Button Hype extended, Giveaway - hitbox News Update with Oxy',
22 'alt_title': 'hitboxlive - Aug 9th #6',
0c0a70f4 23 'description': '',
da3f7fb7 24 'ext': 'mp4',
ec85ded8 25 'thumbnail': r're:^https?://.*\.jpg$',
0c0a70f4 26 'duration': 215.1666,
da3f7fb7 27 'resolution': 'HD 720p',
0c0a70f4 28 'uploader': 'hitboxlive',
da3f7fb7 29 'view_count': int,
0c0a70f4 30 'timestamp': 1407576133,
da3f7fb7 31 'upload_date': '20140809',
32 'categories': ['Live Show'],
33 },
34 'params': {
35 # m3u8 download
36 'skip_download': True,
37 },
5f6fbcea
S
38 }, {
39 'url': 'https://www.smashcast.tv/hitboxlive/videos/203213',
40 'only_matching': True,
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(
5f6fbcea 46 '%s/%s' % (url, video_id), video_id, 'Downloading metadata JSON')
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 57 description = clean_html(
3089bc74
S
58 video_meta.get('media_description')
59 or video_meta.get('media_description_md'))
0c0a70f4 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')]
5f6fbcea
S
65 thumbs = [{
66 'url': thumb_base + video_meta.get('media_thumbnail'),
67 'width': 320,
68 'height': 180
69 }, {
70 'url': thumb_base + video_meta.get('media_thumbnail_large'),
71 'width': 768,
72 'height': 432
73 }]
da3f7fb7 74
da3f7fb7 75 return {
76 'id': video_id,
77 'title': title,
78 'alt_title': alt_title,
79 'description': description,
da3f7fb7 80 'ext': 'mp4',
81 'thumbnails': thumbs,
82 'duration': duration,
0c0a70f4 83 'uploader': uploader,
da3f7fb7 84 'view_count': views,
0c0a70f4 85 'timestamp': timestamp,
da3f7fb7 86 'categories': categories,
da3f7fb7 87 }
e3947e2b 88
89 def _real_extract(self, url):
90 video_id = self._match_id(url)
91
e3947e2b 92 player_config = self._download_json(
5f6fbcea 93 'https://www.smashcast.tv/api/player/config/video/%s' % video_id,
33b066bd 94 video_id, 'Downloading video JSON')
e3947e2b 95
bc94bd51
S
96 formats = []
97 for video in player_config['clip']['bitrates']:
98 label = video.get('label')
99 if label == 'Auto':
100 continue
101 video_url = video.get('url')
102 if not video_url:
103 continue
104 bitrate = int_or_none(video.get('bitrate'))
105 if determine_ext(video_url) == 'm3u8':
106 if not video_url.startswith('http'):
107 continue
108 formats.append({
109 'url': video_url,
110 'ext': 'mp4',
111 'tbr': bitrate,
112 'format_note': label,
113 'protocol': 'm3u8_native',
114 })
115 else:
116 formats.append({
117 'url': video_url,
118 'tbr': bitrate,
119 'format_note': label,
120 })
121
008bee0f 122 metadata = self._extract_metadata(
5f6fbcea 123 'https://www.smashcast.tv/api/media/video', video_id)
bc94bd51 124 metadata['formats'] = formats
e3947e2b 125
126 return metadata
127
128
6368e2e6 129class HitboxLiveIE(HitboxIE): # XXX: Do not subclass from concrete IE
0c0a70f4 130 IE_NAME = 'hitbox:live'
5f6fbcea
S
131 _VALID_URL = r'https?://(?:www\.)?(?:hitbox|smashcast)\.tv/(?P<id>[^/?#&]+)'
132 _TESTS = [{
e3947e2b 133 'url': 'http://www.hitbox.tv/dimak',
134 'info_dict': {
135 'id': 'dimak',
136 'ext': 'mp4',
0c0a70f4
S
137 'description': 'md5:c9f80fa4410bc588d7faa40003fc7d0e',
138 'timestamp': int,
139 'upload_date': compat_str,
140 'title': compat_str,
141 'uploader': 'Dimak',
e3947e2b 142 },
143 'params': {
144 # live
145 'skip_download': True,
146 },
5f6fbcea
S
147 }, {
148 'url': 'https://www.smashcast.tv/dimak',
149 'only_matching': True,
150 }]
151
152 @classmethod
153 def suitable(cls, url):
154 return False if HitboxIE.suitable(url) else super(HitboxLiveIE, cls).suitable(url)
e3947e2b 155
156 def _real_extract(self, url):
157 video_id = self._match_id(url)
158
e3947e2b 159 player_config = self._download_json(
5f6fbcea 160 'https://www.smashcast.tv/api/player/config/live/%s' % video_id,
0c0a70f4 161 video_id)
e3947e2b 162
163 formats = []
164 cdns = player_config.get('cdns')
165 servers = []
166 for cdn in cdns:
1e10d02f
S
167 # Subscribe URLs are not playable
168 if cdn.get('rtmpSubscribe') is True:
169 continue
e3947e2b 170 base_url = cdn.get('netConnectionUrl')
ec85ded8 171 host = re.search(r'.+\.([^\.]+\.[^\./]+)/.+', base_url).group(1)
e3947e2b 172 if base_url not in servers:
173 servers.append(base_url)
174 for stream in cdn.get('bitrates'):
175 label = stream.get('label')
65939eff
S
176 if label == 'Auto':
177 continue
178 stream_url = stream.get('url')
179 if not stream_url:
180 continue
181 bitrate = int_or_none(stream.get('bitrate'))
182 if stream.get('provider') == 'hls' or determine_ext(stream_url) == 'm3u8':
183 if not stream_url.startswith('http'):
184 continue
e3947e2b 185 formats.append({
65939eff 186 'url': stream_url,
e3947e2b 187 'ext': 'mp4',
65939eff
S
188 'tbr': bitrate,
189 'format_note': label,
190 'rtmp_live': True,
191 })
192 else:
193 formats.append({
194 'url': '%s/%s' % (base_url, stream_url),
195 'ext': 'mp4',
196 'tbr': bitrate,
e3947e2b 197 'rtmp_live': True,
198 'format_note': host,
199 'page_url': url,
200 'player_url': 'http://www.hitbox.tv/static/player/flowplayer/flowplayer.commercial-3.2.16.swf',
201 })
14f41bc2
S
202
203 metadata = self._extract_metadata(
5f6fbcea 204 'https://www.smashcast.tv/api/media/live', video_id)
e3947e2b 205 metadata['formats'] = formats
206 metadata['is_live'] = True
39ca3b5c 207 metadata['title'] = metadata.get('title')
14f41bc2 208
e3947e2b 209 return metadata