Skip to content

Instantly share code, notes, and snippets.

@revox
Last active August 29, 2015 14:10
Show Gist options
  • Save revox/ad18339ef47a38e3fec3 to your computer and use it in GitHub Desktop.
Save revox/ad18339ef47a38e3fec3 to your computer and use it in GitHub Desktop.
Follow a user or list of users tweets and write them to CSV
# Finding topics of interest by using the filtering capablities it offers.
import twitter, json, sys, csv
# == OAuth Authentication ==
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key=""
consumer_secret=""
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""
auth = twitter.oauth.OAuth(access_token, access_token_secret,consumer_key, consumer_secret)
twitter_api = twitter.Twitter(auth=auth)
csvfile = open('tweetdata.csv', 'a')
csvwriter = csv.writer(csvfile)
# what does following a user mean - https://dev.twitter.com/streaming/overview/request-parameters#follow
u = 918797696
print >> sys.stderr, 'Filtering the public timeline for a user="%s"' % (u,)
# Reference the self.auth parameter
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
# See https://dev.twitter.com/docs/streaming-apis
stream = twitter_stream.statuses.filter(follow=u)
# For illustrative purposes, when all else fails, search for Justin Bieber
# and something is sure to turn up (at least, on Twitter)
for tweet in stream:
print tweet['text']
user = tweet['user']['screen_name']
# print json.dumps(tweet, indent=1)
csvwriter.writerow([tweet['created_at'],user.encode('utf-8'),tweet['text'].encode('utf-8')])
# sys.exit()
# Save to a database in a particular collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment