1
+ #!/usr/bin/env python3
2
+
1
3
from datetime import datetime
2
4
import json
3
5
import os
6
8
7
9
import requests
8
10
9
- # the following 4 are the actual values that pertain to your account and this specific metric
10
11
api_key = os .environ ['STATUSPAGE_API_KEY' ]
11
- page_id = 'fzcq6v7wcg65'
12
- metric_id = 'rfcg9djxtg6n'
12
+ page_id = os . environ [ 'STATUSPAGE_PAGE_ID' ]
13
+ metric_id = os . environ [ 'STATUSPAGE_METRIC_ID' ]
13
14
api_base = 'api.statuspage.io'
14
15
15
16
github_id = os .environ ['GITHUB_OAUTH_KEY' ]
16
17
github_secret = os .environ ['GITHUB_OAUTH_SECRET' ]
17
18
19
+
18
20
def get_rate_limit ():
21
+ """Retrieve the current GitHub rate limit for our auth tokens"""
19
22
r = requests .get ('https://api.github.com/rate_limit' ,
20
23
params = {
21
24
'client_id' : github_id ,
@@ -26,7 +29,9 @@ def get_rate_limit():
26
29
resp = r .json ()
27
30
return resp ['resources' ]['core' ]
28
31
29
- def post_data (limit , remaining ):
32
+
33
+ def post_data (limit , remaining , ** ignore ):
34
+ """Send the percent-remaining GitHub rate limit to statuspage"""
30
35
percent = 100 * remaining / limit
31
36
now = int (datetime .utcnow ().timestamp ())
32
37
url = "https://api.statuspage.io/v1/pages/{page_id}/metrics/{metric_id}/data.json" .format (
@@ -45,11 +50,17 @@ def post_data(limit, remaining):
45
50
)
46
51
r .raise_for_status ()
47
52
53
+
54
+ def get_and_post ():
55
+ data = get_rate_limit ()
56
+ print (json .dumps (data ))
57
+ post_data (limit = data ['limit' ], remaining = data ['remaining' ])
58
+
59
+
48
60
while True :
49
61
try :
50
- limit = get_rate_limit ()
51
- print (json .dumps (limit ))
52
- post_data (limit ['limit' ], limit ['remaining' ])
62
+ get_and_post ()
53
63
except Exception as e :
54
64
print ("Error: %s" % e , file = sys .stderr )
65
+ # post every two minutes
55
66
time .sleep (120 )
0 commit comments