From fe1b8be5190d9f3269368d65bd5d2aa20e7c1a04 Mon Sep 17 00:00:00 2001 From: Ruthenic Date: Sun, 30 Oct 2022 04:54:40 -0400 Subject: [PATCH] initial work --- .vscode/settings.json | 4 ++++ src/classes/AO3.ts | 18 ++++++++++++++++++ src/classes/Chapter.ts | 4 ++++ src/classes/Work.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/classes/index.ts | 0 src/global.d.ts | 1 + test.ts | 14 ++++++++++++++ 7 files changed, 82 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 src/classes/AO3.ts create mode 100644 src/classes/Chapter.ts create mode 100644 src/classes/Work.ts create mode 100644 src/classes/index.ts create mode 100644 src/global.d.ts create mode 100644 test.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8675ad5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "deno.enable": true, + "deno.unstable": true +} \ No newline at end of file diff --git a/src/classes/AO3.ts b/src/classes/AO3.ts new file mode 100644 index 0000000..b68e907 --- /dev/null +++ b/src/classes/AO3.ts @@ -0,0 +1,18 @@ +import { IAxiodResponse } from "https://deno.land/x/axiod@0.26.2/interfaces.ts"; +import axiod from "https://deno.land/x/axiod@0.26.2/mod.ts"; +import Work from "./Work.ts"; + +export default class AO3 { + session: typeof axiod + + constructor(opts?: Record) { + this.session = axiod.create({ + baseURL: opts?.url ?? "https://archiveofourown.org/" + }) + } + + async getWork(id: ID) { + const res = await this.session.get(`/works/${id}?view_adult=true&view_full_work=true`) + return new Work(id, res.data, this.session) + } +} \ No newline at end of file diff --git a/src/classes/Chapter.ts b/src/classes/Chapter.ts new file mode 100644 index 0000000..7b9a198 --- /dev/null +++ b/src/classes/Chapter.ts @@ -0,0 +1,4 @@ +export default class Chapter { + constructor(workId: ID, id: ID) { + } +} \ No newline at end of file diff --git a/src/classes/Work.ts b/src/classes/Work.ts new file mode 100644 index 0000000..cf31272 --- /dev/null +++ b/src/classes/Work.ts @@ -0,0 +1,41 @@ +import axiod from "https://deno.land/x/axiod@0.26.2/mod.ts" +import Chapter from "./Chapter.ts" +import { DOMParser, Element, HTMLDocument } from "https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts"; + +export default class Work { + session: typeof axiod + id: ID + document: HTMLDocument + chapters: Chapter[] = [] + tags: string[] = [] + published?: Date + updated?: Date + + constructor(id: ID, body: string, session: typeof axiod) { + this.session = session + this.id = id + this.document = new DOMParser().parseFromString(body, "text/html") as HTMLDocument + this.populateTags() + this.populateDates() + } + + populateTags() { + Array.from((this.document.querySelector("dd.fandom > ul.commas") as Element).children).map( + t => this.tags.push(t.children[0].innerText) + ) + Array.from((this.document.querySelector("dd.relationship > ul.commas") as Element).children).map( + t => this.tags.push(t.children[0].innerText) + ) + Array.from((this.document.querySelector("dd.character > ul.commas") as Element).children).map( + t => this.tags.push(t.children[0].innerText) + ) + Array.from((this.document.querySelector("dd.freeform > ul.commas") as Element).children).map( + t => this.tags.push(t.children[0].innerText) + ) + } + + populateDates() { + this.published = new Date(this.document.querySelector("dd.published")?.innerText as string) + this.updated = new Date(this.document.querySelector("dd.status")?.innerText as string) + } +} \ No newline at end of file diff --git a/src/classes/index.ts b/src/classes/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..e3d664d --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1 @@ +type ID = BigInt | number | string \ No newline at end of file diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..7ba0dbf --- /dev/null +++ b/test.ts @@ -0,0 +1,14 @@ +import AO3 from "./src/classes/AO3.ts"; +import { assert } from "https://deno.land/std@0.161.0/testing/asserts.ts"; + +const ao3 = new AO3(); + +const work = await ao3.getWork(37522864) + +assert(work.tags.length > 0) +assert(work.tags.includes("Attempted Murder")) +console.log(`TAGS: ${work.tags}`) +assert(work.published instanceof Date) +console.log(`PUBLISHED: ${work.published}`) +assert(work.updated instanceof Date) +console.log(`UPDATED: ${work.updated}`) \ No newline at end of file