diff --git a/package.json b/package.json index fd58655..0e958a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "neo-scraper", - "version": "0.4.1", + "version": "0.4.2", "description": "Scrape a booru-like post object from the DOM.", "main": "dist/index.js", "types": "dist/index.d.js", diff --git a/src/NeoScraper.ts b/src/NeoScraper.ts index ebc5f8b..16035e9 100644 --- a/src/NeoScraper.ts +++ b/src/NeoScraper.ts @@ -12,6 +12,7 @@ export default class NeoScraper { new engines.Danbooru(), new engines.e621(), new engines.Gelbooru(), + new engines.Inkbunny(), new engines.Moebooru(), new engines.Pixiv(), new engines.Reddit(), diff --git a/src/engines/Inkbunny.ts b/src/engines/Inkbunny.ts new file mode 100644 index 0000000..0e10de8 --- /dev/null +++ b/src/engines/Inkbunny.ts @@ -0,0 +1,73 @@ +import { ScrapeEngine, ScrapeResult, ScrapedPost, ScrapedTag } from "../ScrapeEngine"; + +export default class Inkbunny implements ScrapeEngine { + name = "inkbunny"; + + canImport(url: Location): boolean { + return url.host == "inkbunny.net"; + } + + scrapeDocument(document: Document): ScrapeResult { + let result = new ScrapeResult(this.name); + + let post = new ScrapedPost(); + post.pageUrl = document.location.href; + + // Set image url + const downloadEl = document.querySelector("a[download]") as HTMLAnchorElement; + + if (downloadEl) { + post.contentUrl = downloadEl.href; + } + + // Sometimes the HTML structure is different for whatever reason? + if (!post.contentUrl) { + const imgEl = document.querySelector(".widget_imageFromSubmission > img") as HTMLImageElement; + if (imgEl) { + post.contentUrl = imgEl.src; + } + } + + // Return early if we didn't find an image. + if (!post.contentUrl) { + return result; + } + + // Set rating + const ratingExp = new RegExp("Rating: (.*)"); + const spanEls = Array.from(document.querySelectorAll("div > span")); + const ratingEls = spanEls.map((x) => x as HTMLDivElement).filter((x) => x.innerText.indexOf("Rating:") != -1); + + if (ratingEls.length > 0) { + const matches = ratingEls[0].parentElement!.innerText.match(ratingExp); + if (matches && matches.length > 0) { + switch (matches[1].toLowerCase()) { + case "general": + post.rating = "safe"; + break; + case "mature": + case "adult": + post.rating = "unsafe"; + break; + } + } + } + + // Set tags + const metaKeywords = document.querySelector("meta[name='keywords']") as HTMLMetaElement; + const keywordsContent = metaKeywords.getAttribute("content"); + + if (keywordsContent) { + const keywords = keywordsContent.split(",").map((x) => x.trim()); + + for (const keyword of keywords) { + let tag = new ScrapedTag(keyword); + post.tags.push(tag); + } + } + + result.tryAddPost(post); + + return result; + } +} diff --git a/src/engines/e621.ts b/src/engines/e621.ts index 6a901b5..d2bd066 100644 --- a/src/engines/e621.ts +++ b/src/engines/e621.ts @@ -12,7 +12,7 @@ export default class e621 implements ScrapeEngine { let result = new ScrapeResult(this.name); let post = new ScrapedPost(); - post.source = document.location.href; + post.pageUrl = document.location.href; // Set image url const downloadEl = document.querySelector("#image-download-link > a") as HTMLAnchorElement; diff --git a/src/engines/index.ts b/src/engines/index.ts index bcf0b9f..a67f569 100644 --- a/src/engines/index.ts +++ b/src/engines/index.ts @@ -3,6 +3,7 @@ export { default as Danbooru } from "./Danbooru"; export { default as e621 } from "./e621"; export { default as Fallback } from "./Fallback"; export { default as Gelbooru } from "./Gelbooru"; +export { default as Inkbunny } from "./Inkbunny"; export { default as Moebooru } from "./Moebooru"; export { default as Pixiv } from "./Pixiv"; export { default as Reddit } from "./Reddit";