Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

I just wanna get an email when Admiral Cloudberg posts one of her essays, what can I say

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This cronjob runs daily to check for new posts on Admiral Cloudberg's Medium page
// and sends an email notification if a new post is found.
// It uses the RSS feed of the Medium page to check for updates.
import { blob } from "https://esm.town/v/std/blob";
import { email } from "https://esm.town/v/std/email";
import { default as Parse } from "npm:rss-to-json";
export default async function notify() {
try {
// Fetch and parse the RSS feed
const rss = await Parse.parse("https://medium.com/feed/@admiralcloudberg");
// Get the latest post
const latestPost = rss.items[0];
// Check if we have a stored "last checked" date
const lastChecked = await blob.getJSON("lastCheckedDate");
const lastCheckedDate = lastChecked ? new Date(lastChecked) : new Date(0);
const latestPostDate = new Date(latestPost.published);
// If the latest post is newer than our last check, send an email
if (latestPostDate > lastCheckedDate) {
await email({
subject: "New post from Admiral Cloudberg!",
text: `There's a new post on Admiral Cloudberg's Medium:
Title: ${latestPost.title}
Link: ${latestPost.link}
Check it out!`,
});
// Update the last checked date
await blob.setJSON("lastCheckedDate", new Date().toISOString());
return new Response("Email sent for new post.", { status: 200 });
} else {
return new Response("No new posts found.", { status: 200 });
}
} catch (error) {
console.error("Error checking for new posts:", error);
return new Response("Error checking for new posts.", { status: 500 });
}
}
August 25, 2024