Part one of the tutorial on monitoring twitter
This file contains 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 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