❤️❤️🎁 Connect to TikTok live in 3 lines 🎁❤️❤️
Introduction
A Rust library. Use it to receive live stream events such as comments and gifts in realtime from TikTok LIVE No credentials are required.
Join the support discord and visit the #rust-support channel for questions, contributions and ideas. Feel free to make pull requests with missing/new features, fixes, etc
Do you prefer other programming languages?
NOTE: This is not an official API. It's a reverse engineering project.
Overview
Getting started
Dependencies
[dependencies]
tiktoklive = "0.0.12"
tokio = { version = "1.35.1", features = ["full"] }
log = "0.4"
env_logger = "0.10.1"
Usage example
use env_logger::{Builder, Env}; use log::LevelFilter; use std::time::Duration; use tiktoklive::{
core::live_client::TikTokLiveClient,
data::live_common::TikTokLiveSettings,
errors::LibError,
generated::events::TikTokLiveEvent,
TikTokLive,
};
use tokio::signal;
#[tokio::main] async fn main() {
init_logger("info");
let user_name = "tragdate";
let client = create_client(user_name);
let handle = tokio::spawn(async move {
if let Err(e) = client.connect().await {
match e {
LibError::LiveStatusFieldMissing => {
println!(
"Failed to get live status (probably needs authenticated client): {}",
e
);
let auth_client = create_client_with_cookies(user_name); if let Err(e) = auth_client.connect().await {
eprintln!("Error connecting to TikTok Live after retry: {}", e);
}
}
_ => {
eprintln!("Error connecting to TikTok Live: {}", e);
}
}
}
});
signal::ctrl_c().await.expect("Failed to listen for Ctrl+C");
handle.await.expect("The spawned task has panicked"); }
fn handle_event(_client: &TikTokLiveClient, event: &TikTokLiveEvent) {
match event {
TikTokLiveEvent::OnMember(join_event) => {
println!("user: {} joined", join_event.raw_data.user.nickname);
}
TikTokLiveEvent::OnChat(chat_event) => {
println!(
"user: {} -> {}",
chat_event.raw_data.user.nickname, chat_event.raw_data.content
);
}
TikTokLiveEvent::OnGift(gift_event) => {
let nick = &gift_event.raw_data.user.nickname;
let gift_name = &gift_event.raw_data.gift.name;
let gifts_amount = gift_event.raw_data.gift.combo;
println!(
"user: {} sends gift: {} x {}",
nick, gift_name, gifts_amount
);
}
TikTokLiveEvent::OnLike(like_event) => {
let nick = &like_event.raw_data.user.nickname;
println!("user: {} likes", nick);
}
_ => {} }
}
fn init_logger(default_level: &str) {
let env = Env::default().filter_or("LOG_LEVEL", default_level); Builder::from_env(env) .filter_module("tiktoklive", LevelFilter::Debug) .init(); }
fn configure(settings: &mut TikTokLiveSettings) {
settings.http_data.time_out = Duration::from_secs(12); }
fn configure_with_cookies(settings: &mut TikTokLiveSettings) {
settings.http_data.time_out = Duration::from_secs(12); let contents = "YOUR_COOKIES"; settings
.http_data
.headers
.insert("Cookie".to_string(), contents.to_string()); }
fn create_client(user_name: &str) -> TikTokLiveClient {
TikTokLive::new_client(user_name) .configure(configure) .on_event(handle_event) .build() }
fn create_client_with_cookies(user_name: &str) -> TikTokLiveClient {
TikTokLive::new_client(user_name) .configure(configure_with_cookies) .on_event(handle_event) .build() }```
## Contributing
Your improvements are welcome! Feel free to open an <a href="https://github.com/jwdeveloper/TikTok-Live-Java/issues">issue</a> or <a href="https://github.com/jwdeveloper/TikTok-Live-Java/pulls">pull request</a>.
## Contributors
[Zmole Cristian](https: