You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB

import watt from "../wattpad.ts";
import { Feed } from "npm:feed";
// FIXME: put in a utils file somewhere to reduce code duplication
function decodeCopyright(licenseID: number) {
switch (licenseID) {
case 1:
return "All Rights Reserved";
case 2:
return "Public Domain";
case 3:
return "CC-BY";
case 4:
return "CC-BY-NC";
case 5:
return "CC-BY-NC-ND";
case 6:
return "CC-BY-NC-SA";
case 7:
return "CC-BY-SA";
case 8:
return "CC-BY-ND";
}
// assume this by default because lol
return "All Rights Reserved";
}
export default async (params: URLSearchParams) => {
const feedtype = params.get("feedtype");
const id = params.get("id");
if (feedtype && id) {
const story = await watt.getStory(id, true, true);
const feed = new Feed({
title: story.name,
description: story.storyJSON.description,
id: story.id,
// FIXME: url shouldn't be hardcoded
link: `https://voltpad.ruthenic.com/story?id=${story.id}`,
// FIXME: see above
favicon: `https://voltpad.ruthenic.com/favicon`,
// FIXME: see above
feedLinks: {
json: `https://voltpad.ruthenic.com/feed?feedtype=json&id=${story.id}`,
atom: `https://voltpad.ruthenic.com/feed?feedtype=atom&id=${story.id}`,
rss: `https://voltpad.ruthenic.com/feed?feedtype=rss&id=${story.id}`
},
// FIXME: this should be read from the work once we have proper decoding
// language: "en",
image: story.storyJSON.cover,
copyright: decodeCopyright(story.storyJSON.copyright),
updated: new Date(story.storyJSON.lastPublishedPart.createDate),
generator: "Voltpad (via npm:feed)",
author: {
name: story.storyJSON.user.fullname,
link: `https://wattpad.com/user/${story.storyJSON.user.name}`
}
});
story.chapters.forEach((ch, idx) => {
feed.addItem({
title: ch.name,
id: ch.id,
link: `https://voltpad.ruthenic.com/story?id=${story.id}&chapter=${idx}`,
content: ch.html,
date: new Date(ch.partJSON.createDate),
image: ch.partJSON.photoUrl ?? undefined
});
});
story.tags.forEach((tag) => {
feed.addCategory(tag);
});
switch (feedtype) {
case "rss":
return feed.rss2();
case "atom":
return feed.atom1();
case "json":
return feed.json1();
default:
// return RSS by default i guess, because it's most ubiquitous
return feed.rss2();
}
} else {
throw "deez nuts";
}
};