aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2023-05-22 15:18:04 -0500
committerChristian Cleberg <hello@cleberg.net>2023-05-22 15:18:04 -0500
commitd70629daf0de6cc42f8c8f39b590ed87c9b3e23e (patch)
treed562d480100b5d4ead892a4fd1609f3610d04ca6 /main.py
downloaddaily-poem-d70629daf0de6cc42f8c8f39b590ed87c9b3e23e.tar.gz
daily-poem-d70629daf0de6cc42f8c8f39b590ed87c9b3e23e.tar.bz2
daily-poem-d70629daf0de6cc42f8c8f39b590ed87c9b3e23e.zip
initial commit
Diffstat (limited to 'main.py')
-rw-r--r--main.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..bf3d530
--- /dev/null
+++ b/main.py
@@ -0,0 +1,39 @@
+# Import Python packages
+from email.mime.text import MIMEText
+import smtplib
+import json
+import requests
+
+# Send API request for a random poem
+json_data = requests.get('https://poetrydb.org/random').json()
+
+# Extract the poem details from the JSON response
+title = json_data[0]['title']
+author = json_data[0]['author']
+line_count = json_data[0]['linecount']
+lines = ''
+for line in json_data[0]['lines']:
+ lines = lines + line + "\n"
+
+# A test print() statement to ensure the request and parsing processed the data
+# correctly
+# print(title, "\n", author, "\n\n", lines)
+
+msg_body = title + "\n" + author + "\n\n" + lines
+
+# Create plaintext message container
+msg = MIMEText(msg_body)
+
+# Prepare the metadata of the message
+sender_email = ''
+recipient_emails = ''
+msg['Subject'] = 'Your Daily Poem (' + line_count + ' lines)'
+msg['From'] = sender_email
+msg['To'] = recipient_email
+
+# Send the message via our own SMTP server, but don't include the
+# envelope header.
+smtp_server = 'localhost'
+s = smtplib.SMTP(smtp_server)
+s.sendmail(sender_email, [recipient_emails], msg.as_string())
+s.quit()