-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkick.py
153 lines (135 loc) · 5.01 KB
/
kick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
$description Kick, a gaming livestreaming platform
$url kick.com
$type live, vod
"""
import json
import os
import subprocess
import platform
import re
import logging
from streamlink.plugin import Plugin, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream import HLSStream, HTTPStream
from streamlink.utils.parse import parse_json
from streamlink.exceptions import PluginError
# Check if environment variable curl_cmd exists to choose curl-impersonate build. Default is curl_chrome110
curl_cmd = os.environ.get('CURL_CMD', 'curl_chrome110')
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "windows" if platform.system() == "Windows" else "linux")
executable_filepath = os.path.join(filepath, curl_cmd)
log = logging.getLogger(__name__)
@pluginmatcher(
re.compile(
# https://github.com/yt-dlp/yt-dlp/blob/9b7a48abd1b187eae1e3f6c9839c47d43ccec00b/yt_dlp/extractor/kick.py#LL33-L33C111
r"https?://(?:www\.)?kick\.com/(?!(?:video|categories|search|auth)(?:[/?#]|$))(?P<channel>[\w_-]+)$",
),
name="live",
)
@pluginmatcher(
re.compile(
# https://github.com/yt-dlp/yt-dlp/blob/2d5cae9636714ff922d28c548c349d5f2b48f317/yt_dlp/extractor/kick.py#LL84C18-L84C104
r"https?://(?:www\.)?kick\.com/video/(?P<video_id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})",
),
name="vod",
)
@pluginmatcher(
re.compile(
r"https?://(?:www\.)?kick\.com/(?!(?:video|categories|search|auth)(?:[/?#]|$))(?P<channel>[\w_-]+)\?clip=(?P<clip_id>[\d_]+)$",
),
name="clip",
)
class KICK(Plugin):
def _get_streams(self):
API_BASE_URL = "https://kick.com/api"
_LIVE_SCHEMA = validate.Schema(
validate.parse_json(),
{
"playback_url": validate.url(path=validate.endswith(".m3u8")),
"livestream": {
"is_live": True,
"id": int,
"session_title": str,
"categories": [{"name": str}],
},
"user": {"username": str},
},
validate.union_get(
"playback_url",
("livestream", "id"),
("user", "username"),
("livestream", "session_title"),
("livestream", "categories", 0, "name"),
),
)
_VIDEO_SCHEMA = validate.Schema(
validate.parse_json(),
{
"source": validate.url(path=validate.endswith(".m3u8")),
"id": int,
"livestream": {
"channel": {"user": {"username": str}},
"session_title": str,
"categories": [{"name": str}],
},
},
validate.union_get(
"source",
"id",
("livestream", "channel", "user", "username"),
("livestream", "session_title"),
("livestream", "categories", 0, "name"),
),
)
_CLIP_SCHEMA = validate.Schema(
validate.parse_json(),
{
"clip": {
"video_url": validate.url(path=validate.endswith(".mp4")),
"id": int,
"channel": {"username": str},
"title": str,
"category": {"name": str},
},
},
validate.union_get(
("clip", "video_url"),
("clip", "id"),
("clip", "channel", "username"),
("clip", "title"),
("clip", "category", "name"),
),
)
live, vod, clip = (
self.matches["live"],
self.matches["vod"],
self.matches["clip"],
)
try:
api_url = "{0}/{1}/{2}".format(
API_BASE_URL,
*(
["v1/channels", self.match["channel"]]
if live
else (
["v1/video", self.match["video_id"]]
if vod
else ["v2/clips", self.match["clip_id"]]
)
)
)
command = [curl_cmd, api_url, '--insecure']
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,text=True)
url, self.id, self.author, self.title, self.category = (
_LIVE_SCHEMA if live else (_VIDEO_SCHEMA if vod else _CLIP_SCHEMA)
).validate(result.stdout)
except (PluginError, TypeError) as err:
log.debug(err)
return
if live or vod:
yield from HLSStream.parse_variant_playlist(self.session, url).items()
elif (
clip and self.author.casefold() == self.match["channel"].casefold()
): # Sanity check if the clip channel is the same as the one in the URL
yield "source", HTTPStream(self.session, url)
__plugin__ = KICK