From 4de8c5e374b741a98d56b0cab8cbeeb7859e88e4 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sun, 3 Nov 2024 20:57:11 -0800 Subject: [PATCH] Add JSON Feed --- functions/feed.json.js | 32 ++++++++++++++++++++++++++++++++ index.html | 1 + 2 files changed, 33 insertions(+) create mode 100644 functions/feed.json.js diff --git a/functions/feed.json.js b/functions/feed.json.js new file mode 100644 index 0000000..fe6e9a0 --- /dev/null +++ b/functions/feed.json.js @@ -0,0 +1,32 @@ +import { getGoogleAuthToken } from './auth.js'; + +export async function onRequestGet({ env }) { + const token = await getGoogleAuthToken(env.EMAIL, env.PRIVATE_KEY, 'https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/documents.readonly'); + const sheetUrl = 'https://sheets.googleapis.com/v4/spreadsheets/1pfGF8yBu3D0GPTezygLuzu3Cif8SkjhtG98nL-czlhc/values/B3:I'; + const googleSheet = await fetch(sheetUrl, { headers: { Authorization: `Bearer ${token}` } }).then(r => r.json()); + const feed = { + version: 'https://jsonfeed.org/version/1.1', + title: 'ardislu.dev', + home_page_url: 'https://ardislu.dev/', + feed_url: 'https://ardislu.dev/feed.json', + description: 'Notes on web development, crypto, self-hosting, and tech in general.', + icon: 'https://ardislu.dev/logo.svg', + favicon: 'https://ardislu.dev/logo.svg', + authors: [{ name: 'Ardis Lu', url: 'mailto:ardis@ardislu.dev' }], + language: 'en-US', + items: [] + } + for (const article of googleSheet.values) { + feed.items.push({ + id: `urn:google:${article[2]}`, + url: `https://ardislu.dev${article[4]}`, + title: article[0], + content_html: `

${article[1]}

${article[3]}`, + date_published: new Date(article[5]).toISOString(), + date_modified: new Date(article[6]).toISOString(), + tags: article[7].split(',').map(s => s.trim()) + }); + } + + return new Response(JSON.stringify(feed), { headers: { 'Content-Type': 'application/feed+json' } }); +} \ No newline at end of file diff --git a/index.html b/index.html index 9fb43c0..f16a3fa 100644 --- a/index.html +++ b/index.html @@ -25,6 +25,7 @@ +