mirror of
https://github.com/jlengrand/twitterboard.git
synced 2026-03-10 08:51:22 +00:00
test with gedit
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -0,0 +1,2 @@
|
||||
oauth.keys
|
||||
basic.keys
|
||||
|
||||
122
streaming_test.py
Normal file
122
streaming_test.py
Normal 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'])
|
||||
Reference in New Issue
Block a user