-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.py
73 lines (57 loc) · 1.98 KB
/
alerts.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
import requests
from datetime import date, timedelta
from retrying import retry
from helpers import get_confirmed, format_time, get_cur_time
ALERTS = []
URL = 'https://api.github.com/repos/silverdrake11/covid_rates_per_capita/issues/6/comments'
CREDENTIALS_FILENAME = 'credentials.txt'
def alert(*stuff):
text = ','.join(map(str, stuff))
print(text)
ALERTS.append(text)
def get_credentials():
with open(CREDENTIALS_FILENAME) as fh:
return fh.read().split(':')[1].strip('/')
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=3)
def get_comments():
date_str = (date.today() - timedelta(1)).isoformat()
url = URL + '?since={}'.format(date_str)
response = requests.get(url)
response.raise_for_status()
return response.json()
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=3)
def post_comment(credentials, comment):
response = requests.post(URL,
headers={'Authorization': 'token ' + credentials},
json={'body':comment})
response.raise_for_status()
def post_alerts():
old_alerts = []
comments = get_comments()
for comment in comments:
old_alerts.extend(comment['body'].splitlines())
old_alerts = set(old_alerts)
to_post = []
for alert in set(ALERTS):
if alert not in old_alerts:
to_post.append(alert)
credentials = get_credentials()
if to_post:
post_comment(credentials, '\n'.join(to_post))
if __name__ == '__main__':
prev_label = None
credentials = get_credentials()
while True:
try:
label = get_confirmed()
if label == prev_label:
try:
post_comment(credentials, 'SITE DOWN!')
except:
traceback.print_exc()
prev_label = label
except:
traceback.print_exc()
print(format_time(get_cur_time()))
time.sleep(7200)