Skip to content

Instantly share code, notes, and snippets.

@unicolet
Last active September 5, 2019 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unicolet/80d08b8babaf5b546b1e37ae9dcece38 to your computer and use it in GitHub Desktop.
Save unicolet/80d08b8babaf5b546b1e37ae9dcece38 to your computer and use it in GitHub Desktop.
#!/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