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

export * from "https://deno.land/x/wattpad@v0.0.4/mod.ts";
import Wattpad, { Story } from "https://deno.land/x/wattpad@v0.0.4/mod.ts";
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;
};
} = {};
// re-init oldest story in cache every 5 minutes
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;
cache[oldStory.story.id].timestamp = new Date();
cache[oldStory.story.id].story.init().then(async (_) => {
for (
let i = 0;
i < cache[oldStory.story.id].story.chapters.length;
i++
) {
await cache[oldStory.story.id].story.chapters[i].init();
}
});
}, 300000);
export { watt };
export default {
search: watt.search,
tagSearch: watt.tagSearch,
async getStory(id: string, initChapters = true): Promise<Story> {
if (!cache[id]) {
cache[id] = {
story: watt.getStory(id),
timestamp: new Date(),
};
await cache[id].story.init();
if (initChapters) {
for (let i = 0; i < cache[id].story.chapters.length; i++) {
await cache[id].story.chapters[i].init();
}
}
}
return cache[id].story;
},
};