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
import { blob } from "https://esm.town/v/std/blob?v=12";
import { email } from "https://esm.town/v/std/email?v=12";
import RssParser from "npm:rss-parser";
export async function rssNotify() {
const lastRunAt = await blob.getJSON(import.meta.url);
console.log(`Last run: ${lastRunAt || "Never"}`);
await blob.setJSON(import.meta.url, new Date());
const feed = await new RssParser().parseURL("https://www.inkandswitch.com/index.xml");
console.log(`There are ${feed.items.length} items in the RSS feed`);
const newItems = feed.items.filter(
(item) => lastRunAt ? new Date(item.pubDate) > new Date(lastRunAt) : true,
);
console.log(`There are ${newItems.length} new items in the RSS feed`);
// if there are new items, email them to me
if (newItems.length > 0) {
await email({
subject: `There are ${newItems.length} new items in the RSS feed`,
html: newItems.map((item) => `<p><a href="${item.link}">${item.title}</a></p>`).join(""),
});
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 7, 2024