### Install TikTokLive Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Install the TikTokLive module using pip. This is the first step to using the library. ```shell pip install TikTokLive ``` -------------------------------- ### start Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Connects to the live chat asynchronously without blocking the main thread. Returns an asyncio.Task object representing the client loop. ```APIDOC ## start ### Description Connects to the live chat without blocking the main thread. This returns an `asyncio.Task` object with the client loop. ### Method async ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### run Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Connects to the livestream and blocks the main thread. This method is recommended for simple scripts. ```APIDOC ## run ### Description Connect to the livestream and block the main thread. This is best for small scripts. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Basic TikTokLive Client Connection and Event Handling Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Connect to a TikTok live stream and handle connection and comment events. Use decorators or manual listener addition for event handling. ```python from TikTokLive import TikTokLiveClient from TikTokLive.events import ConnectEvent, CommentEvent # Create the client client: TikTokLiveClient = TikTokLiveClient(unique_id="@isaackogz") # Listen to an event with a decorator! @client.on(ConnectEvent) async def on_connect(event: ConnectEvent): print(f"Connected to @{event.unique_id} (Room ID: {client.room_id}") # Or, add it manually via "client.add_listener()" async def on_comment(event: CommentEvent) -> None: print(f"{event.user.nickname} -> {event.comment}") client.add_listener(CommentEvent, on_comment) if __name__ == '__main__': # Run the client and block the main thread # await client.start() to run non-blocking client.run() ``` -------------------------------- ### Listen to LikeEvent using Decorator Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Use the @client.on() decorator to listen for specific events like LikeEvent. This method is suitable for immediate event handling. ```python import TikTokLive client = TikTokLive.TikTokLive("test_user") @client.on(TikTokLive.LikeEvent) async def on_like(event: TikTokLive.LikeEvent) -> None: print(f"Received a like!") ``` -------------------------------- ### TikTokLiveClient Initialization Parameters Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md These parameters are used when initializing a TikTokLive client. They control aspects like proxying and underlying client arguments. ```APIDOC ## TikTokLiveClient Initialization Parameters ### Description Parameters used to configure the TikTokLive client upon initialization. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters Table | Param Name | Required | Default | Description | |--------------|------------|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | unique_id | Yes | N/A | The unique username of the broadcaster. You can find this name in the URL of the user. For example, the `unique_id` for [`https://www.tiktok.com/@isaackogz`](https://www.tiktok.com/@isaackogz) would be `isaackogz`. | | web_proxy | No | `None` | TikTokLive supports proxying HTTP requests. This parameter accepts an `httpx.Proxy`. Note that if you do use a proxy you may be subject to reduced connection limits at times of high load. || | ws_proxy | No | `None` | TikTokLive supports proxying the websocket connection. This parameter accepts an `httpx.Proxy`. Using this proxy will never be subject to reduced connection limits. | | web_kwargs | No | `{}` | Under the scenes, the TikTokLive HTTP client uses the [`httpx`](https://github.com/encode/httpx) library. Arguments passed to `web_kwargs` will be forward the the underlying HTTP client. | | ws_kwargs | No | `{}` | Under the scenes, TikTokLive uses the [`websockets`](https://github.com/python-websockets/websockets) library to connect to TikTok. Arguments passed to `ws_kwargs` will be forwarded to the underlying WebSocket client. | ``` -------------------------------- ### Listen to LikeEvent using Decorator Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Use the @client.on() decorator to register an asynchronous function to handle specific events like LikeEvent. This is a convenient way to manage event listeners. ```python import TikTokLive from TikTokLive.events import LikeEvent client = TikTokLive.TikTokLiveClient("username") @client.on(LikeEvent) async def on_like(event: LikeEvent) -> None: # Handle the like event here pass ``` -------------------------------- ### Handle Gift Events in Python Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md This snippet demonstrates how to handle `GiftEvent`s, differentiating between streakable and non-streakable gifts. It shows how to access gift details and user information. To fetch additional gift metadata, pass `fetch_gift_info=True` to `client.run()` or `client.start()`. ```python from TikTokLive.events import GiftEvent @client.on(GiftEvent) async def on_gift(event: GiftEvent): gift = event.gift if gift is None: return # Streakable gift & streak is over if not event.streaking and gift.type == 1: # ``type == 1`` is streakable print(f"{event.user.unique_id} sent {event.repeat_count}x \"{gift.name}\"\n") # Non-streakable gift elif gift.type != 1: print(f"{event.user.unique_id} sent \"{gift.name}\"\n") ``` -------------------------------- ### Handle GiftEvent and Process Gifts Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md This snippet demonstrates how to listen for GiftEvents and process them. It differentiates between streakable and non-streakable gifts, printing the sender, count, and gift name. Ensure `fetch_gift_info=True` is passed to `client.run()` or `client.start()` to cache gift details. ```python import ... @client.on(GiftEvent) async def on_gift(event: GiftEvent): gift = event.gift if gift is None: return # Streakable gift & streak is over if not event.streaking and gift.type == 1: # ``type == 1`` is streakable print(f"{event.user.unique_id} sent {event.repeat_count}x \"{gift.name}\"" # Non-streakable gift elif gift.type != 1: print(f"{event.user.unique_id} sent \"{gift.name}\"" ``` -------------------------------- ### TikTokLiveClient Methods Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Key methods available on the TikTokLiveClient object for managing the connection and event listeners. ```APIDOC ## TikTokLiveClient Methods ### Description Methods available on the `TikTokLiveClient` object for interacting with the livestream. ### Methods Table | Method Name | Notes | Description | |---------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | run | N/A | Connect to the livestream and block the main thread. This is best for small scripts. || | add_listener | N/A | Adds an *asynchronous* listener function (or, you can decorate a function with `@client.on(Type[Event])`) and takes two parameters, an event name and the payload, an AbstractEvent | | connect | `async` | Connects to the tiktok live chat while blocking the current future. When the connection ends (e.g. livestream is over), the future is released. || | start | `async` | Connects to the live chat without blocking the main thread. This returns an `asyncio.Task` object with the client loop. || | disconnect | `async` | Disconnects the client from the websocket gracefully, processing remaining events before ending the client loop. | ``` -------------------------------- ### connect Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Connects to the TikTok live chat asynchronously, blocking the current future. The future is released when the connection ends. ```APIDOC ## connect ### Description Connects to the tiktok live chat while blocking the current future. When the connection ends (e.g. livestream is over), the future is released. ### Method async ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Listen to CommentEvent using add_listener Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Register a callback function for events like CommentEvent using client.add_listener(). This is useful for decoupling event registration from event handling logic. ```python import TikTokLive client = TikTokLive.TikTokLive("test_user") async def on_comment(event: TikTokLive.CommentEvent) -> None: print(f"Received a comment: {event.comment}") client.add_listener(TikTokLive.CommentEvent, on_comment) ``` -------------------------------- ### add_listener Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Adds an asynchronous listener function to handle events. Listeners can also be added using the `@client.on(Type[Event])` decorator. ```APIDOC ## add_listener ### Description Adds an *asynchronous* listener function (or, you can decorate a function with `@client.on(Type[Event])`) and takes two parameters, an event name and the payload, an AbstractEvent. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Listen to CommentEvent using add_listener Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Register an asynchronous function to handle CommentEvent by directly calling client.add_listener(). This method is an alternative to using decorators. ```python import TikTokLive from TikTokLive.events import CommentEvent client = TikTokLive.TikTokLiveClient("username") async def on_comment(event: CommentEvent) -> None: # Handle the comment event here pass client.add_listener(CommentEvent, on_comment) ``` -------------------------------- ### TikTokLiveClient Properties Source: https://github.com/isaackogan/tiktoklive/blob/master/docs/src/index.md Important properties of the TikTokLiveClient object that provide information about the current connection and client state. ```APIDOC ## TikTokLiveClient Properties ### Description Properties available on the `TikTokLiveClient` object that provide access to client state and information. ### Properties Table | Attribute Name | Description | |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | room_id | The Room ID of the livestream room the client is currently connected to. || | web | The TikTok HTTP client. This client has a lot of useful routes you should explore! || | connected | Whether you are currently connected to the livestream. || | logger | The internal logger used by TikTokLive. You can use `client.logger.setLevel(...)` method to enable client debug. || | room_info | Room information that is retrieved from TikTok when you use a connection method (e.g. `client.connect`) with the keyword argument `fetch_room_info=True` . || | gift_info | Extra gift information that is retrieved from TikTok when you use a connection method (e.g. `client.run`) with the keyword argument `fetch_gift_info=True`. | ``` -------------------------------- ### disconnect Source: https://github.com/isaackogan/tiktoklive/blob/master/README.md Disconnects the client from the websocket gracefully, processing any remaining events before ending the client loop. ```APIDOC ## disconnect ### Description Disconnects the client from the websocket gracefully, processing remaining events before ending the client loop. ### Method async ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.