Finish Danbooru loader + small tweaks

feature-show_image_resolution
neobooru 2019-08-04 21:43:08 +02:00
parent 2776b48f2b
commit 817f9a7ef2
5 changed files with 73 additions and 6 deletions

View File

@ -20,7 +20,7 @@ export class SzuruTag {
category?: SzuruCategory; category?: SzuruCategory;
constructor(name: string, category?: SzuruCategory) { constructor(name: string, category?: SzuruCategory) {
this.name = name; this.name = name.replace(/\s/g, "_"); // Replace all spaces with underscores
this.category = category; this.category = category;
} }
} }

View File

@ -48,6 +48,10 @@ select {
display: inline; display: inline;
} }
.tag-general {
color: #24aadd;
}
.tag-copyright { .tag-copyright {
color: rgb(170, 0, 170); color: rgb(170, 0, 170);
} }

View File

@ -11,11 +11,72 @@ export default class Danbooru implements ILoader {
async grabPost(dom: Document): Promise<SzuruPost | null> { async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost(); let post = new SzuruPost();
post.source = dom.location.href; post.source = dom.location.href;
// Get image url // Set image url
const originalImageElements = Array.from(dom.querySelectorAll("#post-information > ul > li > a"))
.map(x => x as HTMLAnchorElement)
.filter(x => x.parentElement && x.parentElement.innerText.startsWith("Size"));
if (originalImageElements.length > 0) {
post.imageUrl = originalImageElements[0].href;
}
// Set safety
const safetyExp = new RegExp("Rating: (.*)");
const safetyElements = Array.from(document.querySelectorAll("#post-information > 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-list > ul > li")).map(x => x as HTMLLIElement);
for (const tagElement of tagElements) {
let tagName: string | undefined;
const tagNameElement = tagElement.querySelector(".search-tag") as HTMLAnchorElement;
if (tagNameElement) {
tagName = tagNameElement.innerText;
}
let category: SzuruCategory | undefined;
switch (tagElement.className) {
case "category-3":
category = SzuruCategory.Copyright;
break;
case "category-4":
category = SzuruCategory.Character;
break;
case "category-1":
category = SzuruCategory.Artist;
break;
case "category-5":
category = SzuruCategory.Meta;
break;
}
if (tagName) {
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 has enough filled fields to be considered a valid post
if (post.imageUrl) { if (post.imageUrl) {
@ -24,4 +85,4 @@ export default class Danbooru implements ILoader {
return null; return null;
} }
} }
} }

View File

@ -92,11 +92,11 @@ export default class Gelbooru implements ILoader {
let tagName: string; let tagName: string;
if (isSafebooru) { if (isSafebooru) {
tagName = el.getElementsByTagName("a")[0].innerText.replace(/\s/g, "_"); tagName = el.getElementsByTagName("a")[0].innerText;
} else { } else {
// First <a> is "?" // First <a> is "?"
// Second <a> is "actual_tag_name" // Second <a> is "actual_tag_name"
tagName = el.getElementsByTagName("a")[1].innerText.replace(/\s/g, "_"); tagName = el.getElementsByTagName("a")[1].innerText;
} }
let category: SzuruCategory | undefined; let category: SzuruCategory | undefined;

View File

@ -171,6 +171,8 @@ export default Vue.extend({
if (tag.category) { if (tag.category) {
classes.push("tag-" + tag.category.name); classes.push("tag-" + tag.category.name);
} else {
classes.push("tag-general");
} }
return classes; return classes;