kick.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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,
  11. )
  12. class KickBaseIE(InfoExtractor):
  13. def _real_initialize(self):
  14. self._request_webpage(
  15. HEADRequest('https://kick.com/'), None, 'Setting up session', fatal=False, impersonate=True)
  16. xsrf_token = self._get_cookies('https://kick.com/').get('XSRF-TOKEN')
  17. if not xsrf_token:
  18. self.write_debug('kick.com did not set XSRF-TOKEN cookie')
  19. KickBaseIE._API_HEADERS = {
  20. 'Authorization': f'Bearer {xsrf_token.value}',
  21. 'X-XSRF-TOKEN': xsrf_token.value,
  22. } if xsrf_token else {}
  23. def _call_api(self, path, display_id, note='Downloading API JSON', headers={}, **kwargs):
  24. return self._download_json(
  25. f'https://kick.com/api/v1/{path}', display_id, note=note,
  26. headers=merge_dicts(headers, self._API_HEADERS), impersonate=True, **kwargs)
  27. class KickIE(KickBaseIE):
  28. _VALID_URL = r'https?://(?:www\.)?kick\.com/(?!(?:video|categories|search|auth)(?:[/?#]|$))(?P<id>[\w-]+)'
  29. _TESTS = [{
  30. 'url': 'https://kick.com/yuppy',
  31. 'info_dict': {
  32. 'id': '6cde1-kickrp-joe-flemmingskick-info-heremust-knowmust-see21',
  33. 'ext': 'mp4',
  34. 'title': str,
  35. 'description': str,
  36. 'channel': 'yuppy',
  37. 'channel_id': '33538',
  38. 'uploader': 'Yuppy',
  39. 'uploader_id': '33793',
  40. 'upload_date': str,
  41. 'live_status': 'is_live',
  42. 'timestamp': int,
  43. 'thumbnail': r're:^https?://.*\.jpg',
  44. 'categories': list,
  45. },
  46. 'skip': 'livestream',
  47. }, {
  48. 'url': 'https://kick.com/kmack710',
  49. 'only_matching': True,
  50. }]
  51. def _real_extract(self, url):
  52. channel = self._match_id(url)
  53. response = self._call_api(f'channels/{channel}', channel)
  54. if not traverse_obj(response, 'livestream', expected_type=dict):
  55. raise UserNotLive(video_id=channel)
  56. return {
  57. 'id': str(traverse_obj(
  58. response, ('livestream', ('slug', 'id')), get_all=False, default=channel)),
  59. 'formats': self._extract_m3u8_formats(
  60. response['playback_url'], channel, 'mp4', live=True),
  61. 'title': traverse_obj(
  62. response, ('livestream', ('session_title', 'slug')), get_all=False, default=''),
  63. 'description': traverse_obj(response, ('user', 'bio')),
  64. 'channel': channel,
  65. 'channel_id': str_or_none(traverse_obj(response, 'id', ('livestream', 'channel_id'))),
  66. 'uploader': traverse_obj(response, 'name', ('user', 'username')),
  67. 'uploader_id': str_or_none(traverse_obj(response, 'user_id', ('user', 'id'))),
  68. 'is_live': True,
  69. 'timestamp': unified_timestamp(traverse_obj(response, ('livestream', 'created_at'))),
  70. 'thumbnail': traverse_obj(
  71. response, ('livestream', 'thumbnail', 'url'), expected_type=url_or_none),
  72. 'categories': traverse_obj(response, ('recent_categories', ..., 'name')),
  73. }
  74. class KickVODIE(KickBaseIE):
  75. _VALID_URL = r'https?://(?:www\.)?kick\.com/video/(?P<id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})'
  76. _TESTS = [{
  77. 'url': 'https://kick.com/video/58bac65b-e641-4476-a7ba-3707a35e60e3',
  78. 'md5': '3870f94153e40e7121a6e46c068b70cb',
  79. 'info_dict': {
  80. 'id': '58bac65b-e641-4476-a7ba-3707a35e60e3',
  81. 'ext': 'mp4',
  82. 'title': '🤠REBIRTH IS BACK!!!!🤠!stake CODE JAREDFPS 🤠',
  83. 'description': 'md5:02b0c46f9b4197fb545ab09dddb85b1d',
  84. 'channel': 'jaredfps',
  85. 'channel_id': '26608',
  86. 'uploader': 'JaredFPS',
  87. 'uploader_id': '26799',
  88. 'upload_date': '20240402',
  89. 'timestamp': 1712097108,
  90. 'duration': 33859.0,
  91. 'thumbnail': r're:^https?://.*\.jpg',
  92. 'categories': ['Call of Duty: Warzone'],
  93. },
  94. 'params': {
  95. 'skip_download': 'm3u8',
  96. },
  97. 'expected_warnings': [r'impersonation'],
  98. }]
  99. def _real_extract(self, url):
  100. video_id = self._match_id(url)
  101. response = self._call_api(f'video/{video_id}', video_id)
  102. return {
  103. 'id': video_id,
  104. 'formats': self._extract_m3u8_formats(response['source'], video_id, 'mp4'),
  105. 'title': traverse_obj(
  106. response, ('livestream', ('session_title', 'slug')), get_all=False, default=''),
  107. 'description': traverse_obj(response, ('livestream', 'channel', 'user', 'bio')),
  108. 'channel': traverse_obj(response, ('livestream', 'channel', 'slug')),
  109. 'channel_id': str_or_none(traverse_obj(response, ('livestream', 'channel', 'id'))),
  110. 'uploader': traverse_obj(response, ('livestream', 'channel', 'user', 'username')),
  111. 'uploader_id': str_or_none(traverse_obj(response, ('livestream', 'channel', 'user_id'))),
  112. 'timestamp': unified_timestamp(response.get('created_at')),
  113. 'duration': float_or_none(traverse_obj(response, ('livestream', 'duration')), scale=1000),
  114. 'thumbnail': traverse_obj(
  115. response, ('livestream', 'thumbnail'), expected_type=url_or_none),
  116. 'categories': traverse_obj(response, ('livestream', 'categories', ..., 'name')),
  117. }