Instantly share code, notes, and snippets.

@edsu /max_id_bug.py
Last active Aug 29, 2015

Embed
What would you like to do?
Add your Twitter credentials to your environment, run this and notice how the second request yields only one tweet? Try running again with a sleep between them (uncomment line 27) and notice how the second request finds results this time? This seems to be a bug, or a new "feature" intended to prevent using the API to walk backwards in search res…
#!/usr/bin/env python
# you'll need to install python's oauth2 for this to work
import os
import json
import time
import oauth2
ck = os.environ.get('CONSUMER_KEY')
cks = os.environ.get('CONSUMER_SECRET')
at = os.environ.get('ACCESS_TOKEN')
ats = os.environ.get("ACCESS_TOKEN_SECRET")
consumer = oauth2.Consumer(key=ck, secret=cks)
token = oauth2.Token(at, ats)
client = oauth2.Client(consumer, token, timeout=60)
q = 'dconstruct'
print "request #1"
resp, content = client.request('https://api.twitter.com/1.1/search/tweets.json?count=100&q=%s' % q)
for s in json.loads(content)['statuses']:
print s['id_str']
last_id = int(s['id_str'])
# time.sleep(20)
print "request #2"
resp, content = client.request('https://api.twitter.com/1.1/search/tweets.json?count=100&q=%s&max_id=%i' % (q, last_id))
for s in json.loads(content)['statuses']:
print s['id_str']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment