stable-diffusion-utils/utils/renamer/preprocess.js
2022-10-04 21:25:47 +00:00

89 lines
2.4 KiB
JavaScript
Executable File

let FS = require("fs")
let Fetch = require("node-fetch")
let files = FS.readdirSync(`./in/`);
let kson = {
files: {},
tags: {}
};
let csv = FS.readFileSync(`./tags.csv`)
csv = csv.toString().split("\n")
for ( let i in csv ) {
let [k, v] = csv[i].split(",")
kson.tags[k] = parseInt(v);
}
// fill it with tags of whatever you don't want to make it into the filename
// for starters, you can also add "anthro", "male", "female", as they're very common tags
let filters = [
"female"
];
let parse = async () => {
for ( let i in files ) {
let file = files[i];
let md5 = file.match(/^([a-f0-9]{32})/);
if ( !md5 ) continue;
md5 = md5[1];
let ext = file.split(".").pop()
console.log(i, files.length, md5, ext);
let r = await Fetch( `https://e621.net/posts.json?tags=md5:${md5}`, {
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
} );
let json = JSON.parse(await r.text());
json = json.posts[0];
if ( !json ) continue;
tags = [];
let artist = "";
let content = "";
switch ( json.rating ) {
case "s": content = "safe content"; break;
case "q": content = "questionable content"; break;
case "e": content = "explict content"; break;
}
for ( let cat in json.tags ) {
if ( cat === "artist" ) {
let tag = "by " + json.tags["artist"].join(" and ")
if ( !kson.tags[tag] ) continue;
artist = tag;
} else for ( let k in json.tags[cat] ) {
let tag = json.tags[cat][k];
if ( !kson.tags[tag] ) continue;
if ( tag.indexOf("/") >= 0 ) continue;
if ( filters.includes(tag) ) continue;
tags.push(tag);
}
}
tags = tags.sort( (a, b) => {
return kson.tags[b] - kson.tags[a]
})
if ( artist ) tags.unshift(artist);
if ( content ) tags.unshift(content);
kson.files[md5] = tags;
let jointmp = "";
let filtered = [];
for ( let i in tags ) {
if ( (jointmp + " " + tags[i]).length > 192 ) break; // arbitrary limit for filenames and tokens, adjust if you're getting bitched at from the web UI
jointmp += " " + tags[i];
filtered.push(tags[i])
}
let joined = filtered.join(" ")
require("fs").copyFileSync(`./in/${file}`, `./out/${joined}.${ext}`)
// rate limit
await new Promise( (resolve) => {
setTimeout(resolve, 500)
} )
}
}
parse();