Get Poem-a-Day as Push Notification

Since I get Vachanas as a push notification, the probability of me reading it has increased. This encouraged me to add Poem-a-Day to this list. Poets.org publishes Poem-a-Day every day on its website. My initial idea was to scrape the HTML page and send myself a notification. But Poets.org has a simple API that exposes that day's Poem. You can access it here.

https://api.poets.org/api/poem-aday

It's a simple JSON format with both Poem and Poet information.

[
    {
        "poem":
        {
            "title": "At Sixty-Five",
            "text": "<p><span class=\"long-line\">It was all so different than he expected. </span><br />\r\n<span class=\"long-line\">For years he’d been agnostic; now he meditated. </span><br />\r\n<span class=\"long-line\">For years he’d dreamed of being an artist living abroad; </span><br />\r\n<span class=\"long-line\">now he reread Baudelaire, Emerson, Bishop. </span><br />\r\n<span class=\"long-line\">He’d never considered marriage . . . </span><br />\r\n<span class=\"long-line\">Still, a force through green <em>did</em> fuse. </span><br />\r\n<span class=\"long-line\">Yes, he wore his pants looser. </span><br />\r\n<span class=\"long-line\">No, he didn’t do crosswords in bed. </span><br />\r\n<span class=\"long-line\">No, he didn’t file for Social Security. </span><br />\r\n<span class=\"long-line\">Yes, he danced alone in the bathroom mirror, </span><br />\r\n<span class=\"long-line\">since younger men expected generosity. </span><br />\r\n<span class=\"long-line\">Long ago, his thesis had been described as promising, </span><br />\r\n<span class=\"long-line\">“with psychological heat and the consuming </span><br />\r\n<span class=\"long-line\">will of nature.” Now he thought, “<em>This</em> then is all.” </span></p>\r\n\r\n<p><span class=\"long-line\">On the rooftop, in pale flickering moonlight, </span><br />\r\n<span class=\"long-line\">he pondered the annihilated earth. </span><br />\r\n<span class=\"long-line\">At the pond, half-a-mile across was not </span><br />\r\n<span class=\"long-line\">too far to swim because he seemed to be </span><br />\r\n<span class=\"long-line\">going toward something. Yes, the love impulse </span><br />\r\n<span class=\"long-line\">had frequently revealed itself in terms of conflict; </span><br />\r\n<span class=\"long-line\">but this was an old sound, an austere element. </span><br />\r\n<span class=\"long-line\">Yes, he’d been no angel and so what . . . </span><br />\r\n<span class=\"long-line\">Yes, tiny moths emerged from the hall closet. </span><br />\r\n<span class=\"long-line\">Yes, the odor of kombucha made him sick. </span><br />\r\n<span class=\"long-line\">Yes, he lay for hours pondering the treetops, </span><br />\r\n<span class=\"long-line\">the matriarchal clouds, the moon. </span><br />\r\n<span class=\"long-line\">Though his spleen collected melancholy trophies, </span><br />\r\n<span class=\"long-line\">his imagination was not impeded. </span></p>\r\n",
            "soundcloud": "<iframe frameborder=\"0\" height=\"200\" scrolling=\"no\" src=\"https://playlist.megaphone.fm?e=POETS3859573126\" width=\"100%\"></iframe>",
            "vertical": null,
            "uuid": "a5cafd13-e735-4ae9-b445-28fb2528af1a",
            "alias": "/poem/sixty-five",
            "attribution": "<p>Copyright © 2022 by Henri Cole. Originally published in Poem-a-Day on December 5, 2022, by the Academy of American Poets.</p>\r\n",
            "about": "<p>“I wrote this poem on my birthday.”<br />\r\n—<em>Henri Cole</em></p>\r\n"
        },
        "poet":
        {
            "name": "Henri Cole",
            "alias": "/poet/henri-cole",
            "image": "https://api.poets.org/sites/default/files/styles/poem_a_day_portrait/public/images/biographies/HenriCole_UpdatedBioImage2022_0.png?itok=b_NaFBHE"
        }
    }
]

I used Node-Red to format this JSON into the format NTFY requires, along with view actions in case I want to share the URL with folks or read more about the poet. Below is that piece of javascript code in Node-Red. It's important to remember that NTFY supports only text format. So it's required to remove the HTML tags. I have used simple regex to do that. You can add a line break by adding \n if you want.

Node-Red flow for daily inspiration
poem_payload = msg.payload

msg.headers = {}
msg.headers['Content-Type'] = 'application/json';
html = poem_payload[0].poem.text;
text = html.replace(/<[^>]+>/g, '');
poem = "https://poets.org/"+poem_payload[0].poem.alias
poet = "https://poets.org/"+poem_payload[0].poet.alias;

payload = {
    "topic": "personal",
    "priority": 4,
    "tags": [
        "heart"
    ],
    "title": poem_payload[0].poem.title +" by " + poem_payload[0].poet.name,
    "message": text,
    "actions": [{ "action": "view", "label": "Poem", "url": poem }, { "action": "view", "label": "Poet", "url": poet }]
}
msg.payload = payload

return msg;

Also you could choose a different channel for each of this subscription. That way you can get them on different devices based on your preference.


You can read this blog using RSS Feed. But if you are the person who loves getting emails, then you can join my readers by signing up.

Join 2,238 other subscribers

1 Response

  1. December 6, 2022

    […] Clear skies Get Poem-a-Day as Push Notification https://thejeshgn.com/2022/12/06/get-poem-a-day-as-push-notification/ using #NodeRed #ntfy #selfhost Posted on December 6, 2022 by @thej in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.