Skip to content

Commit 2ba20ed

Browse files
authored
Merge pull request #19 from gub-7/get_stream_destination_info
2 parents f636f36 + 2672c85 commit 2ba20ed

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

kick/client.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .http import HTTPClient
1111
from .livestream import PartialLivestream
1212
from .message import Message
13-
from .users import ClientUser, PartialUser, User
13+
from .users import ClientUser, PartialUser, User, DestinationInfo
1414
from .utils import MISSING, decorator, setup_logging
1515

1616
if TYPE_CHECKING:
@@ -213,6 +213,28 @@ async def fetch_user(self, name: str, /) -> User:
213213
user = User(data=data, http=self.http)
214214
return user
215215

216+
async def fetch_stream_url_and_key(self) -> DestinationInfo:
217+
"""
218+
|coro|
219+
220+
Fetches your stream URL and stream key from the API.
221+
You must be authenticated to use this endpoint.
222+
223+
Raises
224+
-----------
225+
HTTPException
226+
Fetching Failed
227+
Forbidden
228+
You are not authenticated
229+
230+
Returns
231+
-----------
232+
DestinationInfo
233+
"""
234+
235+
data = await self.http.fetch_stream_destination_url_and_key()
236+
return DestinationInfo(data=data)
237+
216238
def dispatch(self, event_name: str, *args, **kwargs) -> None:
217239
event_name = f"on_{event_name}"
218240

kick/http.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343
ReplyOriginalSender,
4444
V1MessageSentPayload,
4545
)
46-
from .types.user import ChatterPayload, ClientUserPayload, UserPayload
46+
from .types.user import (
47+
ChatterPayload,
48+
ClientUserPayload,
49+
UserPayload,
50+
DestinationInfoPayload,
51+
)
4752
from .types.videos import GetVideosPayload
4853

4954
T = TypeVar("T")
@@ -60,10 +65,9 @@
6065
async def json_or_text(response: ClientResponse, /) -> Union[dict[str, Any], str]:
6166
text = await response.text()
6267
try:
63-
try:
64-
return json.loads(text)
65-
except json.JSONDecodeError:
66-
pass
68+
return json.loads(text)
69+
except json.JSONDecodeError:
70+
pass
6771
except KeyError:
6872
pass
6973

@@ -238,9 +242,11 @@ async def request(self, route: Route, **kwargs) -> Any:
238242
try:
239243
res = await self.__session.request(
240244
route.method,
241-
url
242-
if self.whitelisted is True
243-
else f"{self.bypass_host}:{self.bypass_port}/request?url={url}",
245+
(
246+
url
247+
if self.whitelisted is True
248+
else f"{self.bypass_host}:{self.bypass_port}/request?url={url}"
249+
),
244250
headers=headers,
245251
cookies=cookies,
246252
**kwargs,
@@ -488,6 +494,9 @@ def reply_to_message(
488494
def get_me(self) -> Response[ClientUserPayload]:
489495
return self.request(Route.root("GET", "/api/v1/user"))
490496

497+
def fetch_stream_destination_url_and_key(self) -> Response[DestinationInfoPayload]:
498+
return self.request(Route.root("GET", "/stream/publish_token"))
499+
491500
async def get_asset(self, url: str) -> bytes:
492501
if self.__session is MISSING:
493502
self.__session = ClientSession()

kick/types/user.py

+5
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,8 @@ class ClientUserPayload(TypedDict):
140140
streamer_channel: ClientUserStreamerChannelsPayload
141141
roles: list # Unknown
142142
profilepic: str | None
143+
144+
145+
class DestinationInfoPayload(TypedDict):
146+
rtmp_publish_path: str
147+
rtmp_stream_token: str

kick/users.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,33 @@
1616
if TYPE_CHECKING:
1717
from .chatroom import Chatroom
1818
from .http import HTTPClient
19-
from .types.user import ClientUserPayload, InnerUser, UserPayload
19+
from .types.user import (ClientUserPayload, InnerUser, UserPayload,
20+
DestinationInfoPayload)
2021

21-
__all__ = ("User", "Socials", "PartialUser", "ClientUser")
22+
__all__ = ("DestinationInfo", "Socials", "PartialUser", "User", "ClientUser")
23+
24+
25+
class DestinationInfo(BaseDataclass["DestinationInfoPayload"]):
26+
"""
27+
Information about a user's stream destination
28+
29+
Attributes
30+
-----------
31+
stream_url: str
32+
The URL for streaming
33+
stream_key: str
34+
The stream key
35+
"""
36+
37+
@property
38+
def stream_url(self) -> str:
39+
"""The URL for streaming"""
40+
return self._data["rtmp_publish_path"]
41+
42+
@property
43+
def stream_key(self) -> str:
44+
"""The stream key"""
45+
return self._data["rtmp_stream_token"]
2246

2347

2448
class Socials(BaseDataclass["InnerUser | ClientUserPayload"]):

0 commit comments

Comments
 (0)