config | ||
src | ||
.gitignore | ||
Cargo.lock | ||
Cargo.toml | ||
README.md |
Twitter Monitor
A Rust-based Twitter monitoring service that streams tweets from your home timeline to a web frontend. Supports both cURL-based authentication and Twitter API authentication.
Features
- Real-time home timeline monitoring
- WebSocket-based streaming
- Detailed response logging to files
- Support for both cURL and API authentication
- Clean, responsive web interface
- Configurable polling intervals with countdown timer
- Robust error handling and retries
How It Works
The application consists of three main components:
- Twitter Client: Handles authentication and API requests to fetch tweets
- WebSocket Server: Streams tweets to connected clients in real-time
- Frontend: Displays tweets and provides a user interface
Authentication Methods
cURL-based Authentication (Recommended)
This method captures an authenticated browser session to access Twitter without API keys:
-
Get cURL Command:
# Using Chrome browser: 1. Go to x.com (Twitter) 2. Log in to your account 3. Navigate to your home timeline (click "For you" or "Following") 4. Press F12 to open DevTools 5. Go to the Network tab 6. Find the "HomeLatestTimeline" request 7. Right-click on the request 8. Select "Copy" > "Copy as cURL (bash)"
-
Save cURL Command:
# Create a file in the config directory mkdir -p config touch config/curl.txt # Paste the copied cURL command into curl.txt
Twitter API Authentication (Alternative)
For official API access (Not fully implemented yet):
- Create Twitter Developer Account at https://developer.twitter.com/
- Get API Keys from your Twitter Developer Portal
- Configure API Keys in your configuration file
Installation
-
Clone the Repository:
git clone https://github.com/yourusername/twitter-monitor cd twitter-monitor
-
Build the Project:
cargo build --release
-
Configure the Project: Create a configuration file in the
config
directory:# config/session.yaml twitter: # Path to your curl command file curl_file: "config/curl.txt" # Or use a bearer token (coming soon) # bearer_token: "your_bearer_token" monitoring: # Polling interval in seconds polling_interval: 60 # Maximum tweets to fetch per request max_tweets: 10 server: # WebSocket server host host: "127.0.0.1" # WebSocket server port port: 8080
Running the Project
Start the server with:
# Development mode
cargo run -- --config config/session.yaml
# Production mode
cargo run --release -- --config config/session.yaml
The application will:
- Parse the cURL command to extract authentication details
- Start monitoring your Twitter home timeline
- Start a WebSocket server to stream tweets
- Save all Twitter API responses to files for debugging
Project Architecture
Core Components
-
TwitterClient (
src/twitter/client.rs
)- Responsible for Twitter API interactions
- Handles both cURL-based and API token authentication
- Implements tweet parsing and monitoring logic
- Includes robust error handling and retries
-
CurlAuth (
src/twitter/curl.rs
)- Parses cURL commands to extract authentication data
- Extracts URL, headers, cookies, and request data
- Provides logging of parsed components
-
Server (
src/server.rs
)- Manages WebSocket connections
- Broadcasts tweets to connected clients
- Implements client heartbeat mechanisms
-
Main (
src/main.rs
)- Coordinates application components
- Loads configuration and initializes services
- Sets up monitoring and broadcasting
Technical Challenges Solved
-
Twitter API Authentication
- Implemented parsing of cURL commands to extract session cookies
- Added required feature flags to match Twitter's GraphQL API requirements
- Handled authentication errors with clear messaging
-
Robust Data Handling
- Implemented safe UTF-8 text handling to avoid panics
- Added detailed response logging to files for debugging
- Carefully parsed nested JSON structures
-
Real-time Communication
- Built WebSocket server with proper connection handling
- Implemented heartbeat mechanism to detect disconnected clients
- Added broadcast functionality with error handling
-
User Experience
- Added countdown timer between scans
- Implemented clean logging for better readability
- Provided detailed status updates during operation
Troubleshooting
cURL Authentication Issues
-
Session Expired:
- Twitter sessions expire after some time
- Generate a new cURL command and update your
curl.txt
file - Restart the application
-
400 Bad Request Errors:
- Twitter's API requirements change frequently
- The application automatically adds required feature flags
- Response details are saved to
twitter_response_*.txt
files for debugging
-
Parse Errors:
- Twitter's response format may change
- Check the saved response files
- Update the parsing logic if necessary
Configuration Options
twitter:
# Path to your curl command file
curl_file: "config/curl.txt"
# Or use a bearer token (coming soon)
# bearer_token: "your_bearer_token"
monitoring:
# Polling interval in seconds
polling_interval: 60
# Maximum tweets to fetch per request
max_tweets: 10
server:
# WebSocket server host
host: "127.0.0.1"
# WebSocket server port
port: 8080
License
This project is licensed under the MIT License - see the LICENSE file for details.