Finish "import tag category" feature

Well the basics of it anyway, there are no options to configure yet so it'll always set the tag to whatever category the source website wants it to be.
feature-show_image_resolution
neobooru 2019-09-01 12:53:07 +02:00
parent 9019bb3c32
commit 6ca334d034
3 changed files with 38 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{
"name": "szuru-chrome",
"version": "0.1.0",
"version": "0.1.1",
"description": "Import media from various sites into szurubooru",
"author": "neobooru",
"license": "MIT",

View File

@ -1,7 +1,7 @@
import axios, { AxiosRequestConfig } from 'axios';
import { LocalPost, LocalError } from './LocalTypes';
import { SzuruSiteConfig } from './Config';
import { TagsResult, TagCategoriesResult, Post } from './SzuruTypes';
import { TagsResult, TagCategoriesResult, Post, Tag } from './SzuruTypes';
/**
* A 1:1 wrapper around the szurubooru API.
@ -36,8 +36,16 @@ export default class SzuruWrapper {
return (await this.apiGet("info")).data;
}
async getTags(): Promise<TagsResult> {
return (await this.apiGet("tags")).data;
async updateTag(tag: Tag): Promise<any> {
return (await this.apiPut("tag/" + tag.names[0], tag)).data;
}
async getTags(query: string): Promise<TagsResult> {
let url = "tags";
if (query) {
url += query;
}
return (await this.apiGet(url)).data;
}
async getTagCategories(): Promise<TagCategoriesResult> {
@ -81,6 +89,18 @@ export default class SzuruWrapper {
return await this.execute(config);
}
private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
const fullUrl = this.apiUrl + url;
const config: AxiosRequestConfig = {
method: "PUT",
url: fullUrl,
data: data
}
config.headers = { ...this.baseHeaders, ...additionalHeaders };
return await this.execute(config);
}
private async execute(config: AxiosRequestConfig): Promise<any> {
if (this.username && this.authToken) {
const token = "Token " + btoa(`${this.username}:${this.authToken}`);

View File

@ -155,9 +155,6 @@ export default Vue.extend({
// see https://stackoverflow.com/questions/8915845
uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}'>Uploaded post</a>`;
let tagsMsg = new Message("Checking tags...");
this.messages.push(tagsMsg);
// Find tags with "default" category and update it
// TODO: Make all these categories configurable
const tagsWithCategory = this.post.tags.filter(x => x.category);
@ -165,14 +162,21 @@ export default Vue.extend({
.filter(x => x.category == "default")
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
console.dir(unsetCategoryTags);
if (unsetCategoryTags.length != 0) {
let tagsMsg = new Message(`${unsetCategoryTags.length} tags need a different category`);
this.messages.push(tagsMsg);
if (unsetCategoryTags.length == 0) {
tagsMsg.content = "All tags have the correct category";
} else {
tagsMsg.content = `${unsetCategoryTags.length} tags need a different category`;
// TODO: Call /tags?query={tag1},{tag2},{tag3}
// TODO: Foreach tag call PUT /tag/{tag1} with updated category
// unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those
const query = "?query=" + unsetCategoryTags.map(x => encodeURIComponent(x.names[0]));
const tags = (await this.szuru.getTags(query)).results;
for (let i in tags) {
tagsMsg.content = `Updating tag ${i}/${unsetCategoryTags.length}`;
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!
await this.szuru.updateTag(tags[i]);
}
tagsMsg.content = "Updated all tags";
}
} catch (ex) {
const error = ex as LocalError;