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