A simple script to send a text message and email to my wife about our evening walks.
Each day, the sun sets at a different time, and in order to avoid our walk being too hot, too cold, too bright, or too dark, we try to leave approximately 75 minutes before sunset, as our walk takes about 1 hour.
The following bash script runs every morning according to the follwing files:
crontab
0 9 * * * cd /path/to/file && ./sunset_walk.sh
sunset_walk.sh
python sunset_walks.py > daily_message.log # writes the message to the daily log for easy access
DOW=$(date +%A)
cat daily_log.log | mutt -s "${DOW} Sunset Walk Reminder" wife_email@gmail.com
cat daily_log.log | mutt -s "${DOW} Sunset Walk Reminder" wife_phone_number@vtext.com
sunset_walks.py
from datetime import date, timedelta
from suntime import Sun
import personal_info as pi
def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
latitude = pi.home_latitude
longitude = pi.home_longitude
sun = Sun(latitude, longitude)
# Local time
today = date.today()
sunset_time = sun.get_local_sunset_time(today)
walk_departure_time = sunset_time - timedelta(hours=1, minutes=15)
print("Hi Sweetie! \n\n Today, {}, the sun sets at {}, so we should leave to go on our walk at {}. \n\n - JakeBot :)".
format(custom_strftime('%B {S}, %Y', today), sunset_time.strftime('%I:%M %p'), walk_departure_time.strftime('%I:%M %p')))