Last active
September 5, 2019 07:43
-
-
Save unicolet/80d08b8babaf5b546b1e37ae9dcece38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -u | |
import os | |
import sys | |
from datetime import datetime | |
import urllib2 | |
import time | |
def get_gh_ratelimits(token): | |
request = urllib2.Request( | |
url='https://api.github.com/rate_limit', | |
headers={"Authorization": "token %s" % token}) | |
response = urllib2.urlopen(request) | |
return ( | |
int(response.info().getheader('X-RateLimit-Reset')), | |
int(response.info().getheader('X-RateLimit-Remaining')) | |
) | |
while True: | |
reset, remaining = get_gh_ratelimits(sys.argv[1]) | |
if remaining < int(os.environ.get("GH_REMAINING_THRESHOLD", "4990")): | |
print "Only %d calls left, sleeping until %s (UTC) or %s (localtime) in 30s steps" % ( | |
remaining, | |
datetime.utcfromtimestamp(reset), | |
datetime.fromtimestamp(reset) | |
) | |
now = int(datetime.utcnow().strftime("%s")) | |
while now < reset: | |
now = int(datetime.utcnow().strftime("%s")) | |
sys.stdout.write(".") | |
time.sleep(30) | |
break | |
else: | |
print "%d calls left, continuing" % remaining | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment