DEV Community

kaito
kaito

Posted on

📌 How to Monitor a Twitter Account's Latest Tweets with Python (10-Minute Alerts)

Need to track Twitter account activity for brand monitoring or competitor analysis? Here's an efficient solution using Python and the Twitter API Advanced Search endpoint.

Why This Matters

Perfect for tracking competitors' promotions

Monitor brand mentions in real-time

Track event-specific hashtags

Get alerts for crisis management

Step-by-Step Implementation

  1. Set Up Time Windows `from datetime import datetime, timedelta

now = datetime.now()
ten_mins_ago = now - timedelta(minutes=10)
since = int(ten_mins_ago.timestamp()) # Convert to Unix timestamp`

  1. Configure API Request
    url = "https://api.twitterapi.io/twitter/tweet/advanced_search"
    headers = {"X-API-Key": "YOUR_API_KEY"}
    params = {
    "query": f"from:target_username since_time:{since}",
    "queryType": "Latest",
    "include": "nativeretweets"
    }

  2. Handle Pagination
    has_next_page = True
    while has_next_page:
    response = requests.get(url, headers=headers, params=params)
    data = response.json()
    tweets = data.get('tweets', [])
    has_next_page = data.get('has_next_page', False)
    params['cursor'] = data.get('next_cursor')

Pro Tips

  • Add -filter:replies to exclude replies
  • Use sleep(590) for precise 10-minute intervals
  • Store results in CSV/SQLite for historical analysis
  • Implement error handling for API rate limits

Why TwitterAPI.io?

The used API endpoint from twitterapi.io provides:
✅ Native retweet inclusion
✅ Precise time window filtering
✅ Pagination support
✅ High request limits

This solution helps developers build reliable Twitter monitoring tools without platform rate limit issues. The 10-minute interval ensures fresh data while maintaining API compliance.

💬 Let me know what account you'd monitor with this setup! 👇

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs