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! ๐Ÿ‘‡

Top comments (0)