From bb99154131ed22c6ec281649b9b3f758616f4149 Mon Sep 17 00:00:00 2001 From: Miwory Date: Thu, 11 Dec 2025 19:06:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20update=5Fuser=5Fchat=5Fcolor,=20create=5Fclip,?= =?UTF-8?q?=20get=5Fclips?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/twitchclient/api.py | 91 ++++++++++++++++++++++++++++++++++++++ src/twitchclient/schema.py | 29 ++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/twitchclient/api.py b/src/twitchclient/api.py index 1468f6c..871ad98 100644 --- a/src/twitchclient/api.py +++ b/src/twitchclient/api.py @@ -1145,3 +1145,94 @@ class TwitchAPIClient(AioHTTPXClient): case _: raise s.Error(req.status_code, 'Internal Server Error') + + async def update_user_chat_color( + self, + access_token: str, + user_id: int | str, + color: str, + ): + req = await self.put( + '/chat/color', + headers={'Authorization': f'Bearer {access_token}'}, + params={'user_id': user_id, 'color': color}, + ) + + match req.status_code: + case st.NO_CONTENT: + return True + + case st.BAD_REQUEST | st.UNAUTHORIZED: + raise s.Error(req.status_code, req.json()['message']) + + case _: + raise s.Error(req.status_code, 'Internal Server Error') + + async def create_clip( + self, + access_token: str, + broadcaster_id: int | str, + *, + has_delay: bool | None = None, + ): + req = await self.post( + '/clips', + headers={'Authorization': f'Bearer {access_token}'}, + params=self.clean_dict( + {'broadcaster_id': broadcaster_id, 'has_delay': has_delay} + ), + ) + + match req.status_code: + case st.ACCEPTED: + return True + + case ( + st.BAD_REQUEST | st.UNAUTHORIZED | st.FORBIDDEN | st.NOT_FOUND + ): + raise s.Error(req.status_code, req.json()['message']) + + case _: + raise s.Error(req.status_code, 'Internal Server Error') + + async def get_clips( + self, + access_token: str, + broadcaster_id: int | str | None = None, + game_id: int | str | None = None, + clip_id: str | list[str] | None = None, + started_at: datetime | None = None, + ended_at: datetime | None = None, + first: int = 20, + before: str | None = None, + after: str | None = None, + *, + is_featured: bool | None = None, + ): + req = await self.get( + '/clips', + headers={'Authorization': f'Bearer {access_token}'}, + params=self.clean_dict( + { + 'broadcaster_id': broadcaster_id, + 'game_id': game_id, + 'clip_id': clip_id, + 'started_at': started_at, + 'ended_at': ended_at, + 'first': first, + 'before': before, + 'after': after, + 'is_featured': is_featured, + } + ), + ) + + match req.status_code: + case st.OK: + return s.Clips.model_validate(req.json()).data + + case st.BAD_REQUEST | st.UNAUTHORIZED | st.NOT_FOUND: + raise s.Error(req.status_code, req.json()['message']) + + case _: + raise s.Error(req.status_code, 'Internal Server Error') diff --git a/src/twitchclient/schema.py b/src/twitchclient/schema.py index 99aedcb..bdde561 100644 --- a/src/twitchclient/schema.py +++ b/src/twitchclient/schema.py @@ -640,3 +640,32 @@ class UserChatColor(BaseModel): model_config = ConfigDict(extra='forbid') data: list[UserChatColorData] + + +class Clip(BaseModel): + model_config = ConfigDict(extra='forbid') + + id: str + url: str + embed_url: str + broadcaster_id: int + broadcaster_name: str + creator_id: int + creator_name: str + video_id: str + game_id: int + language: str + title: str + view_count: int + created_at: datetime + thumbnail_url: str + duration: float + vod_offset: int | None + is_featured: bool + + +class Clips(BaseModel): + model_config = ConfigDict(extra='forbid') + + data: list[Clip] + pagination: Pagination | dict[Any, Any] | None = None