aboutsummaryrefslogtreecommitdiff
path: root/daily-update.py
blob: f9c402d92aa305383be3782711a9043941baf02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3

import smtplib
import sys
import time
import requests

from datetime import date
from bs4 import BeautifulSoup
from email.mime.text import MIMEText 
from email.utils import formatdate

import email_helper
from config import config

def get_weather():
    print("getting weather")
    weather_url = "https://forecast.weather.gov/MapClick.php?lon=%s&lat=%s" % (config["weather"]["lon"], config["weather"]["lat"])
    soup = BeautifulSoup(requests.get(weather_url).text, features="lxml")
    return str(soup.select("#detailed-forecast")[0])

def get_old_news():
    print("getting old news")
    year = int(date.today().strftime("%Y")) - 100
    century_ago = str(year) + date.today().strftime("-%m-%d")
    news_text = ""
    urls = config["news"]["urls"].split(",")
    names = config["news"]["names"].split(",")
    for i in range(len(urls)):
        full_url = urls[i] % century_ago
        name = names[i]
        if requests.get(full_url).status_code != 404:
            news_text += '<a href="%s">%s %s</a>\n' % (full_url, name, century_ago)
    return news_text

def get_unread_reminders():
    print("getting unread reminders")
    subjects = email_helper.filter_unread("subject", "REMINDER:", "subject")
    subjects = [s[len("REMINDER: "):].strip() for s in subjects]
    if len(subjects) > 0:
        reminder_html = "<h1>Reminders:</h1><ul>"
        for s in subjects:
            reminder_html += "<li>%s</li>" % s
        reminder_html += "</ul>\n"
        return reminder_html
    return ""

def format_email():
    print("forming email")
    return '%s%s%s' % (get_unread_reminders(), get_old_news(), get_weather())

def send_update_email():
    frm = config["email"]["user"]
    name = config["email"]["name"]
    
    today = date.today().strftime("%b %d")
    subject = "Updates for " + today

    body = format_email()
    email_helper.send(frm, name, frm, subject, body, "html")

if __name__ == "__main__":
    send_update_email()