TikTokLive.client package
Submodules
TikTokLive.client.base module
- class TikTokLive.client.base.BaseClient(unique_id: str, loop: Optional[asyncio.events.AbstractEventLoop] = None, client_params: Optional[dict] = None, headers: Optional[dict] = None, timeout_ms: Optional[int] = None, polling_interval_ms: int = 1000, process_initial_data: bool = True, fetch_room_info_on_connect: bool = True, enable_extended_gift_info: bool = True, trust_env: bool = False, proxy_container: Optional[TikTokLive.client.proxy.ProxyContainer] = None, lang: Optional[str] = 'en-US')
Bases:
object
Base client responsible for long polling to the TikTok Webcast API
- async add_proxies(*proxies: str) None
Add proxies to the proxy container for request usage
- Parameters
proxies – Proxies for usage
- Returns
None
- property available_gifts: Dict[int, TikTokLive.types.objects.ExtendedGift]
Available gift information for live room
- Returns
Gift info
- property connected: bool
Whether the client is connected
- Returns
Result
- async get_proxies() List[str]
Get a list of the current proxies in the proxy container being used for requests
- Returns
The proxies in the request container
- async remove_proxies(*proxies: str) None
Remove proxies from the proxy container for request usage
- Parameters
proxies – Proxies to remove
- Raises
ValueError – Raises ValueError if proxy is not present
- Returns
None
- async retrieve_available_gifts() Optional[Dict[int, TikTokLive.types.objects.ExtendedGift]]
Retrieve available gifts from Webcast API
- Returns
None
- async retrieve_room_info() Optional[dict]
Method to retrieve room information
- Returns
Dictionary containing all room info
- property room_id: Optional[int]
Room ID if the connection was successful
- Returns
Room’s ID
- property room_info: Optional[dict]
Room info dict if the connection was successful
- Returns
Room Info Dict
- run(session_id: Optional[str] = None) None
Run client while blocking main thread
- Returns
None
- async send_message(text: str, session_id: Optional[str] = None) Optional[str]
Send a message to the TikTok Live Chat
- Parameters
-
text – The message you want to send to the chat
session_id – The Session ID (If you’ve already supplied one, you don’t need to)
- Returns
None
- async set_proxies_enabled(enabled: bool) None
Set whether to use proxies in requests
- Parameters
enabled – Whether proxies are enabled or not
- Returns
None
- async start(session_id: Optional[str] = None) Optional[str]
Start the client without blocking the main thread
- Returns
Room ID that was connected to
- async stop() None
Stop the client
- Returns
None
- property unique_id: str
Unique ID of the streamer
- Returns
Their unique ID
- property viewer_count: Optional[int]
Return viewer count of user
- Returns
Viewer count
TikTokLive.client.client module
- class TikTokLive.client.client.TikTokLiveClient(unique_id: str, debug: bool = False, **options)
Bases:
pyee.AsyncIOEventEmitter
,TikTokLive.client.base.BaseClient
TikTokLive Client responsible for emitting events asynchronously
TikTokLive.client.http module
- class TikTokLive.client.http.TikTokHTTPClient(headers: Optional[Dict[str, str]] = None, timeout_ms: Optional[int] = None, proxy_container: Optional[TikTokLive.client.proxy.ProxyContainer] = None, trust_env: bool = True)
Bases:
object
Client for making HTTP requests to TikTok’s Webcast API
- DEFAULT_CLIENT_PARAMS: Dict[str, Union[int, bool, str]] = {'aid': 1988, 'app_name': 'tiktok_web', 'browser_language': 'en', 'browser_name': 'Mozilla', 'browser_online': True, 'browser_platform': 'Win32', 'browser_version': '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36', 'cookie_enabled': True, 'cursor': '', 'device_platform': 'web', 'did_rule': 3, 'fetch_rule': 1, 'identity': 'audience', 'internal_ext': '', 'last_rtt': 0, 'live_id': 12, 'priority_region': 'US', 'region': 'US', 'resp_content_type': 'protobuf', 'screen_height': 1152, 'screen_width': 2048, 'tz_name': 'Europe/Berlin', 'version_code': 180800}
- DEFAULT_REQUEST_HEADERS: Dict[str, str] = {'Accept': 'text/html,application/json,application/protobuf', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Origin': 'https://www.tiktok.com', 'Referer': 'https://www.tiktok.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'}
- TIKTOK_HTTP_ORIGIN: str = 'https://www.tiktok.com'
- TIKTOK_URL_WEB: str = 'https://www.tiktok.com/'
- TIKTOK_URL_WEBCAST: str = 'https://webcast.tiktok.com/webcast/'
- async get_deserialized_object_from_webcast_api(path: str, params: dict, schema: str) dict
Retrieve and deserialize an object from the Webcast API
- Parameters
-
path – Webcast path
params – Parameters to encode into URL
schema – Proto schema to decode from
- Returns
Deserialized data from API in dictionary format
- Raises
asyncio.TimeoutError
- async get_json_object_from_webcast_api(path: str, params: dict) dict
Retrieve JSON formatted data from the Webcast API
- Parameters
-
path – Webcast path
params – Parameters to encode into URL
- Returns
JSON data from Webcast API
- Raises
asyncio.TimeoutError
- async get_livestream_page_html(unique_id: str) str
Get livestream page HTML given a unique id
- Parameters
unique_id – Unique ID of the streamer
- Returns
HTML string containing page data
- Raises
asyncio.TimeoutError
- async post_json_to_webcast_api(path: str, params: dict, json: Optional[dict] = None)
Post JSON to the Webcast API
- Parameters
-
path – Path to POST
params – URLEncoded Params
json – JSON Data
- Returns
Result
TikTokLive.client.proxy module
- class TikTokLive.client.proxy.ProxyContainer(*proxies: str, mode: int = 1, enabled: bool = True)
Bases:
object
- property count: int
Get the current number of proxies in the container
- Returns
The current number of proxies
- get() Optional[str]
Fetch a proxy using one of the rotation settings defined in RotationSetting
- Returns
The HTTP/S proxy to return
- set_enabled(enabled: bool) None
Set whether the system is enabled
- Parameters
enabled – Whether to pull a proxy on get()
- Returns
None
- set_pinned(index: int) None
Set the proxy rotator to pinned mode in RotationSetting enum
- Parameters
index – Index to pin to
- Returns
None
- set_unpinned() None
Remove pinned status and return to whatever mode was set before set_pinned
:return:None
- class TikTokLive.client.proxy.RotationSetting(value)
Bases:
enum.Enum
Rotation settings for a proxy container
- CONSECUTIVE: int = 1
Rotate proxies consecutively, from proxy 0 -> 1 -> 2 -> …etc.
- PINNED: int = 3
Don’t rotate proxies at all, pin to a specific proxy index with set_pinned()
- RANDOM: int = 2
Rotate proxies randomly, from proxy 0 -> 69 -> 420 -> 1 -> …etc.