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

master
Drake 3 months ago
parent 8e68139215
commit 50edb2df60

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

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

Loading…
Cancel
Save