Skip to content

Instantly share code, notes, and snippets.

@davebshow
Last active August 29, 2015 14:24
Show Gist options
  • Save davebshow/64274fd2ad1ec979acc8 to your computer and use it in GitHub Desktop.
Save davebshow/64274fd2ad1ec979acc8 to your computer and use it in GitHub Desktop.
import json
import time
from TwitterAPI import TwitterAPI
# This is the object that we will use to send the request to Twitter
# A request is a HTTP request, just like your browser does when it goes to a website!
api = TwitterAPI(consumer_key, consumer_secret, access_token_key,
access_token_secret)
# UNTESTED!!!
def get_tweets():
# This asks twitter to open up a stream of data
# The stream sends tweets that come from the specified location.
r = api.request('statuses/filter', {'locations': '-74,40,-73,41'})
# This `with` statement is what is called a context manager
# It allows you to open a file, write to it, then it will handle closing the file so you don't have to.
with open("output.txt", "w") as f:
# This is a time. Literally ten seconds in the future. Can be adjusted by chaning 10 to another # of seconds
timeout = time.time() + 10 # Run for ten seconds.
# While current time is less than ten seconds in the future (from start). After ten seconds quit.
for tweet in r:
if time.time() >= timeout:
break
# This writes the data to a file, followed by a newline character (1 tweet per line)
data = json.dumps(tweet)
print(data)
f.write("{0}\n".format(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment