Skip to content

Instantly share code, notes, and snippets.

@edsu
Created March 24, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsu/897e7181fbd8a25bd986257e8372b5c4 to your computer and use it in GitHub Desktop.
Save edsu/897e7181fbd8a25bd986257e8372b5c4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Twitter's rate limits allow App Auth contexts to search at 450 requests
every 15 minutes, and User Auth contexts at 180 requests per 15 minutes.
This script exercises both contexts and counts how tweets it is able to
receive. We should see a significant number more tweets coming back for App
Auth.
Typical output should look like:
app auth: 44999
user auth: 18000
https://developer.twitter.com/en/docs/basics/rate-limits
"""
import logging
from twarc import Twarc
from datetime import datetime
from datetime import timedelta
logging.basicConfig(
filename='time_test.log',
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s"
)
def count_tweets(app_auth):
"""
Search for covid_19 in tweets using the given context and return the number
of tweets that were fetched in 10 minutes.
"""
count = 0
t = Twarc(app_auth=app_auth)
start = None
for tweet in t.search('covid_19'):
# start the timer when we get the first tweet
if start is None:
start = datetime.now()
count += 1
if datetime.now() - start > timedelta(minutes=10):
break
t.client.close()
return count
print('app auth: ', count_tweets(app_auth=True))
print('user auth: ', count_tweets(app_auth=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment