Remove safety tags from list and add categories
parent
4c0c790c0c
commit
580e308f8f
|
|
@ -4,7 +4,8 @@ test("get derpibooru.org post 3153524", async () => {
|
||||||
const pageUrl = "https://derpibooru.org/images/3153524";
|
const pageUrl = "https://derpibooru.org/images/3153524";
|
||||||
const res = await scrapeUrl(pageUrl);
|
const res = await scrapeUrl(pageUrl);
|
||||||
expect(res.posts.length).toBe(1);
|
expect(res.posts.length).toBe(1);
|
||||||
expect(res.posts[0]).toHaveTag("safe");
|
expect(res.posts[0].rating).toBe("safe");
|
||||||
|
expect(res.posts[0]).not.toHaveTag("safe");
|
||||||
expect(res.posts[0]).toHaveTag("yay");
|
expect(res.posts[0]).toHaveTag("yay");
|
||||||
expect(res.posts[0]).toHaveTag("flutteryay");
|
expect(res.posts[0]).toHaveTag("flutteryay");
|
||||||
expect(res.posts[0]).toHaveSource("https://twitter.com/Confetti_Cakez/status/1674145455666544640");
|
expect(res.posts[0]).toHaveSource("https://twitter.com/Confetti_Cakez/status/1674145455666544640");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { ScrapeEngineBase, ScrapeResult, ScrapedPost, ScrapedTag, ScrapeEngineFeature } from "../ScrapeEngine.js";
|
import { ScrapeEngineBase, ScrapeResult, ScrapedPost, ScrapedTag, ScrapeEngineFeature } from "../ScrapeEngine.js";
|
||||||
import { TagCategory } from "../BooruTypes.js";
|
import { SafetyRating, TagCategory } from "../BooruTypes.js";
|
||||||
|
|
||||||
export default class Philomena extends ScrapeEngineBase {
|
export default class Philomena extends ScrapeEngineBase {
|
||||||
name = "Philomena";
|
name = "Philomena";
|
||||||
|
|
@ -14,6 +14,17 @@ export default class Philomena extends ScrapeEngineBase {
|
||||||
"manebooru.art",
|
"manebooru.art",
|
||||||
"twibooru.org",
|
"twibooru.org",
|
||||||
];
|
];
|
||||||
|
safetyMap = new Map<string, SafetyRating>([
|
||||||
|
["safe", "safe"],
|
||||||
|
["suggestive", "sketchy"],
|
||||||
|
["questionable", "sketchy"],
|
||||||
|
["explicit", "unsafe"],
|
||||||
|
]);
|
||||||
|
categoryMap = new Map<string, TagCategory>([
|
||||||
|
["character", "character"],
|
||||||
|
["species", "species"],
|
||||||
|
["oc", "character"],
|
||||||
|
]);
|
||||||
|
|
||||||
scrapeDocument(document: Document): ScrapeResult {
|
scrapeDocument(document: Document): ScrapeResult {
|
||||||
const result = new ScrapeResult(this.name);
|
const result = new ScrapeResult(this.name);
|
||||||
|
|
@ -21,9 +32,7 @@ export default class Philomena extends ScrapeEngineBase {
|
||||||
post.pageUrl = document.location.href;
|
post.pageUrl = document.location.href;
|
||||||
|
|
||||||
// Set image url
|
// Set image url
|
||||||
const downloadEl = document.querySelector(
|
const downloadEl = document.querySelector("a[title='Download (no tags in filename)']") as HTMLAnchorElement;
|
||||||
"div[id^='image_meta_'] > div:nth-child(4) > a:nth-child(4)"
|
|
||||||
) as HTMLAnchorElement;
|
|
||||||
post.contentUrl = downloadEl?.href;
|
post.contentUrl = downloadEl?.href;
|
||||||
|
|
||||||
// Set content type
|
// Set content type
|
||||||
|
|
@ -35,26 +44,34 @@ export default class Philomena extends ScrapeEngineBase {
|
||||||
post.rating = "unsafe";
|
post.rating = "unsafe";
|
||||||
|
|
||||||
// Set tags
|
// Set tags
|
||||||
const tagEls = Array.from(document.querySelectorAll("a.tag__name")).map((x) => x as HTMLLIElement);
|
const tagEls = Array.from(document.querySelectorAll(".tag.dropdown")).map((x) => x as HTMLSpanElement);
|
||||||
|
|
||||||
for (const el of tagEls) {
|
for (const el of tagEls) {
|
||||||
const tagName = el.innerText;
|
let tagName = el.getAttribute("data-tag-name");
|
||||||
let category: TagCategory | undefined;
|
if (!tagName) continue;
|
||||||
|
|
||||||
// Set rating (philomena uses tags for this)
|
// Set rating (philomena uses tags for this)
|
||||||
switch (tagName) {
|
// These tags also have `data-tag-category="rating"` which we could use.
|
||||||
case "safe":
|
const safety = this.safetyMap.get(tagName);
|
||||||
post.rating = "safe";
|
if (safety != undefined) {
|
||||||
break;
|
post.rating = safety;
|
||||||
case "suggestive":
|
// Don't add this as a tag.
|
||||||
post.rating = "sketchy";
|
continue;
|
||||||
break;
|
}
|
||||||
case "questionable":
|
|
||||||
post.rating = "sketchy";
|
let category: TagCategory | undefined;
|
||||||
break;
|
const artistMatches = tagName.match("artist:(.*)");
|
||||||
case "explicit":
|
if (artistMatches && artistMatches.length == 2) {
|
||||||
post.rating = "unsafe";
|
tagName = artistMatches[1];
|
||||||
break;
|
category = "artist";
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataTagCategory = el.getAttribute("data-tag-category");
|
||||||
|
if (dataTagCategory) {
|
||||||
|
const mappedCategory = this.categoryMap.get(dataTagCategory);
|
||||||
|
if (mappedCategory) {
|
||||||
|
category = mappedCategory;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tagName) {
|
if (tagName) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue