aboutsummaryrefslogtreecommitdiff
path: root/kitchen-update-email.py
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2020-09-20 01:07:23 +0000
committerMark Powers <mark@marks.kitchen>2020-09-20 01:07:23 +0000
commitb4441232cb2c54e7ab0173a09640eff3828017cd (patch)
tree459d422f093c5de07173469689ecc705a1ef7286 /kitchen-update-email.py
Initial commit
Diffstat (limited to 'kitchen-update-email.py')
-rw-r--r--kitchen-update-email.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/kitchen-update-email.py b/kitchen-update-email.py
new file mode 100644
index 0000000..821127e
--- /dev/null
+++ b/kitchen-update-email.py
@@ -0,0 +1,70 @@
+import smtplib
+from email.mime.text import MIMEText
+from email.utils import formatdate
+import pymysql
+import sys
+
+import email_helper
+from config import config
+
+def get_last_sent_update(cursor):
+ cursor.execute("SELECT last_update FROM updates;")
+ return cursor.fetchone()[0]
+
+def get_later_post(cursor, last):
+ cursor.execute("SELECT id,type FROM posts where id > " + str(last) +" ORDER BY id asc")
+ return cursor.fetchone()
+
+def set_last_sent_update(cursor, db, post_id):
+ cursor.execute("UPDATE updates SET last_update=" + str(post_id)+ " where id=1")
+ db.commit()
+
+def get_emails(cursor):
+ cursor.execute("SELECT name, address FROM emails")
+ return cursor.fetchall()
+
+def add_update(cursor, db, post_id):
+ cursor.execute("insert into updates values (1, " +str(post_id)+", NOW())")
+ db.commit()
+
+def handle_update():
+ """ Check for a new post on my website and email everyone on the list
+ if there is one.
+ """
+
+ db = pymysql.connect("localhost", config["database"]["user"], config["database"]["pass"], config["database"]["db"])
+ cursor = db.cursor()
+
+ last = get_last_sent_update(cursor)
+
+ new_post = get_later_post(cursor, last)
+ if not new_post:
+ print("no new post")
+ sys.exit()
+ if new_post[1] == 'index':
+ print("ignoring index post")
+ sys.exit()
+ print(new_post)
+
+ set_last_sent_update(cursor, db, new_post[0])
+ emails = get_emails(cursor)
+
+ name = config["email"]["name"]
+ frm = config["email"]["user"]
+ subject = "marks.kitchen update"
+ for email in emails:
+ to = email[1]
+ body = """
+ Hi """ + email[0] + """!
+
+ There is a new post on marks.kitchen! Check it out: https://marks.kitchen/post/"""+new_post[1]+"""/"""+str(new_post[0])+"""
+
+ Mark (mark@marks.kitchen)
+ """
+ email_helper.send(frm, name, to, subject, body)
+ add_update(cursor, db, new_post[0])
+ db.close()
+
+if __name__ == "__main__":
+ handle_update()
+