gentest.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:pyttanko 作者: Francesco149 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号