script/stream/recorder_tweetstream.py
changeset 248 ffb0a6d08000
parent 207 621fa6caec0c
child 242 cdd7d3c0549c
--- a/script/stream/recorder_tweetstream.py	Wed Jul 27 12:24:43 2011 +0200
+++ b/script/stream/recorder_tweetstream.py	Wed Jul 27 12:25:45 2011 +0200
@@ -8,8 +8,9 @@
 import os
 import socket
 import sys
+import time
 import tweetstream
-import tweetstream.auth
+import tweepy.auth
 socket._fileobject.default_bufsize = 0
 
 
@@ -21,7 +22,7 @@
 #just put it in a sqlite3 tqble
 
 
-class ReconnectingTweetStream(tweetstream.TrackStream):
+class ReconnectingTweetStream(tweetstream.FilterStream):
     """TweetStream class that automatically tries to reconnect if the
     connecting goes down. Reconnecting, and waiting for reconnecting, is
     blocking.
@@ -43,12 +44,12 @@
 
     """
 
-    def __init__(self, auth, keywords, url="track", reconnects=3, error_cb=None, retry_wait=5, **kwargs):
+    def __init__(self, auth, keywords, reconnects=3, error_cb=None, retry_wait=5, **kwargs):
         self.max_reconnects = reconnects
         self.retry_wait = retry_wait
         self._reconnects = 0
         self._error_cb = error_cb
-        super(ReconnectingTweetStream, self).__init__(auth, keywords, url, **kwargs)
+        super(ReconnectingTweetStream, self).__init__(auth=auth, track=keywords, **kwargs)
 
     def next(self):
         while True:
@@ -89,11 +90,11 @@
     track_list = [k for k in track_list.split(',')]
     
     if username and password:
-        auth = tweetstream.auth.BasicAuthHandler(username, password)        
+        auth = tweepy.auth.BasicAuthHandler(username, password)        
     else:
         consumer_key = models.CONSUMER_KEY
         consumer_secret = models.CONSUMER_SECRET
-        auth = tweetstream.auth.OAuthHandler(consumer_key, consumer_secret, secure=False)
+        auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret, secure=False)
         auth.set_access_token(*(utils.get_oauth_token(token_filename)))
     
     if duration >= 0:
@@ -106,6 +107,7 @@
                 print "Stop recording after %d seconds." % (duration)
                 break
             process_tweet(tweet, session, debug, token_filename)
+            logging.info("Tweet count: %d - current rate : %.2f - running : %s"%(stream.count, stream.rate, int(time.time()-stream.starttime)))
             session.commit()
     finally:
         stream.close()
@@ -149,7 +151,7 @@
     if options.new and os.path.exists(options.filename):
         os.remove(options.filename)
     
-    engine, metadata = models.setup_database('sqlite:///' + options.filename, echo=(options.debug))
+    engine, metadata = models.setup_database('sqlite:///' + options.filename, echo=(options.debug>=2))
     Session = sessionmaker(bind=engine)
     session = Session()