horrible jank to make single chapter works 'work'

master
Drake 1 year ago
parent 50edb2df60
commit fdcc73cb7a

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

@ -24,6 +24,12 @@ export default class AO3 {
get: async (path: string) => { get: async (path: string) => {
const res = await fetch( const res = await fetch(
opts?.url ?? "https://archiveofourown.org/" + path, 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) { if (res.status > 300) {
console.log(res); console.log(res);

@ -78,7 +78,7 @@ export default class Chapter {
this.populateMetadata(); this.populateMetadata();
this.populateSummary(); this.populateSummary();
this.populateNotes(); this.populateNotes();
this.populateText(); await this.populateText();
} }
populateMetadata() { populateMetadata() {
@ -104,7 +104,7 @@ export default class Chapter {
?.trim(); ?.trim();
} }
populateText() { async populateText() {
/*this.text = this.#document.querySelector("div.userstuff[role='article']")?.innerText.trim().replace(/Chapter Text\s+/, "") as string*/ /*this.text = this.#document.querySelector("div.userstuff[role='article']")?.innerText.trim().replace(/Chapter Text\s+/, "") as string*/
//"div.userstuff[role='article'] > p" //"div.userstuff[role='article'] > p"
Array.from( Array.from(
@ -113,7 +113,27 @@ export default class Chapter {
), ),
).forEach( ).forEach(
(t) => this.#text += (t as Element).innerText + "\n", (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 { export interface SearchParameters {
limit?: number;
any?: string; any?: string;
title?: string; title?: string;
author?: string; author?: string;
@ -80,9 +81,14 @@ export default class Search {
"text/html", "text/html",
) as HTMLDocument; ) as HTMLDocument;
let i = 0;
const limit = this.#opts.limit ?? 20;
await asyncForEach( await asyncForEach(
Array.from(this.#document.querySelectorAll("[role='article']")), Array.from(this.#document.querySelectorAll("[role='article']")),
async (e: Element) => { async (e: Element) => {
if (i >= limit) {
return;
}
const workId = e.id.replace("work_", ""); const workId = e.id.replace("work_", "");
console.log(workId); console.log(workId);
const res = await this.#session.get( const res = await this.#session.get(
@ -96,6 +102,7 @@ export default class Search {
); );
await work.init(); await work.init();
this.results.push(work); this.results.push(work);
i++;
}, },
); );
} }

@ -5,11 +5,12 @@ const ao3 = new AO3();
const res = await ao3.search({ const res = await ao3.search({
any: "hazbin hotel", any: "hazbin hotel",
limit: 1,
}); });
await res.update(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"); let work = await ao3.getWork("37522864");

Loading…
Cancel
Save