]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/follow.py
Let's use the stdlib io system for great sanity.
[z_archive/twitter.git] / twitter / follow.py
index abc5fdabfe7a1b931e55202439f4ecdfc34c212c..75297cb3262e96a52b0b00b4594e2d0464bc19ae 100644 (file)
@@ -9,6 +9,7 @@ OPTIONS
  -r --followers        display followers of the given user (default)
  -g --following        display users the given user is following
  -a --api-rate         see your current API rate limit status
+ -i --ids              prepend user id to each line. useful to tracking renames
 
 AUTHENTICATION
     Authenticate to Twitter using OAuth to see following/followers of private
@@ -18,9 +19,16 @@ AUTHENTICATION
 
 from __future__ import print_function
 
-import os, sys, time, calendar, urllib2, httplib
+import os, sys, time, calendar
 from getopt import gnu_getopt as getopt, GetoptError
 
+try:
+    import urllib.request as urllib2
+    import http.client as httplib
+except ImportError:
+    import urllib2
+    import httplib
+
 # T-Follow (Twitter-Follow) application registered by @stalkr_
 CONSUMER_KEY='USRZQfvFFjB6UvZIN2Edww'
 CONSUMER_SECRET='AwGAaSzZa5r0TDL8RKCDtffnI9H9mooZUdOa95nw8'
@@ -31,10 +39,11 @@ from .oauth_dance import oauth_dance
 from .auth import NoAuth
 from .util import Fail, err
 
+
 def parse_args(args, options):
     """Parse arguments from command-line to set options."""
-    long_opts = ['help', 'oauth', 'followers', 'following', 'api-rate']
-    short_opts = "horga"
+    long_opts = ['help', 'oauth', 'followers', 'following', 'api-rate', 'ids']
+    short_opts = "horgai"
     opts, extra_args = getopt(args, short_opts, long_opts)
 
     for opt, arg in opts:
@@ -49,6 +58,8 @@ def parse_args(args, options):
             options['followers'] = False
         elif opt in ('-a', '--api-rate'):
             options['api-rate' ] = True
+        elif opt in ('-i', '--ids'):
+            options['show_id'] = True
 
     options['extra_args'] = extra_args
 
@@ -180,7 +191,8 @@ def main(args=sys.argv[1:]):
     options = {
         'oauth': False,
         'followers': True,
-        'api-rate': False
+        'api-rate': False,
+        'show_id': False
     }
     try:
         parse_args(args, options)
@@ -206,7 +218,7 @@ def main(args=sys.argv[1:]):
     else:
         auth = NoAuth()
 
-    twitter = Twitter(auth=auth, api_version='1', domain='api.twitter.com')
+    twitter = Twitter(auth=auth, api_version='1.1', domain='api.twitter.com')
 
     if options['api-rate']:
         rate_limit_status(twitter)
@@ -224,7 +236,17 @@ def main(args=sys.argv[1:]):
             raise SystemExit(1)
 
         for uid in user_ids:
-            print(users[uid].encode("utf-8"))
+            if options['show_id']:
+              try:
+                print(str(uid) + "\t" + users[uid].encode("utf-8"))
+              except KeyError:
+                pass
+            
+            else:
+              try:
+                print(users[uid].encode("utf-8"))
+              except KeyError:
+                pass
 
         # print total on stderr to separate from user list on stdout
         if options['followers']: