[ie/kick:clips] Support new URL format (#11107)
[yt-dlp3.git] / yt_dlp / extractor / kick.py
blobbd21e595012c89f02d2e79e62164b4362f807e95
1 import functools
3 from .common import InfoExtractor
4 from ..networking import HEADRequest
5 from ..utils import (
6 UserNotLive,
7 determine_ext,
8 float_or_none,
9 int_or_none,
10 merge_dicts,
11 parse_iso8601,
12 str_or_none,
13 traverse_obj,
14 unified_timestamp,
15 url_or_none,
19 class KickBaseIE(InfoExtractor):
20 def _real_initialize(self):
21 self._request_webpage(
22 HEADRequest('https://kick.com/'), None, 'Setting up session', fatal=False, impersonate=True)
23 xsrf_token = self._get_cookies('https://kick.com/').get('XSRF-TOKEN')
24 if not xsrf_token:
25 self.write_debug('kick.com did not set XSRF-TOKEN cookie')
26 KickBaseIE._API_HEADERS = {
27 'Authorization': f'Bearer {xsrf_token.value}',
28 'X-XSRF-TOKEN': xsrf_token.value,
29 } if xsrf_token else {}
31 def _call_api(self, path, display_id, note='Downloading API JSON', headers={}, **kwargs):
32 return self._download_json(
33 f'https://kick.com/api/{path}', display_id, note=note,
34 headers=merge_dicts(headers, self._API_HEADERS), impersonate=True, **kwargs)
37 class KickIE(KickBaseIE):
38 IE_NAME = 'kick:live'
39 _VALID_URL = r'https?://(?:www\.)?kick\.com/(?!(?:video|categories|search|auth)(?:[/?#]|$))(?P<id>[\w-]+)'
40 _TESTS = [{
41 'url': 'https://kick.com/buddha',
42 'info_dict': {
43 'id': '92722911-nopixel-40',
44 'ext': 'mp4',
45 'title': str,
46 'description': str,
47 'timestamp': int,
48 'thumbnail': r're:https?://.+\.jpg',
49 'categories': list,
50 'upload_date': str,
51 'channel': 'buddha',
52 'channel_id': '32807',
53 'uploader': 'Buddha',
54 'uploader_id': '33057',
55 'live_status': 'is_live',
56 'concurrent_view_count': int,
57 'release_timestamp': int,
58 'age_limit': 18,
59 'release_date': str,
61 'params': {'skip_download': 'livestream'},
62 # 'skip': 'livestream',
63 }, {
64 'url': 'https://kick.com/xqc',
65 'only_matching': True,
68 @classmethod
69 def suitable(cls, url):
70 return False if (KickVODIE.suitable(url) or KickClipIE.suitable(url)) else super().suitable(url)
72 def _real_extract(self, url):
73 channel = self._match_id(url)
74 response = self._call_api(f'v2/channels/{channel}', channel)
75 if not traverse_obj(response, 'livestream', expected_type=dict):
76 raise UserNotLive(video_id=channel)
78 return {
79 'channel': channel,
80 'is_live': True,
81 'formats': self._extract_m3u8_formats(response['playback_url'], channel, 'mp4', live=True),
82 **traverse_obj(response, {
83 'id': ('livestream', 'slug', {str}),
84 'title': ('livestream', 'session_title', {str}),
85 'description': ('user', 'bio', {str}),
86 'channel_id': (('id', ('livestream', 'channel_id')), {int}, {str_or_none}, any),
87 'uploader': (('name', ('user', 'username')), {str}, any),
88 'uploader_id': (('user_id', ('user', 'id')), {int}, {str_or_none}, any),
89 'timestamp': ('livestream', 'created_at', {unified_timestamp}),
90 'release_timestamp': ('livestream', 'start_time', {unified_timestamp}),
91 'thumbnail': ('livestream', 'thumbnail', 'url', {url_or_none}),
92 'categories': ('recent_categories', ..., 'name', {str}),
93 'concurrent_view_count': ('livestream', 'viewer_count', {int_or_none}),
94 'age_limit': ('livestream', 'is_mature', {bool}, {lambda x: 18 if x else 0}),
95 }),
99 class KickVODIE(KickBaseIE):
100 IE_NAME = 'kick:vod'
101 _VALID_URL = r'https?://(?:www\.)?kick\.com/[\w-]+/videos/(?P<id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})'
102 _TESTS = [{
103 'url': 'https://kick.com/xqc/videos/8dd97a8d-e17f-48fb-8bc3-565f88dbc9ea',
104 'md5': '3870f94153e40e7121a6e46c068b70cb',
105 'info_dict': {
106 'id': '8dd97a8d-e17f-48fb-8bc3-565f88dbc9ea',
107 'ext': 'mp4',
108 'title': '18+ #ad 🛑LIVE🛑CLICK🛑DRAMA🛑NEWS🛑STUFF🛑REACT🛑GET IN HHERE🛑BOP BOP🛑WEEEE WOOOO🛑',
109 'description': 'THE BEST AT ABSOLUTELY EVERYTHING. THE JUICER. LEADER OF THE JUICERS.',
110 'channel': 'xqc',
111 'channel_id': '668',
112 'uploader': 'xQc',
113 'uploader_id': '676',
114 'upload_date': '20240909',
115 'timestamp': 1725919141,
116 'duration': 10155.0,
117 'thumbnail': r're:^https?://.*\.jpg',
118 'view_count': int,
119 'categories': ['Just Chatting'],
120 'age_limit': 0,
122 'params': {'skip_download': 'm3u8'},
125 def _real_extract(self, url):
126 video_id = self._match_id(url)
127 response = self._call_api(f'v1/video/{video_id}', video_id)
129 return {
130 'id': video_id,
131 'formats': self._extract_m3u8_formats(response['source'], video_id, 'mp4'),
132 **traverse_obj(response, {
133 'title': ('livestream', ('session_title', 'slug'), {str}, any),
134 'description': ('livestream', 'channel', 'user', 'bio', {str}),
135 'channel': ('livestream', 'channel', 'slug', {str}),
136 'channel_id': ('livestream', 'channel', 'id', {int}, {str_or_none}),
137 'uploader': ('livestream', 'channel', 'user', 'username', {str}),
138 'uploader_id': ('livestream', 'channel', 'user_id', {int}, {str_or_none}),
139 'timestamp': ('created_at', {parse_iso8601}),
140 'duration': ('livestream', 'duration', {functools.partial(float_or_none, scale=1000)}),
141 'thumbnail': ('livestream', 'thumbnail', {url_or_none}),
142 'categories': ('livestream', 'categories', ..., 'name', {str}),
143 'view_count': ('views', {int_or_none}),
144 'age_limit': ('livestream', 'is_mature', {bool}, {lambda x: 18 if x else 0}),
149 class KickClipIE(KickBaseIE):
150 IE_NAME = 'kick:clips'
151 _VALID_URL = r'https?://(?:www\.)?kick\.com/[\w-]+(?:/clips/|/?\?(?:[^#]+&)?clip=)(?P<id>clip_[\w-]+)'
152 _TESTS = [{
153 'url': 'https://kick.com/mxddy?clip=clip_01GYXVB5Y8PWAPWCWMSBCFB05X',
154 'info_dict': {
155 'id': 'clip_01GYXVB5Y8PWAPWCWMSBCFB05X',
156 'ext': 'mp4',
157 'title': 'Maddy detains Abd D:',
158 'channel': 'mxddy',
159 'channel_id': '133789',
160 'uploader': 'AbdCreates',
161 'uploader_id': '3309077',
162 'thumbnail': r're:^https?://.*\.jpeg',
163 'duration': 35,
164 'timestamp': 1682481453,
165 'upload_date': '20230426',
166 'view_count': int,
167 'like_count': int,
168 'categories': ['VALORANT'],
169 'age_limit': 18,
171 'params': {'skip_download': 'm3u8'},
172 }, {
173 'url': 'https://kick.com/destiny?clip=clip_01H9SKET879NE7N9RJRRDS98J3',
174 'info_dict': {
175 'id': 'clip_01H9SKET879NE7N9RJRRDS98J3',
176 'title': 'W jews',
177 'ext': 'mp4',
178 'channel': 'destiny',
179 'channel_id': '1772249',
180 'uploader': 'punished_furry',
181 'uploader_id': '2027722',
182 'duration': 49.0,
183 'upload_date': '20230908',
184 'timestamp': 1694150180,
185 'thumbnail': 'https://clips.kick.com/clips/j3/clip_01H9SKET879NE7N9RJRRDS98J3/thumbnail.png',
186 'view_count': int,
187 'like_count': int,
188 'categories': ['Just Chatting'],
189 'age_limit': 0,
191 'params': {'skip_download': 'm3u8'},
192 }, {
193 'url': 'https://kick.com/spreen/clips/clip_01J8RGZRKHXHXXKJEHGRM932A5',
194 'info_dict': {
195 'id': 'clip_01J8RGZRKHXHXXKJEHGRM932A5',
196 'ext': 'mp4',
197 'title': 'KLJASLDJKLJKASDLJKDAS',
198 'channel': 'spreen',
199 'channel_id': '5312671',
200 'uploader': 'AnormalBarraBaja',
201 'uploader_id': '26518262',
202 'duration': 43.0,
203 'upload_date': '20240927',
204 'timestamp': 1727399987,
205 'thumbnail': 'https://clips.kick.com/clips/f2/clip_01J8RGZRKHXHXXKJEHGRM932A5/thumbnail.webp',
206 'view_count': int,
207 'like_count': int,
208 'categories': ['Minecraft'],
209 'age_limit': 0,
211 'params': {'skip_download': 'm3u8'},
214 def _real_extract(self, url):
215 clip_id = self._match_id(url)
216 clip = self._call_api(f'v2/clips/{clip_id}/play', clip_id)['clip']
217 clip_url = clip['clip_url']
219 if determine_ext(clip_url) == 'm3u8':
220 formats = self._extract_m3u8_formats(clip_url, clip_id, 'mp4')
221 else:
222 formats = [{'url': clip_url}]
224 return {
225 'id': clip_id,
226 'formats': formats,
227 **traverse_obj(clip, {
228 'title': ('title', {str}),
229 'channel': ('channel', 'slug', {str}),
230 'channel_id': ('channel', 'id', {int}, {str_or_none}),
231 'uploader': ('creator', 'username', {str}),
232 'uploader_id': ('creator', 'id', {int}, {str_or_none}),
233 'thumbnail': ('thumbnail_url', {url_or_none}),
234 'duration': ('duration', {float_or_none}),
235 'categories': ('category', 'name', {str}, all),
236 'timestamp': ('created_at', {parse_iso8601}),
237 'view_count': ('views', {int_or_none}),
238 'like_count': ('likes', {int_or_none}),
239 'age_limit': ('is_mature', {bool}, {lambda x: 18 if x else 0}),