forked from tpanum/siteeagle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (60 loc) · 2.45 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
import httpx
import argparse
import hashlib
import time
from difflib import unified_diff
from bs4 import BeautifulSoup
def get_hash(s):
return hashlib.sha256(s.encode()).hexdigest()
def get_content(site, selector):
resp = httpx.get(site)
out = resp.text
if selector is not None:
soup = BeautifulSoup(out, 'html.parser')
targets = soup.select(selector)
out = "\n".join([str(t) for t in targets])
checksum = get_hash(out)
content = (out, checksum)
return content
def main(diff, site=None, selector=None, frequency=None, ntfy_channel=None):
content = None
succesive_errors = 0
while True:
next_content = None
try:
next_content = get_content(site, selector)
except Exception as e:
payload = f"Siteeagle for '{site}' had an error."
if succesive_errors >= 3:
payload = f"[TERMINATING!] {payload}"
httpx.post(f"https://ntfy.sh/{ntfy_channel}",
data=payload)
if succesive_errors >= 3:
raise e
succesive_errors += 1
time.sleep(frequency)
continue
if content is not None:
if content[1] != next_content[1]:
if diff:
payload = "".join(l for l in unified_diff(content[0], next_content[0], fromfile=f"{site}_before", tofile=f"{site}_after"))
else:
payload = f"Site ({site}) change from '{content[0]}' to '{next_content[0]}'"
httpx.post(f"https://ntfy.sh/{ntfy_channel}", data=payload)
content = next_content
succesive_errors = 0
time.sleep(frequency)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--diff", action=argparse.BooleanOptionalAction,
help="send only diff instead of whole site if set")
parser.add_argument("-s", "--site", type=str,
help="location of site to monitor")
parser.add_argument("-z", "--selector", type=str,
help="selector to watch for the given site")
parser.add_argument("-f", "--frequency", type=int, default=30,
help="amount of seconds to wait until next retry")
parser.add_argument("-c", "--ntfy-channel", type=str,
help="the channel topic to use for ntfy.sh")
args = vars(parser.parse_args())
main(**args)