|
| 1 | +import Parser from "rss-parser"; |
| 2 | +import RSS from "rss"; |
| 3 | + |
| 4 | +const parser = new Parser({ |
| 5 | + headers: { "User-Agent": "Scaleway Serverless Examples" }, |
| 6 | +}); |
| 7 | +const SOURCE_FEED_URL = process.env.SOURCE_FEED_URL || "https://lobste.rs/rss"; |
| 8 | +const WORTHWHILE_TOPICS = process.env.WORTHWHILE_TOPICS || |
| 9 | + "nixos, nix, serverless, terraform"; |
| 10 | + |
| 11 | +const feed = new RSS({ |
| 12 | + title: `Worthwhile items from ${SOURCE_FEED_URL}`, |
| 13 | + description: `All about ${WORTHWHILE_TOPICS}`, |
| 14 | + language: "en", |
| 15 | + ttl: "60", |
| 16 | +}); |
| 17 | + |
| 18 | +// This is a simple function that given an RSS feed, |
| 19 | +// filters out the items that contain any of the topics in the WORTHWHILE_TOPICS environment variable. |
| 20 | +// It then exposes a RSS feed on its own, with the filtered items. |
| 21 | +async function handler(event, _context, _cb) { |
| 22 | + const userAgent = event.headers["User-Agent"]; |
| 23 | + const ip = event.headers["X-Forwarded-For"].split(",")[0]; |
| 24 | + console.log("Got request from %s with user agent %s", ip, userAgent); |
| 25 | + |
| 26 | + const topics = WORTHWHILE_TOPICS.split(",").map((t) => t.trim()); |
| 27 | + |
| 28 | + const sourceFeed = await parser.parseURL(SOURCE_FEED_URL); |
| 29 | + console.log("Parsing feed: %s", feed.title); |
| 30 | + |
| 31 | + const worthwhileItems = sourceFeed.items.filter((item) => { |
| 32 | + // Filter out items that don't contain any of the topics |
| 33 | + return topics.some((topic) => item.title.toLowerCase().includes(topic)); |
| 34 | + }); |
| 35 | + |
| 36 | + console.log("Found %d worthwhile items", worthwhileItems.length); |
| 37 | + |
| 38 | + worthwhileItems.forEach((item) => { |
| 39 | + // Shamelessly repost the items |
| 40 | + feed.item({ |
| 41 | + title: item.title, |
| 42 | + description: item.content, |
| 43 | + url: item.link, |
| 44 | + date: item.pubDate, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + return { |
| 49 | + statusCode: 200, |
| 50 | + headers: { |
| 51 | + "Content-Type": "application/xml", |
| 52 | + }, |
| 53 | + body: feed.xml(), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +export { handler }; |
| 58 | + |
| 59 | +if (process.env.NODE_ENV === "development") { |
| 60 | + import("@scaleway/serverless-functions").then((nodeOffline) => { |
| 61 | + nodeOffline.serveHandler(handler, 8081); |
| 62 | + }); |
| 63 | +} |
0 commit comments