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)".
pull/15/head
GlobalGit3 2023-07-02 01:49:40 -05:00
parent 580e308f8f
commit 8d215729f3
1 changed files with 29 additions and 5 deletions

View File

@ -14,6 +14,9 @@ export default class Philomena extends ScrapeEngineBase {
"manebooru.art", "manebooru.art",
"twibooru.org", "twibooru.org",
]; ];
removePrefixes = ["artist", "editor", "character", "species", "oc", "ship"];
removeprefixesSuffix = new Map([["oc", "_(oc)"]]);
artistPrefixes = ["artist", "editor"];
safetyMap = new Map<string, SafetyRating>([ safetyMap = new Map<string, SafetyRating>([
["safe", "safe"], ["safe", "safe"],
["suggestive", "sketchy"], ["suggestive", "sketchy"],
@ -24,6 +27,7 @@ export default class Philomena extends ScrapeEngineBase {
["character", "character"], ["character", "character"],
["species", "species"], ["species", "species"],
["oc", "character"], ["oc", "character"],
["content-official", "copyright"],
]); ]);
scrapeDocument(document: Document): ScrapeResult { scrapeDocument(document: Document): ScrapeResult {
@ -60,12 +64,8 @@ export default class Philomena extends ScrapeEngineBase {
} }
let category: TagCategory | undefined; 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"); const dataTagCategory = el.getAttribute("data-tag-category");
if (dataTagCategory) { if (dataTagCategory) {
const mappedCategory = this.categoryMap.get(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) { if (tagName) {
const tag = new ScrapedTag(tagName, category); const tag = new ScrapedTag(tagName, category);
post.tags.push(tag); post.tags.push(tag);