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.

60 lines
1.6 KiB

1 year ago
export * from "https://deno.land/x/wattpad@v0.2.0/mod.ts";
import Wattpad, { Story } from "https://deno.land/x/wattpad@v0.2.0/mod.ts";
1 year ago
import config from "./config.ts";
const watt = new Wattpad();
if (config.user) {
await watt.authenticate(
config.user.username,
config.user.password,
);
}
const cache: {
[k: string]: {
story: Story;
timestamp: Date;
};
} = {};
// delete oldest story in cache every 5 minutes as to not memory leak to hell and back
1 year ago
setInterval(() => {
const oldStory = Object.values(cache).sort((a, b) =>
// @ts-expect-error 2363; javascript is a disgrace but it makes sorting dates easy so ¯\_(ツ)_/¯
b.timestamp - a.timestamp
).pop(); // teehee
if (!oldStory) return;
delete cache[oldStory.story.id];
1 year ago
}, 300000);
export { watt };
export default {
getUser: watt.getUser,
1 year ago
search: watt.search,
async getStory(
id: string,
initChapters = true,
recache = false,
): Promise<Story> {
if (!cache[id] || recache) {
1 year ago
cache[id] = {
story: watt.getStory(id),
timestamp: new Date(),
};
await cache[id].story.init();
cache[id].story.storyJSON.description = cache[id].story.storyJSON
.description.replaceAll(
"\n",
"<br>",
);
1 year ago
if (initChapters) {
for (let i = 0; i < cache[id].story.chapters.length; i++) {
await cache[id].story.chapters[i].init();
}
}
}
return cache[id].story;
},
};