krz/daily-poem

Archived

Poetry emailed, at your will. automation email poetry

Commit d5b1b5d10b

d5b1b5d10b2d9e9f24ef796d71ae95acb2a58de1

parent: 87148bd456

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 19:07 UTC

Add Qlty config; pass a Poem to build_message

Layout: unified · split

.qlty/.gitignore added +7
@@ -0,0 +1,7 @@
1*
2!configs
3!configs/**
4!hooks
5!hooks/**
6!qlty.toml
7!.gitignore
.qlty/qlty.toml added +44
@@ -0,0 +1,44 @@
1config_version = "0"
2
3exclude_patterns = [
4 "**/vendor/**",
5 "**/node_modules/**",
6 "**/testdata/**",
7 "**/dist/**",
8 "**/.build/**",
9 "**/build/**",
10 "**/*.min.*",
11 "**/generated/**",
12 ".claude/**",
13 "**/Pods/**",
14]
15
16test_patterns = [
17 "**/test/**",
18 "**/tests/**",
19 "**/Tests/**",
20 "**/*_test.*",
21 "**/test_*.*",
22 "**/*.test.*",
23 "**/*Tests.swift",
24 "e2e/**",
25]
26
27[[source]]
28name = "default"
29default = true
30
31[[plugin]]
32name = "osv-scanner"
33
34[[plugin]]
35name = "trufflehog"
36
37# Early returns are guard clauses, which keep the happy path unindented.
38[smells.return_statements]
39enabled = false
40
41# Total file complexity is the sum of its functions, which function_complexity
42# already checks.
43[smells.file_complexity]
44enabled = false
main.py +14 −9
@@ -1,6 +1,7 @@
1# Fetch a random poem from PoetryDB and mail it. 1# Fetch a random poem from PoetryDB and mail it.
2import smtplib 2import smtplib
3from email.mime.text import MIMEText 3from email.mime.text import MIMEText
4from typing import NamedTuple
4 5
5import requests 6import requests
6 7
@@ -12,30 +13,34 @@ SENDER_EMAIL = ""
12RECIPIENT_EMAILS = "" 13RECIPIENT_EMAILS = ""
13 14
14 15
16class Poem(NamedTuple):
17 title: str
18 author: str
19 line_count: str
20 lines: str
21
22
15def fetch_poem(url=POETRY_URL): 23def fetch_poem(url=POETRY_URL):
16 """Return (title, author, line_count, body) for a random poem.""" 24 """Return a random poem."""
17 json_data = requests.get(url, timeout=30).json() 25 json_data = requests.get(url, timeout=30).json()
18 poem = json_data[0] 26 poem = json_data[0]
19 lines = "" 27 lines = ""
20 for line in poem["lines"]: 28 for line in poem["lines"]:
21 lines = lines + line + "\n" 29 lines = lines + line + "\n"
22 return poem["title"], poem["author"], poem["linecount"], lines 30 return Poem(poem["title"], poem["author"], poem["linecount"], lines)
23 31
24 32
25def build_message(title, author, line_count, lines, sender, recipient): 33def build_message(poem, sender, recipient):
26 """Return a plaintext MIMEText message for one poem.""" 34 """Return a plaintext MIMEText message for one poem."""
27 msg = MIMEText(title + "\n" + author + "\n\n" + lines) 35 msg = MIMEText(poem.title + "\n" + poem.author + "\n\n" + poem.lines)
28 msg["Subject"] = "Your Daily Poem (" + line_count + " lines)" 36 msg["Subject"] = "Your Daily Poem (" + poem.line_count + " lines)"
29 msg["From"] = sender 37 msg["From"] = sender
30 msg["To"] = recipient 38 msg["To"] = recipient
31 return msg 39 return msg
32 40
33 41
34def main(): 42def main():
35 title, author, line_count, lines = fetch_poem() 43 msg = build_message(fetch_poem(), SENDER_EMAIL, RECIPIENT_EMAILS)
36 msg = build_message(
37 title, author, line_count, lines, SENDER_EMAIL, RECIPIENT_EMAILS
38 )
39 44
40 # Send via the local SMTP server, without the envelope header. 45 # Send via the local SMTP server, without the envelope header.
41 s = smtplib.SMTP(SMTP_SERVER) 46 s = smtplib.SMTP(SMTP_SERVER)
tests/test_main.py +3 −2
@@ -1,7 +1,7 @@
1import unittest 1import unittest
2from unittest.mock import patch 2from unittest.mock import patch
3 3
4from main import build_message, fetch_poem 4from main import Poem, build_message, fetch_poem
5 5
6 6
7class FakeResponse: 7class FakeResponse:
@@ -14,7 +14,8 @@ class FakeResponse:
14 14
15class TestBuildMessage(unittest.TestCase): 15class TestBuildMessage(unittest.TestCase):
16 def test_headers_and_body(self): 16 def test_headers_and_body(self):
17 msg = build_message("A Title", "An Author", "4", "one\ntwo\n", "from@x", "to@y") 17 poem = Poem("A Title", "An Author", "4", "one\ntwo\n")
18 msg = build_message(poem, "from@x", "to@y")
18 19
19 self.assertEqual(msg["Subject"], "Your Daily Poem (4 lines)") 20 self.assertEqual(msg["Subject"], "Your Daily Poem (4 lines)")
20 self.assertEqual(msg["From"], "from@x") 21 self.assertEqual(msg["From"], "from@x")