]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/cmdline.py
- Check rate limit using the command line tool. Patch by @stalkr_
[z_archive/twitter.git] / twitter / cmdline.py
index 34e3b58ee61221e8bb4fc03508f78d9cadc77f15..5ed95109d14da94477ad1c4a1ee0bc1b8269793c 100644 (file)
@@ -22,6 +22,7 @@ ACTIONS:
  search         search twitter (Beware: octothorpe, escape it)
  set            set your twitter status
  shell          login to the twitter shell
+ rate           get your current rate limit status (remaining API reqs)
 
 
 OPTIONS:
@@ -87,7 +88,7 @@ from .api import Twitter, TwitterError
 from .oauth import OAuth, write_token_file, read_token_file
 from .oauth_dance import oauth_dance
 from . import ansi
-from .util import smrt_input
+from .util import smrt_input, printNicely
 
 OPTIONS = {
     'action': 'friends',
@@ -360,13 +361,6 @@ class NoSuchAction(Action):
     def __call__(self, twitter, options):
         raise NoSuchActionError("No such action: %s" %(options['action']))
 
-def printNicely(string):
-    if hasattr(sys.stdout, 'buffer'):
-        sys.stdout.buffer.write(string.encode('utf8'))
-        print()
-    else:
-        print(string.encode('utf8'))
-
 class StatusAction(Action):
     def __call__(self, twitter, options):
         statuses = self.getStatuses(twitter, options)
@@ -530,6 +524,13 @@ class DoNothingAction(Action):
     def __call__(self, twitter, options):
         pass
 
+class RateLimitStatus(Action):
+    def __call__(self, twitter, options):
+        rate = twitter.account.rate_limit_status()
+        print("Remaining API requests: %s / %s (hourly limit)" % (rate['remaining_hits'], rate['hourly_limit']))
+        print("Next reset in %ss (%s)" % (int(rate['reset_time_in_seconds']-time.time()),
+                                          time.asctime(time.localtime(rate['reset_time_in_seconds']))))
+
 actions = {
     'authorize' : DoNothingAction,
     'follow'    : FollowAction,
@@ -544,6 +545,7 @@ actions = {
     'search'    : SearchAction,
     'set'       : SetStatusAction,
     'shell'     : TwitterShell,
+    'rate'      : RateLimitStatus,
 }
 
 def loadConfig(filename):