banool / recreation-gov-campsite-checker

Scrapes the recreation.gov website to check for campsite availabilities 🏕🏕
Apache License 2.0
324 stars 166 forks source link

Add email notifier #34

Open mlgold opened 3 years ago

mlgold commented 3 years ago

This is a simple email notifier using swaks. It requires an account and smtp server that can be used to send emails.

I believe if you want to use gmail to send emails this auth method will not work, so I left the json import at the top of the notifier for future use with a more secure authorization.

The example below sends to both an email and sms. Android Gmail now only appears to allow a different notification sound based in sms messages. The notification sound based on email labels no longer seems to be available.

It is quick and dirty. There is no checking if swaks succeded.


import json
import random
import sys
import time
import os

from hashlib import md5

from camping import SUCCESS_EMOJI

from datetime import datetime

MESSAGE_FILE = "message.txt"

available_site_strings = []
for line in sys.stdin:
    line = line.strip()
    if SUCCESS_EMOJI in line:
        name = " ".join(line.split(":")[0].split(" ")[1:])
        available = line.split(":")[1][1].split(" ")[0]
        s = "{} site(s) available in {}".format(available, name)
        available_site_strings.append(s)

if available_site_strings:
    message = " 🏕🏕🏕\n"
    message += "\n".join(available_site_strings)
    message += "\n" + "🏕" * random.randint(5, 20)  # To avoid duplicate messages.

    with open(MESSAGE_FILE, "w") as f:
        f.write(message)

    os.system('swaks --body "message.txt" --to foo@bar.com,2125551234@vtext.com --from "notifier@foo.com" --server smtp.foo.com --auth LOGIN --auth-user "notifier@foo.com" --auth-password "foopassword" -tls --header "Subject: Campsites Available"')
    sys.exit(0)
else:
    now = datetime.now()
    print(now , "No campsites available, not messaging 😞")
    sys.exit(1)
BillXMA commented 3 years ago

Goo idea. I use a similar gmail-based notifier and it works like a charm for email and sms

banool commented 3 years ago

I would accept a PR to add this as a notifier option. Ideally though we would overhaul the notifier setup first, such as to add some common framework.

mlgold commented 3 years ago

I agree, an overhaul of the notifier setup would be a good idea. Since this issue was raised, I added playing a loud sound on the computer it is running on, which is in my family room. The notifications take seconds to a minute or two to arrive and in that time the campsite is gone. The sound plays immediately. I also have a cron job that sets the path and tries two more times to run camping.py if it fails. So, ideally, a cron script, an updated camping.py and a more generic notifier would be good.