From c8e84e3d321e5a10afb7f23575b458cf02fc6b29 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Wed, 12 Dec 2012 17:20:42 +0100 Subject: [PATCH] test with gedit --- .gitignore | 2 + streaming_test.py | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 streaming_test.py diff --git a/.gitignore b/.gitignore index e69de29..b7f9d6e 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +oauth.keys +basic.keys diff --git a/streaming_test.py b/streaming_test.py new file mode 100644 index 0000000..e10733b --- /dev/null +++ b/streaming_test.py @@ -0,0 +1,122 @@ +from tweepy.streaming import StreamListener +from tweepy import OAuthHandler +from tweepy import Stream +from tweepy.auth import BasicAuthHandler +from tweepy.auth import AuthHandler + +from textwrap import TextWrapper + +# Go to http://dev.twitter.com and create an app. +# The consumer key and secret will be generated for you after + +# After the step above, you will be redirected to your app's page. +# Create an access token under the the "Your access token" section + + +class StreamWatcherListener(StreamListener): + + status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ') + + def on_status(self, status): + try: + print self.status_wrapper.fill(status.text) + print '\n %s %s via %s\n' % (status.author.screen_name, status.created_at, status.source) + except: + # Catch any unicode errors while printing to console + # and just ignore them to avoid breaking application. + pass + + def on_error(self, status_code): + print 'An error has occured! Status code = %s' % status_code + return True # keep stream alive + + def on_timeout(self): + print 'Snoozing Zzzzzz' + + +class StdOutListener(StreamListener): + """ A listener handles tweets are the received from the stream. + This is a basic listener that just prints received tweets to stdout. + """ + + def on_data(self, data): + print data + return True + + def on_error(self, status): + print status + + +class Authentification(AuthHandler): + """ + Extracts private connexion information to authenticate to Twitter. + Avoids having to disclose private information on the web. + """ + + def __init__(self, oauth=False): + """ + Creates authentication depending on user request. + Default is basic auth (user/pass), but oauth is also possible + """ + self.auth = None + + if oauth: + file_name = "oauth.keys" + self.basic_authenticate(file_name) + else: + file_name = "basic.keys" + self.oauth_authenticate(file_name) + + def basic_authenticate(self, file_name): + """ + Creates an Authhandler using Basic method + """ + try: + with open(file_name) as f: + cred = f.readine() .split(',') + except IOError: + print "Error : Authentication file not found" + + if len(cred) != 2: + print "Error : Expecting to retrieve 2 values" + else: + #username, password + self.auth = BasicAuthHandler(cred[0], cred[1]) + + def oauth_authenticate(self, file_name): + """ + Creates an Authhandler using OAuth method + Also sets access token. + Needs both consumer key and secret and + access token and token_secret + """ + try: + with open(file_name) as f: + consumer = f.readine() .split(',') + access = f.readine() .split(',') + except IOError: + print "Error : Authentication file not found" + + if len(consumer) + len(access) != 4: + print "Error : Expecting to retrieve 4 values" + else: + #consumer_key, consumer_secret + self.auth = OAuthHandler(consumer[0], consumer[1]) + #access_token, access_token_secret + self.auth.set_access_token(access[0], access[1]) + + def get_auth(self): + return self.auth + + +if __name__ == '__main__': + # l = StdOutListener() + l = StreamWatcherListener() + + myAuth = Authentification(oauth = False) + #auth = OAuthHandler(consumer_key, consumer_secret) + #auth.set_access_token(access_token, access_token_secret) + #auth = BasicAuthHandler(username, password) + + stream = Stream(myAuth.get_auth(), l) + stream.filter(track=['basketball'])