From 8d215729f318dfff49315a496ba64d14c4d79cae Mon Sep 17 00:00:00 2001 From: GlobalGit3 <137680273+GlobalGit3@users.noreply.github.com> Date: Sun, 2 Jul 2023 01:49:40 -0500 Subject: [PATCH] Map and Arrays for prefixes Categorize tags and remove prefixes using Maps and Arrays. A quick write-up, but works for the time being to get an idea for what is wanted. Due to the data-tag-category's 'origin' being used for more than artist's names, prefixes are used to categorize the artist tag. There are also other prefixes such as 'editor' that are the same as 'artist'. This should be no problem as all artists' tags start with the 'artist' or 'editor' prefix. Suffixes only applies when removing a prefix, this will probably be changed later to apply without removing the prefix. It was coded to give an idea of wanting to change tagNames from "oc:originalcharacter" to "originalcharacter_(oc)". --- src/engines/Philomena.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/engines/Philomena.ts b/src/engines/Philomena.ts index c79f684..53e8c84 100644 --- a/src/engines/Philomena.ts +++ b/src/engines/Philomena.ts @@ -14,6 +14,9 @@ export default class Philomena extends ScrapeEngineBase { "manebooru.art", "twibooru.org", ]; + removePrefixes = ["artist", "editor", "character", "species", "oc", "ship"]; + removeprefixesSuffix = new Map([["oc", "_(oc)"]]); + artistPrefixes = ["artist", "editor"]; safetyMap = new Map([ ["safe", "safe"], ["suggestive", "sketchy"], @@ -24,6 +27,7 @@ export default class Philomena extends ScrapeEngineBase { ["character", "character"], ["species", "species"], ["oc", "character"], + ["content-official", "copyright"], ]); scrapeDocument(document: Document): ScrapeResult { @@ -60,12 +64,8 @@ export default class Philomena extends ScrapeEngineBase { } let category: TagCategory | undefined; - const artistMatches = tagName.match("artist:(.*)"); - if (artistMatches && artistMatches.length == 2) { - tagName = artistMatches[1]; - category = "artist"; - } + // Categorize tags using data-tag-category const dataTagCategory = el.getAttribute("data-tag-category"); if (dataTagCategory) { const mappedCategory = this.categoryMap.get(dataTagCategory); @@ -74,6 +74,30 @@ export default class Philomena extends ScrapeEngineBase { } } + // Artist tag categorize using prefixes + for (const prefix of this.artistPrefixes) { + if (tagName.includes(prefix + ":")) { + category = "artist"; + break; + } + } + + // Remove listed prefixes (removePrefixes) from tagName + for (const prefix of this.removePrefixes) { + if (tagName.includes(prefix + ":")) { + tagName = tagName.replace(prefix + ":", "").trim(); + + // Add suffix if configured + const suffix = this.removeprefixesSuffix.get(prefix); + if (suffix) { + tagName = tagName + suffix; + } + + break; + } + } + + // Push tag if (tagName) { const tag = new ScrapedTag(tagName, category); post.tags.push(tag);