banool/recreation-gov-campsite-checker

running in cron and notifications

Closed this issue ยท 7 comments

I've got this running well on my Debian system, using this command:
python3.7 camping.py --start-date 2021-08-01 --end-date 2021-08-13 --nights 1 --stdin < parks-sierras.txt

I'm able to get it to run in cron (have to use the full paths in the cron command) but cannot get it to notify. I'd like to receive a system notification and bell, as well as an email. Any ideas?

Another option would be for it to run as a script in terminal. Actually might prefer that so I can monitor the output. I've read the .sh script from the original 'yosemite-camping' github site but not sure how to ge it to run this version..

cc. @ccritter

I'd like to receive a system notification and bell, as well as an email. Any ideas?

That would be on you to write -- If you have a custom email solution it might be a bit trickier, but you could always do something with the Gmail API or another email service API.

https://developers.google.com/gmail/api/guides/sending

As for a system notification, you could take a look at "notify-send" in subprocess or "notify2":

https://askubuntu.com/questions/108764/how-do-i-send-text-messages-to-the-notification-bubbles
https://askubuntu.com/questions/616985/how-do-i-send-desktop-notifications-using-python-3

A simple desktop notification mockup you could try, based on the twitter notifier in this repo. This assumes that you are running the cron script on the same system as the one you want the notification to appear. I didn't add a system beep, but that wouldn't be too hard to do with subprocess as well. WARNING: I have not tested this code at all, since I'm not on my linux box at the moment, but I believe it should work...

Much like the example on the documentation, this would just be piped from the output of your command, for example:
python3.7 camping.py --start-date 2021-08-01 --end-date 2021-08-13 --nights 1 --stdin < parks-sierras.txt | python3.7 system_notifier.py

import sys
import subprocess

from camping import SUCCESS_EMOJI

first_line = next(sys.stdin)

if "Something went wrong" in first_line:
    # This happens somewhat often, due to throttling on recreation site -- you could have it notify you when this happens if you want though too.
    sys.exit()

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].split(" ")[1]
        s = "{} site(s) available in {}".format(available, name)
        available_site_strings.append(s)

if available_site_strings:
    message = first_line.rstrip()
    message += " ๐Ÿ•๐Ÿ•๐Ÿ•\n"
    message += "\n".join(available_site_strings)
    subprocess.Popen(['notify-send', message])
    sys.exit(0)
else:
    print("No campsites available, not notifying ๐Ÿ˜ž")
    sys.exit(1)

Another option would be for it to run as a script in terminal.

Then I'd just set up a bash script that runs the command every few minutes.

https://askubuntu.com/questions/852070/automatically-run-a-command-every-5-minutes

Ok thanks for the help, this give me stuff to work on.. Appreciate it...

@ccritter is it possible using the --nights arg to be "any"?

Such as --nights * or something like that?

@ccritter is it possible using the --nights arg to be "any"?

I mean, setting --nights 1 will get you there, since that is functionally equivalent...

@aries223 did you end up getting yourself set up? If so, you could probably close this issue.

@ccritter Yes have it running via a shell script. Haven't had a chance to try the notification yet..