-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_monitor.py
More file actions
63 lines (48 loc) · 2.12 KB
/
Copy pathweb_monitor.py
File metadata and controls
63 lines (48 loc) · 2.12 KB
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
import requests
import hashlib
import time
from datetime import datetime
# The URL we are surveilling
# For testing, we'll use a site that shows the current time (so it changes every minute)
TARGET_URL = "https://www.google.com"
LOG_FILE = "web_changes.log"
def get_page_hash(url):
"""Fetches the page and returns a unique 'fingerprint' of its content."""
try:
response = requests.get(url, timeout=10)
# We use SHA-256 to create a 64-character 'fingerprint' of the text
return hashlib.sha256(response.text.encode('utf-8')).hexdigest()
except Exception as e:
print(f"[!] Error fetching {url}: {e}")
return None
def log_change(url):
"""Logs when a change is detected on the target website."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(LOG_FILE, "a") as f:
f.write(f"[{timestamp}] WEB ALERT: Change detected on {url}\n")
def monitor_website():
print(f"--- Web Content Surveillance Started ---")
print(f"--- Monitoring: {TARGET_URL} ---")
# Get the initial 'fingerprint' of the page
current_hash = get_page_hash(TARGET_URL)
if not current_hash:
print("Failed to get initial fingerprint. Check your connection.")
return
print(f"Initial Fingerprint: {current_hash[:10]}...")
try:
while True:
time.sleep(30) # Check every 30 seconds
new_hash = get_page_hash(TARGET_URL)
if new_hash and new_hash != current_hash:
print(f"\n[!!!] CHANGE DETECTED on {TARGET_URL}")
print(f" Old Fingerprint: {current_hash[:10]}...")
print(f" New Fingerprint: {new_hash[:10]}...")
log_change(TARGET_URL)
# Update the fingerprint so we can catch the NEXT change
current_hash = new_hash
else:
print(f"[{datetime.now().strftime('%H:%M:%S')}] No changes detected.")
except KeyboardInterrupt:
print("\nWeb monitoring stopped.")
if __name__ == "__main__":
monitor_website()