format + fix for when works don't have specific type of tag

master
Drake 1 year ago
parent 8e68139215
commit 50edb2df60

@ -46,6 +46,6 @@ export default class AO3 {
} }
async search(opts: SearchParameters) { async search(opts: SearchParameters) {
return new Search(opts, this.session, new DOMParser()) return new Search(opts, this.session, new DOMParser());
} }
} }

@ -42,7 +42,7 @@ export interface SearchParameters {
} }
export default class Search { export default class Search {
#baseApiURL = "/works/search?commit=Search" #baseApiURL = "/works/search?commit=Search";
#opts: SearchParameters; #opts: SearchParameters;
#session: { #session: {
get: (path: string) => Promise<Response>; get: (path: string) => Promise<Response>;
@ -52,14 +52,14 @@ export default class Search {
results: Work[] = []; results: Work[] = [];
#urlConstructor(pageNum = 1) { #urlConstructor(pageNum = 1) {
let url = this.#baseApiURL let url = this.#baseApiURL;
url += `&page=${pageNum} url += `&page=${pageNum}
&work_search[query]=${this.#opts.any ?? ""} &work_search[query]=${this.#opts.any ?? ""}
&work_search[title]=${this.#opts.title ?? ""} &work_search[title]=${this.#opts.title ?? ""}
&work_search[creators]=${this.#opts.author ?? ""}` &work_search[creators]=${this.#opts.author ?? ""}`;
return url return url;
} }
constructor(opts: SearchParameters, session: { constructor(opts: SearchParameters, session: {
@ -67,30 +67,36 @@ export default class Search {
}, DOMParser: DOMParser) { }, DOMParser: DOMParser) {
this.#session = session; this.#session = session;
this.#opts = opts; this.#opts = opts;
this.#DOMParser = DOMParser this.#DOMParser = DOMParser;
} }
async update(pageNum: number) { async update(pageNum: number) {
this.results = [] this.results = [];
const url = this.#urlConstructor(pageNum) const url = this.#urlConstructor(pageNum);
const res = await this.#session.get(url); const res = await this.#session.get(url);
this.#document = this.#DOMParser.parseFromString( this.#document = this.#DOMParser.parseFromString(
await res.text(), await res.text(),
"text/html", "text/html",
) as HTMLDocument; ) as HTMLDocument;
await asyncForEach(Array.from(this.#document.querySelectorAll("[role='article']")), await asyncForEach(
Array.from(this.#document.querySelectorAll("[role='article']")),
async (e: Element) => { async (e: Element) => {
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(
`/works/${workId}?view_adult=true&view_full_work=true`, `/works/${workId}?view_adult=true&view_full_work=true`,
); );
const work = new Work(workId, await res.text(), this.#session, this.#DOMParser) const work = new Work(
await work.init() workId,
this.results.push(work) await res.text(),
} this.#session,
) this.#DOMParser,
);
await work.init();
this.results.push(work);
},
);
} }
} }

@ -70,7 +70,7 @@ export default class Work {
populateTags() { populateTags() {
Array.from( Array.from(
(this.#document.querySelector("dd.fandom > ul.commas") as Element) (this.#document.querySelector("dd.fandom > ul.commas") as Element)
.children, ?.children ?? [],
).map( ).map(
(t) => this.tags.push(t.children[0].innerText), (t) => this.tags.push(t.children[0].innerText),
); );
@ -78,7 +78,7 @@ export default class Work {
(this.#document.querySelector( (this.#document.querySelector(
"dd.relationship > ul.commas", "dd.relationship > ul.commas",
) as Element) ) as Element)
.children, ?.children ?? [],
).map( ).map(
(t) => this.tags.push(t.children[0].innerText), (t) => this.tags.push(t.children[0].innerText),
); );
@ -86,13 +86,13 @@ export default class Work {
(this.#document.querySelector( (this.#document.querySelector(
"dd.character > ul.commas", "dd.character > ul.commas",
) as Element) ) as Element)
.children, ?.children ?? [],
).map( ).map(
(t) => this.tags.push(t.children[0].innerText), (t) => this.tags.push(t.children[0].innerText),
); );
Array.from( Array.from(
(this.#document.querySelector("dd.freeform > ul.commas") as Element) (this.#document.querySelector("dd.freeform > ul.commas") as Element)
.children, ?.children ?? [], //does that make me insane
).map( ).map(
(t) => this.tags.push(t.children[0].innerText), (t) => this.tags.push(t.children[0].innerText),
); );
@ -109,7 +109,7 @@ export default class Work {
//CW: horrifying jank //CW: horrifying jank
async populateChapters() { async populateChapters() {
let firstChapterUrl = "" //satisfy the typescript gods let firstChapterUrl = ""; //satisfy the typescript gods
try { try {
firstChapterUrl = firstChapterUrl =
(this.#document.querySelector("li.chapter > a") as Element) (this.#document.querySelector("li.chapter > a") as Element)
@ -127,7 +127,7 @@ export default class Work {
this.chapters.push( this.chapters.push(
newChapter, newChapter,
); );
return return;
} }
const res = await this.#session.get(firstChapterUrl); const res = await this.#session.get(firstChapterUrl);
const document = this.#DOMParser.parseFromString( const document = this.#DOMParser.parseFromString(

@ -4,12 +4,12 @@ import { assert } from "https://deno.land/std@0.161.0/testing/asserts.ts";
const ao3 = new AO3(); const ao3 = new AO3();
const res = await ao3.search({ const res = await ao3.search({
any: "hazbin hotel" any: "hazbin hotel",
}) });
await res.update(1) await res.update(1);
console.log(res.results[0]) console.log(res.results[0]);
/* /*
let work = await ao3.getWork("37522864"); let work = await ao3.getWork("37522864");

Loading…
Cancel
Save