From fdcc73cb7a92bc1ab9c74f9a974bdf172399f0f9 Mon Sep 17 00:00:00 2001 From: Ruthenic Date: Fri, 11 Nov 2022 11:10:14 -0500 Subject: [PATCH] horrible jank to make single chapter works 'work' --- .vscode/settings.json | 3 ++- src/classes/AO3.ts | 6 ++++++ src/classes/Chapter.ts | 26 +++++++++++++++++++++++--- src/classes/Search.ts | 7 +++++++ test.ts | 3 ++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index be67fe4..0eb830f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "deno.enable": true, - "deno.unstable": true + "deno.unstable": true, + "editor.formatOnSave": true } diff --git a/src/classes/AO3.ts b/src/classes/AO3.ts index 3d0fb9c..b412fbe 100644 --- a/src/classes/AO3.ts +++ b/src/classes/AO3.ts @@ -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); diff --git a/src/classes/Chapter.ts b/src/classes/Chapter.ts index a9a400e..f79957d 100644 --- a/src/classes/Chapter.ts +++ b/src/classes/Chapter.ts @@ -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), + ); + } } } diff --git a/src/classes/Search.ts b/src/classes/Search.ts index b865d15..92b8e0b 100644 --- a/src/classes/Search.ts +++ b/src/classes/Search.ts @@ -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++; }, ); } diff --git a/test.ts b/test.ts index 6b24148..cca55d6 100644 --- a/test.ts +++ b/test.ts @@ -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");