-
Notifications
You must be signed in to change notification settings - Fork 26
/
client.py
52 lines (36 loc) · 1.23 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from datetime import datetime
import os
import time
from github3 import login
CREDENTIALS_FILE = ".credentials"
def get_token():
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GITHUB_PAT")
if not token:
raise KeyError(
"""ERROR - GitHub token must be provided via environment"""
""" variable "GITHUB_TOKEN" or "GITHUB_PAT"."""
""" Please delete any old ".credentials" file."""
)
return token
# get_token()
def get_github3_client():
token = get_token()
gh = login(token=token)
return gh
def sleep_if_rate_limited(gh, verbose=False):
rates = gh.rate_limit()
if not rates["resources"]["search"]["remaining"]:
reset_epoch = rates["resources"]["search"]["reset"]
reset_dt, now = datetime.utcfromtimestamp(reset_epoch), datetime.utcnow()
if reset_dt > now:
sleep_secs = (reset_dt - now).seconds + 1
if verbose:
print(
(
"sleeping for",
sleep_secs,
"got rate limit",
rates["resources"]["search"],
)
)
time.sleep(sleep_secs)