// I don't ever run node scripts by themselves, but I'm pretty sure you can just node script.js
//
// Expects node-fetch to be downloaded
// get it with npm install node-fetch
//
// Expects two folders, ./in/ and ./out/
// ./in/ is a folder full of images saved from e621 with the hashed filenames intact
// ./out/ will have the parsed files
//
// Expects "tags.csv", you can download yiffy's tag list from https://rentry.org/sdmodels#yiffy-e18ckpt-50ad914b (Tag counts)
//
// I haven't tested this as a standalone, only with it being an endpoint for my server
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);
}
let filters = [
"female"
]
for ( let i in files ) {
let file = files[i];
let md5 = file.match(/^([a-f0-9]{32})/)[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 res.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 > 164 ) break;
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)
} )
}