-
Notifications
You must be signed in to change notification settings - Fork 2
/
argos.py
65 lines (53 loc) · 2.23 KB
/
argos.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
import logging
import requests
import pickle
import telegram
from targets import targets
from secrets import TOKEN, CHAT_IDS
from asyncio import run
from datetime import datetime
from bs4 import BeautifulSoup
BOT = telegram.Bot(TOKEN)
WORKDIR = "/data/"
logging.basicConfig(
format="%(asctime)s [%(levelname)s]: %(message)s",
level=logging.INFO,
filename=WORKDIR + "argos.log"
)
async def main():
for target in targets:
if target["method"] == "GET":
r = requests.get(target["url"], headers=target["headers"])
elif target["method"] == "POST":
r = requests.post(target["url"], json=target["data"], headers=target["headers"])
else:
raise ValueError(f"Unknown method: {target['method']}")
if r.status_code >= 300:
raise ConnectionError(f"Got a non-OK status code: {r.status_code}")
if target["html-tag"]:
result = [item.text for item in BeautifulSoup(r.content, "html.parser").find_all(target["html-tag"])]
elif target["html-class"]:
result = [item.text for item in BeautifulSoup(r.content, "html.parser").find_all(class_=target["html-class"])]
elif target["html-id"]:
result = [item.text for item in BeautifulSoup(r.content, "html.parser").find_all(id=target["html-id"])]
else:
result = r.content
# If the file does not exist yet, we're not going to bother
# creating it because that will happen anyways
prev_result = ""
try:
with open(WORKDIR + target["id"], 'rb') as rd:
prev_result = pickle.load(rd)
except:
pass
if prev_result != result:
# Save the new result
with open(WORKDIR + target["id"], 'wb') as wr:
pickle.dump(result, wr)
# Save the result with a timestamp as a snapshot
with open(WORKDIR + target["id"] + datetime.now().strftime("-%Y%m%d-%H%M"), 'wb') as wr:
pickle.dump(result, wr)
for chat_id in CHAT_IDS:
await BOT.send_message(text=f"Something changed on {target['id']}: {target['url']}", chat_id=chat_id)
if __name__ == '__main__':
run(main())