From ff51bb9ff9b98a0a0ddb23dbc00c339ebbd83a90 Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Mon, 8 Jul 2024 10:44:39 +0100
Subject: [PATCH] Move send_mail() to own module

---
 agenda/mail.py | 28 ++++++++++++++++++++++++++++
 update.py      | 39 ++++++++-------------------------------
 2 files changed, 36 insertions(+), 31 deletions(-)
 create mode 100644 agenda/mail.py

diff --git a/agenda/mail.py b/agenda/mail.py
new file mode 100644
index 0000000..ac629eb
--- /dev/null
+++ b/agenda/mail.py
@@ -0,0 +1,28 @@
+"""Send e-mail."""
+
+import smtplib
+from email.message import EmailMessage
+from email.utils import formatdate, make_msgid
+
+import flask
+
+
+def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
+    """Send an e-mail."""
+    msg = EmailMessage()
+
+    msg["Subject"] = subject
+    msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
+    msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
+    msg["Date"] = formatdate()
+    msg["Message-ID"] = make_msgid()
+
+    # Add extra mail headers
+    for header, value in config["MAIL_HEADERS"]:
+        msg[header] = value
+
+    msg.set_content(body)
+
+    s = smtplib.SMTP(config["SMTP_HOST"])
+    s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
+    s.quit()
diff --git a/update.py b/update.py
index 31d346e..3d4fd2c 100755
--- a/update.py
+++ b/update.py
@@ -4,23 +4,21 @@
 import asyncio
 import json
 import os
-import smtplib
 import sys
 import typing
 from datetime import date, datetime
-from email.message import EmailMessage
-from email.utils import formatdate, make_msgid
 from time import time
 
 import flask
 import requests
 
 import agenda.fx
+import agenda.gwr
+import agenda.mail
 import agenda.thespacedevs
 import agenda.types
 import agenda.uk_holiday
 import agenda.waste_schedule
-from agenda import gwr
 from agenda.types import StrDict
 from web_view import app
 
@@ -50,37 +48,16 @@ async def update_bristol_bins(config: flask.config.Config) -> None:
     print(f"took {time_taken:.1f} seconds")
 
 
-def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
-    """Send an e-mail."""
-    msg = EmailMessage()
-
-    msg["Subject"] = subject
-    msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
-    msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
-    msg["Date"] = formatdate()
-    msg["Message-ID"] = make_msgid()
-
-    # Add extra mail headers
-    for header, value in config["MAIL_HEADERS"]:
-        msg[header] = value
-
-    msg.set_content(body)
-
-    s = smtplib.SMTP(config["SMTP_HOST"])
-    s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
-    s.quit()
-
-
 def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
     """Update GWR advance ticket date cache."""
     filename = os.path.join(config["DATA_DIR"], "advance-tickets.html")
     existing_html = open(filename).read()
-    existing_date = gwr.extract_weekday_date(existing_html)
+    existing_date = agenda.gwr.extract_weekday_date(existing_html)
 
-    new_html = requests.get(gwr.url).text
+    new_html = requests.get(agenda.gwr.url).text
     open(filename, "w").write(new_html)
 
-    new_date = gwr.extract_weekday_date(new_html)
+    new_date = agenda.gwr.extract_weekday_date(new_html)
 
     if existing_date == new_date:
         if sys.stdin.isatty():
@@ -91,11 +68,11 @@ def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
     body = f"""Old date: {existing_date}
 New date: {new_date}
 
-{gwr.url}
+{agenda.gwr.url}
 
 Agenda: https://edwardbetts.com/agenda/
 """
-    send_mail(config, subject, body)
+    agenda.mail.send_mail(config, subject, body)
 
 
 def report_space_launch_change(
@@ -121,7 +98,7 @@ New launch data
 {json.dumps(cur_launch, indent=2)}
     """
 
-    send_mail(config, subject, body)
+    agenda.mail.send_mail(config, subject, body)
 
 
 def get_launch_by_slug(data: StrDict, slug: str) -> StrDict | None: