Skip to content

Instantly share code, notes, and snippets.

@revox
Last active November 3, 2020 14:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revox/874d449cf4c80abcc67e39f51d4cefdb to your computer and use it in GitHub Desktop.
Save revox/874d449cf4c80abcc67e39f51d4cefdb to your computer and use it in GitHub Desktop.
import twitter, json, sys, csv
# == OAuth Authentication ==
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
auth = twitter.oauth.OAuth(OAUTH_TOKEN,
OAUTH_TOKEN_SECRET,
CONSUMER_KEY,
CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
csvfile = open('election2020.csv', 'w')
csvwriter = csv.writer(csvfile,delimiter ='|')
q = "election2020"
# clean up our data so we can write unicode to CSV
def clean(val):
clean = ""
if val:
val = val.replace('|', ' ')
val = val.replace('\n', ' ')
val = val.replace('\r', ' ')
clean = val
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:
if tweet['truncated']:
tweet_text = tweet['extended_tweet']['full_text']
else:
tweet_text = tweet['text']
csvwriter.writerow([tweet['id_str'],
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'])
])
print (tweet_text)
except Exception as err:
print (err)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment