237 lines
5.7 KiB
JavaScript
Executable file
237 lines
5.7 KiB
JavaScript
Executable file
import { Feed } from "feed"
|
|
import { Podcast } from 'podcast';
|
|
import getConfig from 'next/config'
|
|
import { getAllPodcastSeries, getAllPosts, getProjectDetails } from "../external/cms"
|
|
import fs from "fs"
|
|
import config from './config'
|
|
import htmlFindReplaceElementAttrs from "html-find-replace-element-attrs"
|
|
import Image from "next/legacy/image"
|
|
|
|
export const generateRssFeed = async (filter) => {
|
|
|
|
const project = await getProjectDetails(filter);
|
|
|
|
const posts = typeof filter === "undefined" ? await getAllPosts() : await getAllPosts(project.data[0].attributes.Slug);
|
|
|
|
const siteURL = config.siteURL;
|
|
|
|
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
|
|
|
|
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}`;
|
|
|
|
const body = htmlFindReplaceElementAttrs.replace(
|
|
post.attributes.FullBody,
|
|
item => item.parsedUrl,
|
|
{
|
|
tag: "img",
|
|
attr: "src",
|
|
parseAttrValueAsUrl: true,
|
|
baseUrl: config.siteURL,
|
|
urlProtocol: "https",
|
|
}
|
|
)
|
|
|
|
var fullbody;
|
|
|
|
if (post.attributes.FeatureImage.data) {
|
|
const imagepath = serverRuntimeConfig.media_path + post.attributes.FeatureImage.data.attributes.formats.large.url
|
|
const featuredImage = <Image src={imagepath} height="100%" width="100%" />
|
|
fullbody = "<p><img src=" + imagepath + "></img></p>"+body
|
|
} else {
|
|
fullbody = body
|
|
}
|
|
|
|
feed.addItem({
|
|
|
|
title: post.attributes.Title,
|
|
|
|
id: url,
|
|
|
|
link: url,
|
|
|
|
description: fullbody,
|
|
|
|
content: fullbody,
|
|
|
|
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;
|
|
|
|
const author = {
|
|
|
|
name: "James Purser",
|
|
|
|
email: "james@angrybeanie.com",
|
|
|
|
link: "https://aus.social/purserj",
|
|
|
|
};
|
|
|
|
const date = new Date();
|
|
|
|
podcastSeries.data.forEach((series) => {
|
|
|
|
const feed = new Podcast({
|
|
itunesAuthor: "James Purser",
|
|
|
|
itunesOwner: {
|
|
name: "James Purser",
|
|
email: "james@angrybeanie.com"
|
|
},
|
|
|
|
title: series.attributes.Title,
|
|
|
|
description: series.attributes.Description,
|
|
|
|
id: siteURL + "/podcasts/shows/" + series.attributes.Slug,
|
|
|
|
link: siteURL + "/podcasts/shows/" + series.attributes.Slug,
|
|
|
|
image: `${siteURL}` + series.attributes.Logo.data.attributes.url,
|
|
|
|
itunesImage: `${siteURL}` + series.attributes.Logo.data.attributes.url,
|
|
|
|
language: "English",
|
|
|
|
favicon: `${siteURL}/public/images/favicon.png`,
|
|
|
|
copyright: `All rights reserved ${date.getFullYear()}, James Purser`,
|
|
|
|
updated: date,
|
|
|
|
generator: "Feed for Node.js",
|
|
|
|
itunesCategory: [{text: series.attributes.iTunesCategory}],
|
|
});
|
|
|
|
const episodes = series.attributes.podcast_episodes.data
|
|
|
|
episodes.forEach((episode) => {
|
|
|
|
var subtitles = {}
|
|
|
|
if ( episode.attributes.Subtitles.data !== null) {
|
|
subtitles = {
|
|
url: `${siteURL}${episode.attributes.Subtitles.data.attributes.url}`,
|
|
type: episode.attributes.Subtitles.data.attributes.mime,
|
|
language: "English"
|
|
}
|
|
console.log(subtitles)
|
|
}
|
|
|
|
const url = `${siteURL}/podcasts/shows/${series.attributes.Slug}/${episode.attributes.Slug}`;
|
|
|
|
const media_url = `${siteURL}${episode.attributes.Audio_MP3.data.attributes.url}`
|
|
|
|
const media = {
|
|
url: media_url,
|
|
type: episode.attributes.Audio_MP3.data.attributes.mime,
|
|
size: episode.attributes.Audio_MP3.data.attributes.size,
|
|
title: episode.attributes.Audio_MP3.data.attributes.name,
|
|
duration: 0,
|
|
}
|
|
|
|
feed.addItem({
|
|
|
|
title: episode.attributes.Title,
|
|
|
|
id: url,
|
|
|
|
url: url,
|
|
|
|
description: episode.attributes.Description,
|
|
|
|
content: episode.attributes.Description,
|
|
|
|
author: [author],
|
|
|
|
contributor: [author],
|
|
|
|
date: new Date(episode.attributes.publishedAt),
|
|
|
|
enclosure: media,
|
|
|
|
itunesSubtitle: subtitles,
|
|
|
|
podcastTranscript: [subtitles]
|
|
})
|
|
})
|
|
|
|
fs.writeFileSync(`./public/feed/${series.attributes.Slug}.xml`, feed.buildXml());
|
|
})
|
|
}
|
|
|