From f0a9e84e85d4cfa6df9ba7a0f31f13aba93a9def Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Mon, 5 Aug 2019 01:42:33 +0200 Subject: [PATCH] Add konachan and yande.re (Moebooru) --- src/loaders/Moebooru.ts | 97 +++++++++++++++++++++++++++++++++++++++++ src/loaders/index.ts | 1 + 2 files changed, 98 insertions(+) create mode 100644 src/loaders/Moebooru.ts diff --git a/src/loaders/Moebooru.ts b/src/loaders/Moebooru.ts new file mode 100644 index 0000000..908b3ca --- /dev/null +++ b/src/loaders/Moebooru.ts @@ -0,0 +1,97 @@ +import ILoader from "./ILoader" +import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes"; + +export default class Moebooru implements ILoader { + canImport(location: Location): boolean { + return ( + location.host == "yande.re" || + location.host == "konachan.com" + ); + } + + async grabPost(dom: Document): Promise { + let post = new SzuruPost(); + post.source = dom.location.href; + + // Set image url + const originalImageElement = document.querySelector("#highres") as HTMLAnchorElement; + if (originalImageElement) { + post.imageUrl = originalImageElement.href; + } + + // Set safety + // Same method as Gelbooru but cleaner code + const safetyExp = new RegExp("Rating: (.*)"); + const safetyElements = Array.from(document.querySelectorAll("#stats > ul > li")) + .map(x => x as HTMLLIElement) + .filter(x => x.innerText.startsWith("Rating:")); + + if (safetyElements.length > 0) { + const matches = safetyElements[0].innerText.match(safetyExp); + if (matches && matches.length > 0) { + switch (matches[1].toLowerCase()) { + case "safe": + post.safety = "safe"; + break; + case "questionable": + post.safety = "sketchy"; + break; + case "explicit": + post.safety = "unsafe"; + break; + } + } + } + + // Set tags + const tagElements = Array.from(dom.querySelectorAll("#tag-sidebar > li")) + .map(x => x as HTMLLIElement); + + for (const el of tagElements) { + let tagName: string; + let tagNameElements = el.getElementsByTagName("a"); + + if (tagNameElements.length > 1) { + // First is "?" + // Second is "actual_tag_name" + tagName = tagNameElements[1].innerText; + } else { + // Skip if no tag name is found + continue; + } + + let category: SzuruCategory | undefined; + + // Loop over all classes because some sites (konachan) + // have mutliple classes, e.g. "tag-link tag-type-copyright" + for (const className of Array.from(el.classList)) { + switch (className) { + case "tag-type-copyright": + category = SzuruCategory.Copyright; + break; + case "tag-type-character": + category = SzuruCategory.Character; + break; + case "tag-type-artist": + category = SzuruCategory.Artist; + break; + case "tag-type-metadata": + category = SzuruCategory.Meta; + break; + } + } + + let tag = new SzuruTag(tagName, category); + post.tags.push(tag); + } + + // console.dir(post); + + // If post has enough filled fields to be considered a valid post + if (post.imageUrl) { + return post; + } else { + return null; + } + } +} diff --git a/src/loaders/index.ts b/src/loaders/index.ts index bc956f2..8c816c2 100644 --- a/src/loaders/index.ts +++ b/src/loaders/index.ts @@ -1,3 +1,4 @@ export { default as Gelbooru } from "./Gelbooru"; export { default as Danbooru } from "./Danbooru"; export { default as Shimmie2 } from "./Shimmie2"; +export { default as Moebooru } from "./Moebooru";