X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/1b31d642c54e09dc8f8a1ca49c934d6990f2ab33..3756d82e3400879e3ac632d535bdb6d112faecff:/twitter/cmdline.py?ds=sidebyside diff --git a/twitter/cmdline.py b/twitter/cmdline.py index 980b754..fcb5892 100644 --- a/twitter/cmdline.py +++ b/twitter/cmdline.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python +# encoding: utf-8 """ USAGE: @@ -90,10 +92,10 @@ OPTIONS = { def parse_args(args, options): long_opts = ['help', 'format=', 'refresh', 'oauth=', - 'refresh-rate=', 'config=', 'length=', 'timestamp', + 'refresh-rate=', 'config=', 'length=', 'timestamp', 'datestamp', 'no-ssl'] short_opts = "e:p:f:h?rR:c:l:td" - opts, extra_args = getopt(args, short_opts, long_opts) + opts, extra_args = getopt(args, short_opts, long_opts) for opt, arg in opts: if opt in ('-f', '--format'): @@ -137,7 +139,7 @@ def get_time_string(status, options, format="%a %b %d %H:%M:%S +0000 %Y"): return time.strftime("%H:%M:%S ", t) elif datestamp: return time.strftime("%Y-%m-%d ", t) - return "" + return "" class StatusFormatter(object): def __call__(self, status, options): @@ -311,7 +313,7 @@ class NoSuchAction(Action): def __call__(self, twitter, options): raise NoSuchActionError("No such action: %s" %(options['action'])) -def printNicely(string): +def printNicely(string): if sys.stdout.encoding: print string.encode(sys.stdout.encoding, 'replace') else: @@ -331,15 +333,14 @@ class SearchAction(Action): # We need to be pointing at search.twitter.com to work, and it is less # tangly to do it here than in the main() twitter.domain="search.twitter.com" - twitter.uri="" + twitter.uriparts=() # We need to bypass the TwitterCall parameter encoding, so we # don't encode the plus sign, so we have to encode it ourselves query_string = "+".join( [quote(term.decode(get_term_encoding())) for term in options['extra_args']]) - twitter.encoded_args = "q=%s" %(query_string) - results = twitter.search()['results'] + results = twitter.search(q=query_string)['results'] f = get_formatter('search', options) for result in results: resultStr = f(result, options) @@ -447,15 +448,6 @@ class DoNothingAction(Action): def __call__(self, twitter, options): pass -def parse_oauth_tokens(result): - for r in result.split('&'): - k, v = r.split('=') - if k == 'oauth_token': - oauth_token = v - elif k == 'oauth_token_secret': - oauth_token_secret = v - return oauth_token, oauth_token_secret - actions = { 'authorize' : DoNothingAction, 'follow' : FollowAction, @@ -488,8 +480,9 @@ def main(args=sys.argv[1:]): print >> sys.stderr raise SystemExit(1) - config_options = loadConfig( + config_path = os.path.expanduser( arg_options.get('config_filename') or OPTIONS.get('config_filename')) + config_options = loadConfig(config_path) # Apply the various options in order, the most important applied last. # Defaults first, then what's read from config file, then command-line @@ -505,19 +498,22 @@ def main(args=sys.argv[1:]): print >> sys.stderr, "Use 'twitter -h' for help." return 1 + oauth_filename = os.path.expanduser(options['oauth_filename']) + if (options['action'] == 'authorize' - or not os.path.exists(options['oauth_filename'])): + or not os.path.exists(oauth_filename)): oauth_dance( "the Command-Line Tool", CONSUMER_KEY, CONSUMER_SECRET, options['oauth_filename']) - oauth_token, oauth_token_secret = read_token_file(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), secure=options['secure'], - api_version='1') + api_version='1', + domain='api.twitter.com') try: Action()(twitter, options) @@ -525,6 +521,6 @@ def main(args=sys.argv[1:]): print >>sys.stderr, e raise SystemExit(1) except TwitterError, e: - print >> sys.stderr, e.args[0] + print >> sys.stderr, str(e) print >> sys.stderr, "Use 'twitter -h' for help." raise SystemExit(1)