]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/logger.py
Merge pull request #214 from edi-bice/master
[z_archive/twitter.git] / twitter / logger.py
index 331b921839762227f08389ef836d3f6440bb6f33..e18d7a0630a88987fa1e620d4734426b8ca012a4 100644 (file)
@@ -1,42 +1,50 @@
 """
-tw-archiver - Twitter Archiver
+twitter-log - Twitter Logger/Archiver
 
 USAGE:
 
-    tw-archiver [OPTIONS]
+    twitter-log <screen_name> [max_id]
 
 DESCRIPTION:
 
     Produce a complete archive in text form of a user's tweets. The
     archive format is:
 
-        screen_name tweet_id tweet_time
+        screen_name <tweet_id>
+        Date: <tweet_time>
+        [In-Reply-To: a_tweet_id]
 
-            Tweet text spanning multiple lines with
+            Tweet text possibly spanning multiple lines with
             each line indented by four spaces.
 
-    Each tweet is separated by a blank line.
+
+    Each tweet is separated by two blank lines.
 
 """
 
+from __future__ import print_function
+
 import sys
 import os
 from time import sleep
 
+from .api import Twitter, TwitterError
+from .cmdline import CONSUMER_KEY, CONSUMER_SECRET
+from .auth import NoAuth
+from .oauth import OAuth, write_token_file, read_token_file
+from .oauth_dance import oauth_dance
+from .util import printNicely
 
-OPTIONS = {
-    'oauth_filename': os.environ.get('HOME', '') + os.sep + '.twitter_oauth',
-}
-
-from api import Twitter, TwitterError
-from cmdline import CONSUMER_KEY, CONSUMER_SECRET
-from oauth import read_token_file, OAuth
+# Registered by @sixohsix
+CONSUMER_KEY = "OifqLIQIufeY9znQCkbvg"
+CONSUMER_SECRET = "IedFvi0JitR9yaYw9HwcCCEy4KYaLxf4p4rHRqGgX80"
+OAUTH_FILENAME = os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.twitter_log_oauth'
 
 def log_debug(msg):
-    print >> sys.stderr, msg
+    print(msg, file=sys.stderr)
 
-def get_tweets(twitter, max_id=None):
-    kwargs = dict(count=3200)
+def get_tweets(twitter, screen_name, max_id=None):
+    kwargs = dict(count=3200, screen_name=screen_name)
     if max_id:
         kwargs['max_id'] = max_id
 
@@ -45,40 +53,55 @@ def get_tweets(twitter, max_id=None):
     for tweet in tweets:
         if tweet['id'] == max_id:
             continue
-        print "%s %s %s" % (tweet['user']['screen_name'],
-                            tweet['id'],
-                            tweet['created_at'])
+        print("%s %s\nDate: %s" % (tweet['user']['screen_name'],
+                                   tweet['id'],
+                                   tweet['created_at']))
+        if tweet.get('in_reply_to_status_id'):
+            print("In-Reply-To: %s" % tweet['in_reply_to_status_id'])
+        print()
         for line in tweet['text'].splitlines():
-            print '    ' + line.encode('utf-8')
-        print
+            printNicely('    ' + line + '\n')
+        print()
+        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)
+    if not args:
+        print(__doc__)
+        return 1
+
+    if not os.path.exists(OAUTH_FILENAME):
+        oauth_dance(
+            "the Python Twitter Logger", CONSUMER_KEY, CONSUMER_SECRET,
+            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]
+    screen_name = args[0]
+
+    if args[1:]:
+        max_id = args[1]
     else:
         max_id = None
 
     n_tweets = 0
     while True:
         try:
-            tweets_processed, max_id = get_tweets(twitter, max_id)
+            tweets_processed, max_id = get_tweets(twitter, screen_name, 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.")
+                log_debug("That's it, we got all the tweets we could. Done.")
                 break
-        except TwitterError, e:
+        except TwitterError as e:
             log_debug("Twitter bailed out. I'm going to sleep a bit then try again")
             sleep(3)
+
+    return 0