X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/5f3ddd05871335c5d2e000af9241f46821419810..ac98c9826c24f6eae0a1d0b5b59db11759a3652c:/twitter/logger.py diff --git a/twitter/logger.py b/twitter/logger.py index 7cff697..e18d7a0 100644 --- a/twitter/logger.py +++ b/twitter/logger.py @@ -28,9 +28,17 @@ 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 .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 + +# 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(msg, file=sys.stderr) @@ -52,7 +60,7 @@ def get_tweets(twitter, screen_name, max_id=None): print("In-Reply-To: %s" % tweet['in_reply_to_status_id']) print() for line in tweet['text'].splitlines(): - print(' ' + line.encode('utf-8')) + printNicely(' ' + line + '\n') print() print() max_id = tweet['id'] @@ -60,15 +68,22 @@ def get_tweets(twitter, screen_name, max_id=None): return n_tweets, max_id def main(args=sys.argv[1:]): - twitter = Twitter( - auth=NoAuth(), - api_version='1', - domain='api.twitter.com') - 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), + domain='api.twitter.com') + screen_name = args[0] if args[1:]: @@ -83,9 +98,9 @@ def main(args=sys.argv[1:]): 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)