[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / kick.py
blobd124372424a728839d5dde55c48030c498137cd6
1 from .common import InfoExtractor
2 from ..networking import HEADRequest
3 from ..utils import (
4 UserNotLive,
5 float_or_none,
6 merge_dicts,
7 str_or_none,
8 traverse_obj,
9 unified_timestamp,
10 url_or_none,
14 class KickBaseIE(InfoExtractor):
15 def _real_initialize(self):
16 self._request_webpage(HEADRequest('https://kick.com/'), None, 'Setting up session', fatal=False)
17 xsrf_token = self._get_cookies('https://kick.com/').get('XSRF-TOKEN')
18 if not xsrf_token:
19 self.write_debug('kick.com did not set XSRF-TOKEN cookie')
20 KickBaseIE._API_HEADERS = {
21 'Authorization': f'Bearer {xsrf_token.value}',
22 'X-XSRF-TOKEN': xsrf_token.value,
23 } if xsrf_token else {}
25 def _call_api(self, path, display_id, note='Downloading API JSON', headers={}, **kwargs):
26 return self._download_json(
27 f'https://kick.com/api/v1/{path}', display_id, note=note,
28 headers=merge_dicts(headers, self._API_HEADERS), **kwargs)
31 class KickIE(KickBaseIE):
32 _VALID_URL = r'https?://(?:www\.)?kick\.com/(?!(?:video|categories|search|auth)(?:[/?#]|$))(?P<id>[\w-]+)'
33 _TESTS = [{
34 'url': 'https://kick.com/yuppy',
35 'info_dict': {
36 'id': '6cde1-kickrp-joe-flemmingskick-info-heremust-knowmust-see21',
37 'ext': 'mp4',
38 'title': str,
39 'description': str,
40 'channel': 'yuppy',
41 'channel_id': '33538',
42 'uploader': 'Yuppy',
43 'uploader_id': '33793',
44 'upload_date': str,
45 'live_status': 'is_live',
46 'timestamp': int,
47 'thumbnail': r're:^https?://.*\.jpg',
48 'categories': list,
50 'skip': 'livestream',
51 }, {
52 'url': 'https://kick.com/kmack710',
53 'only_matching': True,
56 def _real_extract(self, url):
57 channel = self._match_id(url)
58 response = self._call_api(f'channels/{channel}', channel)
59 if not traverse_obj(response, 'livestream', expected_type=dict):
60 raise UserNotLive(video_id=channel)
62 return {
63 'id': str(traverse_obj(
64 response, ('livestream', ('slug', 'id')), get_all=False, default=channel)),
65 'formats': self._extract_m3u8_formats(
66 response['playback_url'], channel, 'mp4', live=True),
67 'title': traverse_obj(
68 response, ('livestream', ('session_title', 'slug')), get_all=False, default=''),
69 'description': traverse_obj(response, ('user', 'bio')),
70 'channel': channel,
71 'channel_id': str_or_none(traverse_obj(response, 'id', ('livestream', 'channel_id'))),
72 'uploader': traverse_obj(response, 'name', ('user', 'username')),
73 'uploader_id': str_or_none(traverse_obj(response, 'user_id', ('user', 'id'))),
74 'is_live': True,
75 'timestamp': unified_timestamp(traverse_obj(response, ('livestream', 'created_at'))),
76 'thumbnail': traverse_obj(
77 response, ('livestream', 'thumbnail', 'url'), expected_type=url_or_none),
78 'categories': traverse_obj(response, ('recent_categories', ..., 'name')),
82 class KickVODIE(KickBaseIE):
83 _VALID_URL = r'https?://(?:www\.)?kick\.com/video/(?P<id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})'
84 _TESTS = [{
85 'url': 'https://kick.com/video/54244b5e-050a-4df4-a013-b2433dafbe35',
86 'md5': '73691206a6a49db25c5aa1588e6538fc',
87 'info_dict': {
88 'id': '54244b5e-050a-4df4-a013-b2433dafbe35',
89 'ext': 'mp4',
90 'title': 'Making 710-carBoosting. Kinda No Pixel inspired. !guilded - !links',
91 'description': 'md5:a0d3546bf7955d0a8252ffe0fd6f518f',
92 'channel': 'kmack710',
93 'channel_id': '16278',
94 'uploader': 'Kmack710',
95 'uploader_id': '16412',
96 'upload_date': '20221206',
97 'timestamp': 1670318289,
98 'duration': 40104.0,
99 'thumbnail': r're:^https?://.*\.jpg',
100 'categories': ['Grand Theft Auto V'],
102 'params': {
103 'skip_download': 'm3u8',
107 def _real_extract(self, url):
108 video_id = self._match_id(url)
109 response = self._call_api(f'video/{video_id}', video_id)
111 return {
112 'id': video_id,
113 'formats': self._extract_m3u8_formats(response['source'], video_id, 'mp4'),
114 'title': traverse_obj(
115 response, ('livestream', ('session_title', 'slug')), get_all=False, default=''),
116 'description': traverse_obj(response, ('livestream', 'channel', 'user', 'bio')),
117 'channel': traverse_obj(response, ('livestream', 'channel', 'slug')),
118 'channel_id': str_or_none(traverse_obj(response, ('livestream', 'channel', 'id'))),
119 'uploader': traverse_obj(response, ('livestream', 'channel', 'user', 'username')),
120 'uploader_id': str_or_none(traverse_obj(response, ('livestream', 'channel', 'user_id'))),
121 'timestamp': unified_timestamp(response.get('created_at')),
122 'duration': float_or_none(traverse_obj(response, ('livestream', 'duration')), scale=1000),
123 'thumbnail': traverse_obj(
124 response, ('livestream', 'thumbnail'), expected_type=url_or_none),
125 'categories': traverse_obj(response, ('livestream', 'categories', ..., 'name')),