|
|
|
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";
|
|
|
|
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
|
|
|
|
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];
|
|
|
|
}, 300000);
|
|
|
|
|
|
|
|
export { watt };
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getUser: watt.getUser,
|
|
|
|
search: watt.search,
|
|
|
|
async getStory(
|
|
|
|
id: string,
|
|
|
|
initChapters = true,
|
|
|
|
recache = false,
|
|
|
|
): Promise<Story> {
|
|
|
|
if (!cache[id] || recache) {
|
|
|
|
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>",
|
|
|
|
);
|
|
|
|
if (initChapters) {
|
|
|
|
for (let i = 0; i < cache[id].story.chapters.length; i++) {
|
|
|
|
await cache[id].story.chapters[i].init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cache[id].story;
|
|
|
|
},
|
|
|
|
};
|