Generate Atom feed
This commit is contained in:
45
src/lib/server/rss.ts
Normal file
45
src/lib/server/rss.ts
Normal file
@ -0,0 +1,45 @@
|
||||
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.atom`
|
||||
},
|
||||
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.atom', feed.atom1(), { encoding: 'utf8' });
|
||||
}
|
Reference in New Issue
Block a user