Skip to content

Instantly share code, notes, and snippets.

@planglois925
Last active September 1, 2017 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save planglois925/e0c8116a3bb89f32bfc6615fd99f6400 to your computer and use it in GitHub Desktop.
Save planglois925/e0c8116a3bb89f32bfc6615fd99f6400 to your computer and use it in GitHub Desktop.
Part one of the tutorial on monitoring twitter
import tweepy
from tweepy.streaming import StreamListener
def main():
print "[] Twitter monitoring starting"
# these are the values associated with the both the application and consumer
#update these to the values that you gained from the previous section
ckey = ""
csecret = ""
atoken = ""
asecret = ""
#create the auth object leveraging the client key + client secret
auth = tweepy.OAuthHandler(ckey, csecret)
#set the application token + application secret to the new auth object
auth.set_access_token(atoken, asecret)
# now that we have our authentication set, we can connect to the API
api = tweepy.API(auth)
# the listener the API which we've defined below
listener = Listener(api)
#when our powers combine, twitter allows us to connect!
streamer = tweepy.Stream(auth=auth, listener=listener)
print '[] Twitter successfully connected'
#intrim use of track, we'll make sure this value is pulled down from a file in the final version
track = ['test','awesome','hello','world']
streamer.filter(track=track)
# we need to extend the listener class from Tweepy and build upon it
class Listener(StreamListener):
def __init__(self, api=None):
self.api = api or tweepy.API()
#simple proof of concept that will trigger when we recieve a status
def on_status(self, status):
#In our example we're going to print from the status object we recieved
#from twitter the name associated wit the user who posted the tweet
print '%s tweeted something' % status.user.name
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment