import { Feed } from "feed" import { getAllPodcastSeries, getAllPosts, getProjectDetails } from "../external/cms" import fs from "fs" import config from './config' import htmlFindReplaceElementAttrs from "html-find-replace-element-attrs" export const generateRssFeed = async (filter) => { const project = await getProjectDetails(filter); const posts = typeof filter === "undefined" ? await getAllPosts() : await getAllPosts(project.data[0].attributes.tags); const siteURL = config.siteURL; const date = new Date(); const Title = typeof filter === "undefined" ? "Angry Beanie" : project.data[0].attributes.Title const Description = typeof filter == "undefined" ? "A place for all my thoughts and projects" : project.data[0].attributes.Description const feedTitle = typeof filter === "undefined" ? "Angry-Beanie" : project.data[0].attributes.Slug const author = { name: "James Purser", email: "james@angrybeanie.com", link: "https://twitter.com/purserj", }; const feed = new Feed({ title: Title, description: 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}/feed/${feedTitle}-feed.xml`, json: `${siteURL}/feed/${feedTitle}-feed.json`, atom: `${siteURL}/feed/${feedTitle}-atom.xml`, }, author, }); posts.data.forEach((post) => { const url = `${siteURL}/news/${post.attributes.Slug}`; console.log(htmlFindReplaceElementAttrs.find( post.attributes.Body, {tag: "img", attr: "src"} )) const body = htmlFindReplaceElementAttrs.replace( post.attributes.Body, item => item.parsedUrl, { tag: "img", attr: "src", parseAttrValueAsUrl: true, baseUrl: config.siteURL, urlProtocol: "https", } ) feed.addItem({ title: post.attributes.Title, id: url, link: url, description: body, content: body, author: [author], contributor: [author], date: new Date(post.attributes.publishedAt), }); }); fs.mkdirSync("./public/feed", { recursive: true }); fs.writeFileSync(`./public/feed/${feedTitle}-feed.xml`, feed.rss2()); fs.writeFileSync(`./public/feed/${feedTitle}-atom.xml`, feed.atom1()); fs.writeFileSync(`./public/feed/${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/feed/${series.attributes.Slug}.xml`, feed.rss2()); }) }