]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/cmdline.py
- Attempt to determine terminal encoding when tweeting.
[z_archive/twitter.git] / twitter / cmdline.py
index 21c26c9e7b22b558300f6c7fc049b5b4676b0fe4..df3b90cfd6c1083e2b7d8f253e8bb23cd436512f 100644 (file)
@@ -211,6 +211,17 @@ class AnsiSearchFormatter(object):
             ansi.cmdColour(colour), result['from_user'],
             ansi.cmdReset(), result['text']))
 
+_term_encoding = None
+def get_term_encoding():
+    global _term_encoding
+    if not _term_encoding:
+        lang = os.getenv('LANG', 'unknown.UTF-8').split('.')
+        if lang[1:]:
+            _term_encoding = lang[1]
+        else:
+            _term_encoding = 'UTF-8'
+    return _term_encoding
+
 formatters = {}
 status_formatters = {
     'default': StatusFormatter,
@@ -319,9 +330,12 @@ 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=""
         # 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) for term in options['extra_args']])
+        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']
@@ -371,7 +385,7 @@ class LeaveAction(AdminAction):
 
 class SetStatusAction(Action):
     def __call__(self, twitter, options):
-        statusTxt = (u" ".join(options['extra_args'])
+        statusTxt = (" ".join(options['extra_args']).decode(get_term_encoding())
                      if options['extra_args']
                      else unicode(raw_input("message: ")))
         status = (statusTxt.encode('utf8', 'replace'))
@@ -538,7 +552,8 @@ def main(args=sys.argv[1:]):
     twitter = Twitter(
         auth=OAuth(
             oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
-        secure=options['secure'])
+        secure=options['secure'],
+        api_version='1')
 
     try:
         Action()(twitter, options)