-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_alert.py
71 lines (56 loc) · 1.89 KB
/
flag_alert.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
import requests
from typing import Tuple
import time
import os
CBI_API = "https://api.community-boating.org/api/flag-history-recent"
WEBHOOK_URL = os.environ['WEBHOOK_URL']
FLAG_DICT = {
"R": ":large_red_square:",
"Y": ":large_yellow_square:",
"G": ":large_green_square:",
"C": ":flag_down:",
}
INTERVAL = 60 * int(os.environ['INTERVAL_MINS']) # seconds
def notifyHereTag(newFlag: str) -> str:
if newFlag == "Y" or newFlag == "R":
return "<!here>"
return ""
def parseResponse(response: dict) -> Tuple[str, str]:
"""
Case 1 (should never happen unless dock staff initiate flag change len(rows)
times in the span of INTERVAL seconds):
| x x x x x x x x x x x |
Case 2 (captures change):
x x | x x x x x x x x x |
Case 3 (most frequent):
x x x x x x x x x x x | |
"""
now = time.time()
rows = response['data']['rows']
for row in rows:
print(row[0], time.ctime(row[1]), time.ctime(now))
outsideIntervalIdx = 0
while (outsideIntervalIdx < len(rows) and
rows[outsideIntervalIdx][1] >= now - INTERVAL):
outsideIntervalIdx += 1
# Case 3
if outsideIntervalIdx == 0:
return rows[outsideIntervalIdx][0], ""
# Case 1
if outsideIntervalIdx == len(rows):
return rows[-1][0], rows[0][0]
# Case 2
return rows[outsideIntervalIdx][0], rows[0][0]
def main() -> str:
# Retrieve flag-change-history from CBI API
response = requests.get(CBI_API)
prevFlag, newFlag = parseResponse(response.json())
message = ""
# POST to webhook URL to send Slack message
if newFlag and prevFlag != newFlag:
message = (f"Flag just changed from {FLAG_DICT[prevFlag]} to " +
f"{FLAG_DICT[newFlag]}\t{notifyHereTag(newFlag)}")
response = requests.post(WEBHOOK_URL, json={"text": message})
return message
if __name__ == '__main__':
main()