]> jfr.im git - z_archive/twitter.git/commitdiff
By golly the logger works.
authorMike Verdone <redacted>
Sat, 12 Mar 2011 18:03:18 +0000 (19:03 +0100)
committerMike Verdone <redacted>
Sat, 12 Mar 2011 18:03:18 +0000 (19:03 +0100)
twitter/logger.py

index 7ff61f413656b7e0a9ea865a5b801c94d27df7a1..331b921839762227f08389ef836d3f6440bb6f33 100644 (file)
@@ -21,32 +21,64 @@ DESCRIPTION:
 
 import sys
 import os
+from time import sleep
+
 
 OPTIONS = {
     'oauth_filename': os.environ.get('HOME', '') + os.sep + '.twitter_oauth',
 }
 
-from api import Twitter
+from api import Twitter, TwitterError
 from cmdline import CONSUMER_KEY, CONSUMER_SECRET
 from oauth import read_token_file, OAuth
 
-def main(args=sys.argv[1:]):
-    oauth_filename = OPTIONS['oauth_filename']
-    oauth_token, oauth_token_secret = read_token_file(oauth_filename)
-
-    twitter = Twitter(
-        auth=OAuth(
-            oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
-        api_version='1',
-        domain='api.twitter.com')
+def log_debug(msg):
+    print >> sys.stderr, msg
 
+def get_tweets(twitter, max_id=None):
     kwargs = dict(count=3200)
+    if max_id:
+        kwargs['max_id'] = max_id
 
+    n_tweets = 0
     tweets = twitter.statuses.user_timeline(**kwargs)
     for tweet in tweets:
+        if tweet['id'] == max_id:
+            continue
         print "%s %s %s" % (tweet['user']['screen_name'],
                             tweet['id'],
                             tweet['created_at'])
         for line in tweet['text'].splitlines():
             print '    ' + line.encode('utf-8')
         print
+        max_id = tweet['id']
+        n_tweets += 1
+    return n_tweets, max_id
+
+def main(args=sys.argv[1:]):
+    oauth_filename = OPTIONS['oauth_filename']
+    oauth_token, oauth_token_secret = read_token_file(oauth_filename)
+
+    twitter = Twitter(
+        auth=OAuth(
+            oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
+        api_version='1',
+        domain='api.twitter.com')
+
+    if args:
+        max_id = args[0]
+    else:
+        max_id = None
+
+    n_tweets = 0
+    while True:
+        try:
+            tweets_processed, max_id = get_tweets(twitter, max_id)
+            n_tweets += tweets_processed
+            log_debug("Processed %i tweets (max_id %s)" %(n_tweets, max_id))
+            if tweets_processed == 0:
+                log_debug("That's it, we got all the tweets. Done.")
+                break
+        except TwitterError, e:
+            log_debug("Twitter bailed out. I'm going to sleep a bit then try again")
+            sleep(3)