Skip to content

Commit 7def52f

Browse files
committed
add statuspage service for github rate limit
0 parents  commit 7def52f

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM python:3.6
2+
RUN pip install requests
3+
ADD statuspage.py statuspage.py
4+
CMD ["python", "statuspage.py"]

statuspage.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from datetime import datetime
2+
import json
3+
import os
4+
import sys
5+
import time
6+
7+
import requests
8+
9+
# the following 4 are the actual values that pertain to your account and this specific metric
10+
api_key = os.environ['STATUSPAGE_API_KEY']
11+
page_id = 'fzcq6v7wcg65'
12+
metric_id = 'rfcg9djxtg6n'
13+
api_base = 'api.statuspage.io'
14+
15+
github_id = os.environ['GITHUB_OAUTH_KEY']
16+
github_secret = os.environ['GITHUB_OAUTH_SECRET']
17+
18+
def get_rate_limit():
19+
r = requests.get('https://api.github.com/rate_limit',
20+
params={
21+
'client_id': github_id,
22+
'client_secret': github_secret,
23+
}
24+
)
25+
r.raise_for_status()
26+
resp = r.json()
27+
return resp['resources']['core']
28+
29+
def post_data(limit, remaining):
30+
percent = 100 * remaining / limit
31+
now = int(datetime.utcnow().timestamp())
32+
url = "https://api.statuspage.io/v1/pages/{page_id}/metrics/{metric_id}/data.json".format(
33+
page_id=page_id, metric_id=metric_id,
34+
)
35+
36+
r = requests.post(url,
37+
headers={
38+
"Content-Type": "application/x-www-form-urlencoded",
39+
"Authorization": "OAuth " + api_key,
40+
},
41+
data={
42+
'data[timestamp]': now,
43+
'data[value]': percent,
44+
}
45+
)
46+
r.raise_for_status()
47+
48+
while True:
49+
try:
50+
limit = get_rate_limit()
51+
print(json.dumps(limit))
52+
post_data(limit['limit'], limit['remaining'])
53+
except Exception as e:
54+
print("Error: %s" % e, file=sys.stderr)
55+
time.sleep(120)

0 commit comments

Comments
 (0)