Return the number of remaining hits tweepy


EDIT: I am trying the following code in order to read a list of ids and get their corresponant names. I am trying to use reamin_search_limits in order to avoid rate_limit errors.

limits = api.rate_limit_status() remain_search_limits = limits['resources']['search']['/search/tweets']['remaining'] stream = open('myfile','w') ss = open('userNames', 'w') for ids in content: try: limits = api.rate_limit_status() remain_search_limits = limits['resources']['search']['/search/tweets']['remaining'] print 'you have', remain_search_limits, 'API calls remaining until next hour' if remain_search_limits < 2: dtcode = datetime.utcnow() unixtime = calendar.timegm(dtcode.utctimetuple()) sleeptime = rate_limit_json.get('reset_time_in_seconds') - unixtime + 10 print 'waiting ', sleeptime, 'seconds' sleep(sleeptime) else: user = api.get_user(ids) stream.write(str(user.id)+"\n") ss.write(str(user.name)+"\n") except (tweepy.TweepError) as e: print e stream.close() ss.close()

Everytime remain_search_limits is printed it return 180 until to get tweepError exception.


This example shows you how to access how many tweets are remaining.

print rate_limit_json["resources"]["search"]['/search/tweets']['remaining'] 180 "resources" is the key you should be using to access the information inside.

If you want to update the value, put it in a loop reassigning the value after your time.sleep().

Put all the code inside a while loop: Something like this:

limits = api.rate_limit_status() remain_search_limits = limits['resources']['search']['/search/tweets']['remaining'] while remain_search_limits >2: limits = api.rate_limit_status() remain_search_limits = limits['resources']['search']['/search/tweets']['remaining'] else: dtcode = datetime.utcnow() unixtime = calendar.timegm(dtcode.utctimetuple()) sleeptime = rate_limit_json.get('reset_time_in_seconds') - unixtime + 10 print 'waiting ', sleeptime, 'seconds' sleep(sleeptime)

I have not tested the code but it should be close to what you need.

You may want to sleep between calls, I am unfamiliar with the api so not sure exactly what you are doing.


  • How do I move twitter configuration out of the controller? (Rails)
  • GQL query help - How can I write a query with where clause in GQL ? I am using google appengine data
  • how to fade the toggle so the transition looks softer
  • secure iPhone to server communication so that only my app can post data?
  • Android studio 3.1 rebuild project each time to see the changes
  • Query a table in different ways or orderings in Cassandra
  • Can I change font size based on the number of letters? [duplicate]
  • How to overcome the effect of navigation bar
  • use grep and awk to transfer data from .srt to .csv/xls
  • How to draw cross background via css which is responsive
  • ssh2_scp_send() using php corrupts pdf
  • html5 tag support
  • Allowing my app users to post tweets from a single account
  • How can I fill all space of a div 100% with 3 divs with the same size?
  • How to parsing NSDate to RFC 822 always use in English?
  • Where does the file get saved using “File file = new file(filename)” in Android
  • date: illegal option — d, Find difference between two dates
  • How to trick Node.js to load .js files as ES6 modules?
  • Unzip archive in .Net CORE 1.0
  • Streaming screenshots over WebRTC as a video stream from iOS
  • ActiveRecord query for a count of new users by day
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • How to create a file in java without a extension
  • Illegal mix of collations for operation for date/time comparison
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • align graphs with different xlab
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Return words with double consecutive letters
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?