(url, params=None, cnt=0)
| 122 | |
| 123 | |
| 124 | def github_client(url, params=None, cnt=0): |
| 125 | headers = { |
| 126 | "Authorization": "Bearer {}".format(Config.GITHUB_TOKEN), |
| 127 | "Accept": "application/vnd.github.v3+json" |
| 128 | } |
| 129 | time.sleep(2.5) |
| 130 | conn = http_req(url, params=params, headers=headers) |
| 131 | data = conn.json() |
| 132 | if conn.status_code != 200: |
| 133 | message = data.get("message", "Github 错误") |
| 134 | if cnt < 3: |
| 135 | cnt += 1 |
| 136 | if "You have triggered an abuse detection mechanism" in message \ |
| 137 | or "API rate limit exceeded for user ID" in message\ |
| 138 | or "You have exceeded a secondary rate limit" in message: |
| 139 | sleep_time = 20 + 15*cnt |
| 140 | logger.info("rate-limit retry {} {}, time sleep {}".format(cnt, params, sleep_time)) |
| 141 | time.sleep(sleep_time) |
| 142 | return github_client(url, params=params, cnt=cnt) |
| 143 | |
| 144 | raise Exception(message) |
| 145 | |
| 146 | return data |
| 147 | |
| 148 | |
| 149 | class GithubSearch(object): |
no test coverage detected