From 7152dba919594131fcac3fb8499f5c0fae362802 Mon Sep 17 00:00:00 2001 From: Jorge Vargas Date: Tue, 27 Aug 2024 21:10:39 -0600 Subject: [PATCH] Implement RSS feed --- astro.config.mjs | 1 + package.json | 1 + public/rss/pretty-feed-v3.xsl | 141 ++++++++++++++++++++++++++++++++++ src/layouts/base.astro | 7 ++ src/pages/rss.xml.ts | 49 ++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 public/rss/pretty-feed-v3.xsl create mode 100644 src/pages/rss.xml.ts diff --git a/astro.config.mjs b/astro.config.mjs index f6a7a91..3ef4de6 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -9,6 +9,7 @@ import icon from 'astro-icon' // https://astro.build/config export default defineConfig({ + site: 'https://beta.sittingonclouds.net', i18n: { locales: languageTags, defaultLocale: 'en', diff --git a/package.json b/package.json index b7a43ba..4a6a5e4 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@apollo/client": "^3.11.4", "@astrojs/check": "^0.9.2", "@astrojs/node": "^8.3.2", + "@astrojs/rss": "^4.0.7", "@astrojs/tailwind": "^5.1.0", "@auth/core": "^0.32.0", "@eddeee888/gcg-typescript-resolver-files": "^0.10.4", diff --git a/public/rss/pretty-feed-v3.xsl b/public/rss/pretty-feed-v3.xsl new file mode 100644 index 0000000..03ef2bb --- /dev/null +++ b/public/rss/pretty-feed-v3.xsl @@ -0,0 +1,141 @@ + + + + + + + + <xsl:value-of select="/rss/channel/title"/> Web Feed + + + + + + +
+
+

+ + + + + + + + + + + + + + + + + + + Web Feed Preview +

+

+

+ + + + + Visit Website → + +
+

Recent Items

+ +
+

+ + + + + + +

+ + Published: + +
+
+
+ + +
+
diff --git a/src/layouts/base.astro b/src/layouts/base.astro index 727af46..56a5466 100644 --- a/src/layouts/base.astro +++ b/src/layouts/base.astro @@ -25,6 +25,13 @@ import '@/styles/global.css' + +
diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts new file mode 100644 index 0000000..b14a39c --- /dev/null +++ b/src/pages/rss.xml.ts @@ -0,0 +1,49 @@ +import rss, { type RSSFeedItem } from '@astrojs/rss'; +import type { APIContext } from 'astro'; +// @ts-ignore +import { Op } from 'sequelize' + +import db from '@/sequelize'; + +export async function GET(context: APIContext) { + const date = new Date() + date.setDate(date.getDate() - 8) + + const isoDate = date.toISOString() + const limitDate = `${isoDate.split('T')[0]}T00:00:00Z` + const commonArgs = { include: [db.models.artist], order: [['createdAt', 'DESC']] } + + let albums = await db.models.album.findAll({ where: { createdAt: { [Op.gte]: limitDate } }, ...commonArgs }) + if (albums.length < 15) albums = await db.models.album.findAll({ limit: 15, ...commonArgs }) + + const items: RSSFeedItem[] = albums.map((album: any) => { + const { dataValues } = album + const item: RSSFeedItem = { + title: dataValues.title, + pubDate: dataValues.createdAt, + description: dataValues.subTitle || dataValues.artists.map((a: { name: string }) => a.name).join(' - '), + link: `https://www.sittingonclouds.net/album/${dataValues.id}`, + customData: ` + `, + } + + return item + }) + + return rss({ + xmlns: { + media: "http://search.yahoo.com/mrss/", + }, + stylesheet: '/rss/pretty-feed-v3.xsl', + title: 'Sitting on Clouds', + description: 'High Quality soundtrack library', + site: context.site as unknown as string, + trailingSlash: false, + items, + }); +} \ No newline at end of file