Skip to content

Commit

Permalink
fix the rss feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-murphy committed Apr 12, 2024
1 parent c8a1391 commit a6a99c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions functions/src/api/routes/capesInTheWestMarch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const citwmFeed: Route = {
handler: async function (_req: Request, res: Response) {
try {
const rssFeed = await getMostRecentRss("Capes in the West March");
res.setHeader("content-type", "application/rss+xml ");
res.status(200).send(rssFeed.rss);
} catch (ex) {
res.sendStatus(404);
Expand Down
12 changes: 6 additions & 6 deletions functions/src/utils/firestore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { initializeApp } from "firebase-admin";
import { DocumentSnapshot, Timestamp, getFirestore } from "firebase-admin/firestore";
import {
Change,
FirestoreEvent,
} from "firebase-functions/v2/firestore";
DocumentSnapshot,
Timestamp,
getFirestore,
} from "firebase-admin/firestore";
import { Change, FirestoreEvent } from "firebase-functions/v2/firestore";
import { PodcastChannel, PodcastEpisode, RssDocument } from "./types";

initializeApp();
Expand Down Expand Up @@ -71,10 +72,9 @@ export function getDataFromEvent<T>(
event: FirestoreEvent<Change<DocumentSnapshot> | undefined>,
) {
if (event === undefined) return null;
const eventData = event.data;//event.data.after.data();
const eventData = event.data; //event.data.after.data();
if (eventData === undefined) return null;
const afterData = eventData.after.data();
if (afterData === undefined) return null;
return afterData as T;
}

18 changes: 12 additions & 6 deletions functions/src/utils/makeRss.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Timestamp } from "firebase-admin/firestore";
import { PodcastChannel, PodcastEpisode } from "./types";
//import { JSDOM } from "jsdom";
//import { logger } from "firebase-functions/v1";

export function makeRss(feed: PodcastChannel, episodes: PodcastEpisode[]) {
return `<?xml version="1.0" encoding="UTF-8"?>
// xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
const rssString = `<?xml version="1.0" encoding="UTF-8"?>
<rss
version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
Expand All @@ -11,11 +14,14 @@ export function makeRss(feed: PodcastChannel, episodes: PodcastEpisode[]) {
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:podcast="https://podcastindex.org/namespace/1.0"
xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
${makeChannel(feed, episodes.map((episode) => makeItem(episode)).join(""))}
</rss>`;
//const a = new JSDOM(rssString, { contentType: "text/xml" });
//return a.serialize();
//logger.info(a.serialize);
return rssString;
}

function makeChannel(channel: PodcastChannel, items: string) {
Expand All @@ -38,9 +44,9 @@ function makeChannel(channel: PodcastChannel, items: string) {
<atom:link href="${channel.feedUrl}" rel="self" type="application/rss+xml"/>
<docs>${channel.contact.site}</docs>
<podcast:locked>${channel.metadata.locked}</podcast:locked>
<itunes:complete>${channel.metadata.complete}</itunes:complete>
${items}
</channel>`;
</channel>`;
// <itunes:complete>${channel.metadata.complete}</itunes:complete>
}

function formatCategories(
Expand All @@ -62,7 +68,7 @@ function formatCategories(
function makeItem(episodeData: PodcastEpisode) {
return `<item>
<title>${episodeData.title}</title>
<link>${episodeData.fileData.url}</link>
<link><![CDATA[${episodeData.fileData.url}]]></link>
<guid isPermaLink="true"><![CDATA[${episodeData.fileData.url}]]></guid>
<description><![CDATA[${episodeData.description}]]></description>
<pubDate>${new Timestamp(episodeData.publishDate.seconds, episodeData.publishDate.nanoseconds).toDate().toUTCString()}</pubDate>
Expand All @@ -71,7 +77,7 @@ function makeItem(episodeData: PodcastEpisode) {
<itunes:season>${episodeData.metadata.season}</itunes:season>
<itunes:episode>${episodeData.metadata.episode}</itunes:episode>
<itunes:episodeType>${episodeData.metadata.type || "full"}</itunes:episodeType>
<enclosure url="${episodeData.fileData.url}" length="${episodeData.fileData.size}" type="audio/mpeg"/>
<enclosure url="${episodeData.fileData.url.replace("&", "&amp;")}" length="${episodeData.fileData.size}" type="audio/mpeg"/>
</item>`;
}
/*
Expand Down

0 comments on commit a6a99c4

Please sign in to comment.