import re
import subprocess
import sys
import time
REPEAT_SECONDS = 300 # check every 5 minutes
if len(sys.argv) == 4:
url = sys.argv[1]
regexp = sys.argv[2]
email_address = sys.argv[3]
else:
print(f"Usage: {sys.argv[0]} <url> <regex> <email-address>", file=sys.stderr)
sys.exit(1)
while True:
p = subprocess.run(
["curl", "--silent", url], text=True, capture_output=True
)
webpage = p.stdout
if not re.search(regexp, webpage):
time.sleep(REPEAT_SECONDS)
continue
mail_body = f"Generated by {sys.argv[0]}"
subject = f"website '{url}' now matches regex '{regexp}'"
# the echo is for testing, remove to really send email
subprocess.run(["echo", "mail", "-s", subject], text=True, input=mail_body)
sys.exit(0)
Python implementation of /bin/echo
using indexing & while, not pythonesque