Skip to content

Commit

Permalink
Add support for tag types in szurubooru script (fix #2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Aug 22, 2020
1 parent 57e933f commit 6ca5dc1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
58 changes: 54 additions & 4 deletions docs/_docs/commands/szurubooru.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
// Usage:
// node szurubooru.js "username" "token" "tag1 tag2" "safe" "http://source" "path/to/file.jpg"
// node szurubooru.js "username" "token" "tagType1:tag1 tagType2:tag2" "safe" "http://source" "path/to/file.jpg"

const axios = require("axios");
const fs = require("fs");
const FormData = require("form-data");

async function getTag(name) {
try {
const res = await axios.get(`/tag/${name}`);
return res.data;
} catch (e) {
if (e.response.status === 404) {
return null;
}
throw e;
}
}
async function createTag(name, category) {
try {
const data = { names: [name], category };
await axios.post("/tags", data);
console.log(`Tag "${name}" created`);
} catch (e) {
console.error("Error creating tag: " + e.message);
console.error(e.response.data);
}
}
async function updateTag(name, version, category) {
try {
const data = { version, category };
await axios.put(`/tag/${name}`, data);
console.log(`Tag "${name}" updated`);
} catch (e) {
console.error("Error updating tag: " + e.message);
console.error(e.response.data);
}
}
async function setTagCategory(name, category) {
const tag = await getTag(name);
if (tag === null) {
createTag(name, category);
} else if (tag.category !== category) {
updateTag(name, tag.version, category);
}
}

(async () => {
// Szurubooru doesn't use the same ratings as most boorus so we need to map them
const ratingsMap = {
Expand All @@ -23,11 +63,21 @@ const FormData = require("form-data");
axios.defaults.headers.common["Authorization"] = "Token " + Buffer.from(username + ":" + token).toString("base64");
axios.defaults.headers.common["Accept"] = "application/json";

// Parse tags and update categories
const tags = argv[0].split(" ");
for (i = 0; i < tags.length; ++i) {
const parts = tags[i].split(":");
const category = parts.shift();
const name = parts.join(":");
setTagCategory(name, category);
tags[i] = name;
}

// Actual data to send to the server
const data = {
"tags": argv[0].split(" "),
"safety": ratingsMap[argv[1]],
"source": argv[2] || undefined,
tags,
safety: ratingsMap[argv[1]],
source: argv[2] || undefined,
};

// Create a multipart form data request
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/commands/szurubooru.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ npm root -g

Open Grabber, then go to "Options > Commands", and set the "Image" field to:
```
node szurubooru.js "YOUR_USERNAME" "YOUR_TOKEN" "%all%" "%rating%" "%source:unsafe%" "%path%"
node szurubooru.js "YOUR_USERNAME" "YOUR_TOKEN" "%all:includenamespace,unsafe%" "%rating%" "%source:unsafe%" "%path%"
```

Make sure to replace `YOUR_USERNAME` by your Szurubooru username, and `YOUR_TOKEN` by the token created earlier (in the `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` format or similar).

0 comments on commit 6ca5dc1

Please sign in to comment.