import os
import time
import requests
from datetime import datetime
# Example camera URLs (replace with yours)
CAMERAS = {
"cam1": "http://user:pass@192.168.1.10/cgi-bin/snapshot.cgi",
"cam2": "http://user:pass@192.168.1.11/cgi-bin/snapshot.cgi",
"cam3": "http://user:pass@192.168.1.12/cgi-bin/snapshot.cgi",
"cam4": "http://user:pass@192.168.1.13/cgi-bin/snapshot.cgi",
}
SAVE_DIR = "snapshots"
os.makedirs(SAVE_DIR, exist_ok=True)
def capture_snapshot(name, url):
try:
now = datetime.now()
timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
date_folder = os.path.join(SAVE_DIR, now.strftime("%Y-%m-%d"))
os.makedirs(date_folder, exist_ok=True)
filename = os.path.join(date_folder, f"{name}_{timestamp}.jpg")
response = requests.get(url, timeout=10)
if response.status_code == 200:
with open(filename, "wb") as f:
f.write(response.content)
print(f"Saved snapshot: {filename}")
else:
print(f"Failed to get snapshot from {name}")
except Exception as e:
print(f"[{name}] Error: {e}")
while True:
print(f"Capturing snapshots at {datetime.now()}")
for name, url in CAMERAS.items():
capture_snapshot(name, url)
print("Sleeping for 1 hour...")
time. Sleep(3600)