Skip to content

Instantly share code, notes, and snippets.

@jnawjux
Last active July 11, 2021 06:19
Show Gist options
  • Save jnawjux/92ed61fef201d21dfcb8aa06e35d4083 to your computer and use it in GitHub Desktop.
Save jnawjux/92ed61fef201d21dfcb8aa06e35d4083 to your computer and use it in GitHub Desktop.
# Import packages and config
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from config import consumer_key, consumer_secret, access_token, access_token_secret
import datetime
import csv
# Takes tweets and a designated csv file and writes them to it.
class StdOutListener(StreamListener):
def on_status(self, status):
# Filtering English language tweets from users with more than 500 followers
if (status.lang == "en") & (status.user.followers_count >= 500):
# Creating this formatting so when exported to csv the tweet stays on one line
tweet_text = "'" + status.text.replace('\n', ' ') + "'"
csvw.writerow([status.id,
status.user.screen_name,
# created_at is a datetime object, converting to just grab the month/day/year
status.created_at.strftime('%m/%d/%y'),
status.user.followers_count,
tweet_text])
return True
def on_error(self, status_code):
if status_code == 420:
# returning False in on_error disconnects the stream
return False
if __name__ == '__main__':
# This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
# Filter based on listed items
csvw = csv.writer(open("blank.csv", "a"))
csvw.writerow(['twitter_id', 'name', 'created_at',
'followers_count', 'text'])
stream.filter(track=['star wars'])
@jnawjux
Copy link
Author

jnawjux commented Apr 4, 2021

Hey, it should just output to the location of this file, but can be updated in that last section where it’s writing the csv.

@Minakoaino
Copy link

Hello! This script works perfect but doesn't show extended tweets :( I tried adding extended_tweets parameter in the stream class but unfortunately does not work.

@jnawjux
Copy link
Author

jnawjux commented Jul 11, 2021

Hey, haven’t tested this code in a bit. You may just need to review the API docs for the correct parameter name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment