horrible jank to make single chapter works 'work'

master
Drake 3 months ago
parent 50edb2df60
commit fdcc73cb7a

@ -1,4 +1,5 @@
{
"deno.enable": true,
"deno.unstable": true
"deno.unstable": true,
"editor.formatOnSave": true
}

@ -24,6 +24,12 @@ export default class AO3 {
get: async (path: string) => {
const res = await fetch(
opts?.url ?? "https://archiveofourown.org/" + path,
{
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; rv:106.0) Gecko/20100101 Firefox/106.0",
},
},
);
if (res.status > 300) {
console.log(res);

@ -78,7 +78,7 @@ export default class Chapter {
this.populateMetadata();
this.populateSummary();
this.populateNotes();
this.populateText();
await this.populateText();
}
populateMetadata() {
@ -104,7 +104,7 @@ export default class Chapter {
?.trim();
}
populateText() {
async populateText() {
/*this.text = this.#document.querySelector("div.userstuff[role='article']")?.innerText.trim().replace(/Chapter Text\s+/, "") as string*/
//"div.userstuff[role='article'] > p"
Array.from(
@ -113,7 +113,27 @@ export default class Chapter {
),
).forEach(
(t) => this.#text += (t as Element).innerText + "\n",
console.log(this.#text),
);
this.#text = this.#text.trim();
try {
this.#text = this.#text.trim();
} catch {
//assume single chapter work
const res = await this.#session.get(
`/works/${this.#workID}?view_adult=true`,
);
this.#document = this.#DOMParser.parseFromString(
await res.text(),
"text/html",
) as HTMLDocument;
Array.from(
this.#document.querySelectorAll(
"[role='article'] > div.userstuff > p",
),
).forEach(
(t) => this.#text += (t as Element).innerText + "\n",
console.log(this.#text),
);
}
}
}

@ -30,6 +30,7 @@ export const Warnings = {
};
export interface SearchParameters {
limit?: number;
any?: string;
title?: string;
author?: string;
@ -80,9 +81,14 @@ export default class Search {
"text/html",
) as HTMLDocument;
let i = 0;
const limit = this.#opts.limit ?? 20;
await asyncForEach(
Array.from(this.#document.querySelectorAll("[role='article']")),
async (e: Element) => {
if (i >= limit) {
return;
}
const workId = e.id.replace("work_", "");
console.log(workId);
const res = await this.#session.get(
@ -96,6 +102,7 @@ export default class Search {
);
await work.init();
this.results.push(work);
i++;
},
);
}

@ -5,11 +5,12 @@ const ao3 = new AO3();
const res = await ao3.search({
any: "hazbin hotel",
limit: 1,
});
await res.update(1);
console.log(res.results[0]);
console.log(await res.results[0].chapters[0].text);
/*
let work = await ao3.getWork("37522864");

Loading…
Cancel
Save