angrybeanie-front-end/data/internal/feed-generator.js

191 lines
3.8 KiB
JavaScript
Raw Normal View History

import { Feed } from "feed"
import { getAllPodcastSeries, getAllPosts } from "../external/cms"
import fs from "fs"
import config from './config'
export const generateRssFeed = async (filter) => {
const posts = await getAllPosts(filter);
const siteURL = config.siteURL;
const date = new Date();
const Title = typeof filter === "undefined" ? "Angry Beanie" : filter
const feedTitle = typeof filter === "undefined" ? "Angry-Beanie" : filter
const author = {
name: "James Purser",
email: "james@angrybeanie.com",
link: "https://twitter.com/purserj",
};
const feed = new Feed({
title: Title,
description: "A place for all my thoughts and projects",
id: siteURL,
link: siteURL,
image: `${siteURL}/public/images/logo.svg`,
favicon: `${siteURL}/public/images/favicon.png`,
copyright: `All rights reserved ${date.getFullYear()}, James Purser`,
updated: date,
generator: "Feed for Node.js",
feedLinks: {
rss2: `${siteURL}/feeds/${feedTitle}-feed.xml`,
json: `${siteURL}/feeds/${feedTitle}-feed.json`,
atom: `${siteURL}/feeds/${feedTitle}-atom.xml`,
},
author,
});
posts.data.forEach((post) => {
const url = `${siteURL}/news/${post.attributes.Slug}`;
feed.addItem({
title: post.attributes.Title,
id: url,
link: url,
description: post.attributes.Abstract,
content: post.attributes.Abstract,
author: [author],
contributor: [author],
date: new Date(post.attributes.publishedAt),
});
});
fs.mkdirSync("./public/feeds", { recursive: true });
fs.writeFileSync(`./public/feeds/${feedTitle}-feed.xml`, feed.rss2());
fs.writeFileSync(`./public/feeds/${feedTitle}-atom.xml`, feed.atom1());
fs.writeFileSync(`./public/feeds/${feedTitle}-feed.json`, feed.json1());
}
export const generatePodcastFeeds = async () => {
const podcastSeries = await getAllPodcastSeries()
const siteURL = config.siteURL;
console.log(config)
const author = {
name: "James Purser",
email: "james@angrybeanie.com",
link: "https://twitter.com/purserj",
};
const date = new Date();
podcastSeries.data.forEach((series) => {
const feed = new Feed({
title: series.attributes.Title,
description: series.attributes.Description,
id: siteURL,
link: siteURL,
image: `${siteURL}/public/images/logo.svg`,
favicon: `${siteURL}/public/images/favicon.png`,
copyright: `All rights reserved ${date.getFullYear()}, James Purser`,
updated: date,
generator: "Feed for Node.js",
feedLinks: {
rss2: `${siteURL}/feeds/${series.attributes.Slug}.xml`,
},
author,
});
const episodes = series.attributes.podcast_episodes.data
episodes.forEach((episode) => {
const url = `${siteURL}/shows/${series.attributes.Slug}/${episode.attributes.Slug}`;
const media_url = `${siteURL}${episode.attributes.Audio.data.attributes.url}`
const media = {
url: media_url,
type: episode.attributes.Audio.data.attributes.mime,
length: 0,
title: episode.attributes.Audio.data.attributes.name,
duration: 0,
}
feed.addItem({
title: episode.attributes.Title,
id: url,
link: url,
description: episode.attributes.Description,
content: episode.attributes.Description,
author: [author],
contributor: [author],
date: new Date(episode.attributes.publishedAt),
enclosure: media
})
})
console.log(feed.rss2())
fs.writeFileSync(`./public/feeds/${series.attributes.Slug}.xml`, feed.rss2());
})
}