Caches are valid rss feeds now! Yay!

This commit is contained in:
dakedres 2024-02-10 16:42:31 -07:00
parent fa7f27d7ce
commit d3ff0926ec

14
lib.js
View File

@ -138,8 +138,11 @@ export const delayedFetch = async (url, options, courtesyWait = 5 * 1000) => {
return await fetch(url, options) return await fetch(url, options)
} }
export const getCacheFilename = (source) =>
source.name + '.xml'
export const getCachePath = (source, { directory }) => export const getCachePath = (source, { directory }) =>
Path.join(directory, source.name + '.xml') Path.join(directory, getCacheFilename(source))
export const cacheSource = (source, cache) => export const cacheSource = (source, cache) =>
write(getCachePath(source, cache), createCache(source, cache)) write(getCachePath(source, cache), createCache(source, cache))
@ -186,17 +189,16 @@ export const openCache = async (source, cache) => {
export const buildCacheLink = source => export const buildCacheLink = source =>
new URL('https://' + source.hostname) new URL('https://' + source.hostname)
// TODO: Support atom links
// https://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html
// TODO: Add a description of some kind
export const createCache = (source, cache) => `\ export const createCache = (source, cache) => `\
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel> <channel>
<title>${source.displayName}</title> <title>${source.displayName}</title>
<description>${source.description}</description>
<link>${buildCacheLink(source)}</link> <link>${buildCacheLink(source)}</link>
<atom:link href="${new URL(getCacheFilename(source), cache.directoryUrl)}" rel="self" type="application/rss+xml" />
<pubDate>${new Date(source.latestPostDate).toUTCString()}</pubDate> <pubDate>${new Date(source.latestPostDate).toUTCString()}</pubDate>
<generator>rssssing</generator> <generator>rssssing</generator>
${source.posts.map(post => post.item.outerHTML.replaceAll(/\n\s*/g, '')).join('\n')} ${source.posts.map(post => post.item.outerHTML.replaceAll(/\n\s*/g, '')).join('\n')}
@ -582,6 +584,7 @@ export const tumblr = {
createSource(user, courtesyWait, postReducerCallback, cache) { createSource(user, courtesyWait, postReducerCallback, cache) {
let lowercaseUser = user.toLowerCase() let lowercaseUser = user.toLowerCase()
let source = { let source = {
description: `Aggregate feed for @${lowercaseUser} on tumblr.com`,
hostname: lowercaseUser + '.tumblr.com', hostname: lowercaseUser + '.tumblr.com',
pathname: 'rss', pathname: 'rss',
courtesyWait, courtesyWait,
@ -621,6 +624,7 @@ export const tumblr = {
export const nitter = { export const nitter = {
createSource(user, instances, courtesyWait, postReducerCallback, cache) { createSource(user, instances, courtesyWait, postReducerCallback, cache) {
let source = { let source = {
description: `Aggregate feed for @${user} on twitter.com`,
instances, instances,
pathname: user + '/rss', pathname: user + '/rss',
courtesyWait, courtesyWait,