45 lines
1.3 KiB
TypeScript

import { BASE_URL } from '$env/static/private';
import { PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME } from '$env/static/public';
import type { Post } from '$lib//mastodon/response';
import { Feed } from 'feed';
import fs from 'fs/promises';
export function createFeed(posts: Post[]): Feed {
const baseUrl = BASE_URL.endsWith('/') ? BASE_URL : BASE_URL + '/';
const feed = new Feed({
title: `${PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME} music feed`,
description: `Posts about music on ${PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME}`,
id: baseUrl,
link: baseUrl,
language: 'en',
//image: "http://example.com/image.png",
//favicon: "http://example.com/favicon.ico",
copyright: '',
generator: 'moshing-mamut',
feedLinks: {
atom: `${BASE_URL}/feed.xml`
},
author: {
name: '@aymm',
link: 'https://metalhead.club/@aymm'
},
});
posts.forEach(p => {
feed.addItem({
title: p.content,
id: p.url,
link: p.url,
content: p.content,
author: [{
name: p.account.acct,
link: p.account.url
}],
date: new Date(p.created_at)
})
});
feed.addCategory('Music');
return feed;
}
export async function saveAtomFeed(feed: Feed) {
await fs.writeFile('feed.xml', feed.atom1(), { encoding: 'utf8' });
}