Skip to content

Instantly share code, notes, and snippets.

@philshem philshem/
Last active Aug 29, 2015

What would you like to do?
Search twitter via the API and download all corresponding tweets, for later analysis.
import json
import twitter #
import time
def main():
api = twitter.Api(consumer_key='INSERT', \
consumer_secret='INSERT', \
access_token_key='INSERT', \
loops = 5000
with open('tweets.json','wb') as outfile: # json mirror
for search_string in ['advisor OR adviser']:
maxid = 1000000000000000000
for i in xrange(0,loops):
print 'maxid=',maxid,', twitter loop',i
results = api.GetSearch(search_string, count=100,max_id = maxid)
tmpid = maxid
for tweet in results:
tweet = str(tweet).replace('\n',' ').replace('\r',' ')
tweet = (json.loads(tweet))
maxid = int(tweet['id'])
time.sleep(5) # don't piss off twitter
if tmpid == maxid:
print maxid,tmpid,'break'
break # break out of search loop if results aren't updated (maximum is about 1 week)
if __name__ == "__main__":
print 'done with twitter.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.