def osu_get(conn, endpoint, paramsdict=None):
'''GETs /api/endpoint?paramsdict&k=args.key from conn.
return json object, exits process on api errors'''
global osu_treset, osu_ncalls, args
sys.stderr.write("%s %s\n" % (endpoint, str(paramsdict)))
paramsdict["k"] = args.key
path = "/api/%s?%s" % (endpoint, urllib.urlencode(paramsdict))
while True:
while True:
if time.time() >= osu_treset:
osu_ncalls = 0
osu_treset = time.time() + 60
sys.stderr.write("\napi ready\n")
if osu_ncalls < 60:
break
else:
sys.stderr.write("waiting for api cooldown...\r")
time.sleep(1)
try:
conn.request("GET", path)
osu_ncalls += 1
r = conn.getresponse()
raw = ""
while True:
try:
raw += r.read()
break
except httplib.IncompleteRead as e:
raw += e.partial
j = json.loads(raw)
if "error" in j:
sys.stderr.write("%s\n" % j["error"])
sys.exit(1)
return j
except (httplib.HTTPException, ValueError) as e:
sys.stderr.write("%s\n" % (traceback.format_exc()))
try:
'''
prevents exceptions on next request if the
response wasn't previously read due to errors
'''
conn.getresponse().read()
except httplib.HTTPException:
pass
time.sleep(5)
评论列表
文章目录