Last active
November 20, 2017 16:23
-
-
Save revox/9f17dafdd0e92bf13918 to your computer and use it in GitHub Desktop.
Simple twiiter streaming API consumer, writes to CSV file, ignores errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import twitter, json, sys, csv | |
# == OAuth Authentication == | |
consumer_key="" | |
consumer_secret="" | |
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('brexit_data.csv', 'w') | |
csvwriter = csv.writer(csvfile) | |
q = "brexit" | |
# small function encapsuate the encodes and protect replace nulls with empty values | |
def clean(val): | |
clean = "" | |
if val: | |
clean = val.encode('utf-8') | |
return clean | |
print 'Filtering the public timeline for keyword="%s"' % (q) | |
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth) | |
stream = twitter_stream.statuses.filter(track=q) | |
for tweet in stream: | |
# print json.dumps(tweet) | |
try: | |
csvwriter.writerow([tweet['created_at'], | |
clean(tweet['user']['screen_name']), | |
clean(tweet['text']), | |
tweet['user']['created_at'], | |
tweet['user']['followers_count'], | |
tweet['user']['friends_count'], | |
tweet['user']['statuses_count'], | |
clean(tweet['source']), | |
clean(tweet['user']['location']), | |
tweet['user']['geo_enabled'], | |
tweet['user']['lang'], | |
clean(tweet['user']['time_zone']), | |
tweet['retweet_count'] | |
]) | |
print tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8') | |
except Exception as e: | |
print e.message | |
# just print the error and keep going! we don't care if we lose a few tweets due to errors | |
# in a production version of this code we would write the errors into seperate file | |
pass | |
print "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment