test with gedit

This commit is contained in:
2012-12-12 17:20:42 +01:00
parent cde99cd427
commit c8e84e3d32
2 changed files with 124 additions and 0 deletions

2
.gitignore vendored
View File

@@ -0,0 +1,2 @@
oauth.keys
basic.keys

122
streaming_test.py Normal file
View File

@@ -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'])