A Rust port of the TypeScript library (https://github.com/ai16z/agent-twitter-client). This package does not require the Twitter API to use!
A Rust library that provides a scraper interface for the Twitter API. Easily interact with Twitter through authentication, timeline fetching, user operations, and more.
- Authentication with cookies
- Comprehensive user profile management
- Timeline retrieval
- Tweet interactions (like, retweet, post)
- Advanced search capabilities
- User relationship management (follow/unfollow)
Add these dependencies to your Cargo.toml
:
[dependencies]
agent-twitter-client = "0.1.1"
tokio = { version = "1.0", features = ["full"] }
dotenv = "0.15"
use agent_twitter_client::scraper::Scraper;
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
scraper.login(
env::var("TWITTER_USERNAME")?,
env::var("TWITTER_PASSWORD")?,
Some(env::var("TWITTER_EMAIL")?),
Some(env::var("TWITTER_2FA_SECRET")?)
).await?;
Ok(())
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
Ok(())
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
// Follow a user
scraper.follow_user("Rina_RIG").await?;
// Get user profile
let profile = scraper.get_profile("Rina_RIG").await?;
// Get user's followers
let (followers, next_cursor) = scraper.get_followers("Rina_RIG", 20, None).await?;
Ok(())
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
use std::io::Write;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
let home_timeline = scraper.get_direct_message_conversations("chakaboommm", None).await?;
println!("Direct message conversations: {:?}", home_timeline);
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
let conversation_id = "1234567890";
scraper.send_direct_message(conversation_id, "Hello, world!").await?;
Ok(())
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::search::SearchMode;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
// Search tweets
let tweets = scraper.search_tweets(
"@Rina_RIG",
20,
SearchMode::Latest,
None
).await?;
// Search user profiles
let profiles = scraper.search_profiles("rust", 20, None).await?;
Ok(())
}
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
// Get home timeline
let tweets = scraper.get_home_timeline(20, vec![]).await?;
// Get user's tweets and replies
let tweets = scraper.fetch_tweets_and_replies("username", 20, None).await?;
Ok(())
}
use std::fs::File;
use std::io::Read;
use agent_twitter_client::scraper::Scraper;
use agent_twitter_client::error::Result;
use dotenv::dotenv;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let mut scraper = Scraper::new().await?;
let cookie_string = std::env::var("TWITTER_COOKIE_STRING")
.expect("TWITTER_COOKIE_STRING environment variable not set");
scraper.set_from_cookie_string(&cookie_string).await?;
// Like a tweet
scraper.like_tweet("tweet_id").await?;
// Retweet
scraper.retweet("tweet_id").await?;
// Post a new tweet
scraper.send_tweet("Hello, Twitter!", None, None).await?;
// Send a simple tweet
let tweet = scraper.send_tweet("Hello world!", None, None).await?;
// Create media data tuple with image data and MIME type
let mut file = File::open("image.jpg")?;
let mut image_data = Vec::new();
file.read_to_end(&mut image_data)?;
let media_data = vec![(image_data, "image/jpeg".to_string())];
// Send the tweet with the image
let tweet_with_media = scraper.send_tweet(
"Check out this image!",
None,
Some(media_data)
).await?;
Ok(())
}
Create a .env
file with your credentials:
TWITTER_USERNAME=your_username
TWITTER_PASSWORD=your_password
TWITTER_EMAIL=[email protected]
TWITTER_2FA_SECRET=your_2fa_secret # Optional
TWITTER_COOKIE_STRING='your_cookie_string'
Created by Rina
We welcome contributions! Please feel free to submit a Pull Request.