1
0
Fork 0

Using the booru library now pulls from three (3)! boorus to display more _unique_ image selection

This commit is contained in:
grumbulon 2022-04-04 20:31:06 -04:00
parent 3140a84b9c
commit 2e78190d43
5 changed files with 134 additions and 51 deletions

View file

@ -1,5 +1,5 @@
{
"booru": "danbooru",
"tags": ["iwakura_lain"],
"booru": ["danbooru", "safebooru", "konan"],
"tags": ["onodera_kosaki", "-underwear"],
"rating": "safe"
}

120
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"booru": "^2.5.4",
"express": "^4.17.3",
"got": "^12.0.3",
"node": "^17.7.2"
@ -126,6 +127,18 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/booru": {
"version": "2.5.4",
"resolved": "https://registry.npmjs.org/booru/-/booru-2.5.4.tgz",
"integrity": "sha512-gxIVMn3LWEmpxlaw08VhuoR17Ym+NLAIWuE9ThDjjBbAuUqw73q0VmXm5R0etzmVqm+wctORrveNqZAQ/Pz6sQ==",
"dependencies": {
"fast-xml-parser": "^3.19.0",
"node-fetch": "^2.6.1"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@ -386,6 +399,21 @@
"node": ">= 0.8.0"
}
},
"node_modules/fast-xml-parser": {
"version": "3.21.1",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-3.21.1.tgz",
"integrity": "sha512-FTFVjYoBOZTJekiUsawGsSYV9QL0A+zDYCRj7y34IO6Jg+2IMYEtQa+bbictpdpV8dHxXywqU7C0gRDEOFtBFg==",
"dependencies": {
"strnum": "^1.0.4"
},
"bin": {
"xml2js": "cli.js"
},
"funding": {
"type": "paypal",
"url": "https://paypal.me/naturalintelligence"
}
},
"node_modules/finalhandler": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
@ -634,6 +662,25 @@
"resolved": "https://registry.npmjs.org/node-bin-setup/-/node-bin-setup-1.1.0.tgz",
"integrity": "sha512-pTeU6NgUrexiLNtd+AKwvg6cngHMvj5FZ5e2bbv2ogBSIc9yhkXSSaTScfSRZnwHIh5YFmYSYlemLWkiKD7rog=="
},
"node_modules/node-fetch": {
"version": "2.6.7",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/normalize-url": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz",
@ -825,6 +872,11 @@
"node": ">= 0.6"
}
},
"node_modules/strnum": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/strnum/-/strnum-1.0.5.tgz",
"integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA=="
},
"node_modules/toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
@ -833,6 +885,11 @@
"node": ">=0.6"
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
},
"node_modules/type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
@ -869,6 +926,20 @@
"node": ">= 0.8"
}
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
@ -969,6 +1040,15 @@
}
}
},
"booru": {
"version": "2.5.4",
"resolved": "https://registry.npmjs.org/booru/-/booru-2.5.4.tgz",
"integrity": "sha512-gxIVMn3LWEmpxlaw08VhuoR17Ym+NLAIWuE9ThDjjBbAuUqw73q0VmXm5R0etzmVqm+wctORrveNqZAQ/Pz6sQ==",
"requires": {
"fast-xml-parser": "^3.19.0",
"node-fetch": "^2.6.1"
}
},
"bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@ -1169,6 +1249,14 @@
}
}
},
"fast-xml-parser": {
"version": "3.21.1",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-3.21.1.tgz",
"integrity": "sha512-FTFVjYoBOZTJekiUsawGsSYV9QL0A+zDYCRj7y34IO6Jg+2IMYEtQa+bbictpdpV8dHxXywqU7C0gRDEOFtBFg==",
"requires": {
"strnum": "^1.0.4"
}
},
"finalhandler": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
@ -1347,6 +1435,14 @@
"resolved": "https://registry.npmjs.org/node-bin-setup/-/node-bin-setup-1.1.0.tgz",
"integrity": "sha512-pTeU6NgUrexiLNtd+AKwvg6cngHMvj5FZ5e2bbv2ogBSIc9yhkXSSaTScfSRZnwHIh5YFmYSYlemLWkiKD7rog=="
},
"node-fetch": {
"version": "2.6.7",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
"requires": {
"whatwg-url": "^5.0.0"
}
},
"normalize-url": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz",
@ -1489,11 +1585,21 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
},
"strnum": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/strnum/-/strnum-1.0.5.tgz",
"integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA=="
},
"toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
"integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA=="
},
"tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
},
"type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
@ -1518,6 +1624,20 @@
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
},
"webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
},
"whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
"requires": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View file

@ -1,5 +1,6 @@
{
"dependencies": {
"booru": "^2.5.4",
"express": "^4.17.3",
"got": "^12.0.3",
"node": "^17.7.2"

View file

@ -1,64 +1,26 @@
import got from "got";
import { readConfig } from "./config.js";
import downloadFromBooru from "./download.js";
import Booru from 'booru';
let configFile = "./config.json";
export function search() {
export function getFromBooru() {
readConfig(configFile, (err, config) => {
if (err) {
console.log(err);
return;
}
if (config.rating === "safe") {
let queryUrl = `https://${config.booru}.donmai.us/posts.json?tags=${config.tags}+rating:${config.rating}&z=1`;
got(queryUrl)
.then((response) => {
const jsonRes = JSON.parse(response.body);
const randPost = Math.floor(Math.random() * jsonRes.length);
const postID = jsonRes[randPost].id;
getPost(postID);
})
.catch((error) => {
console.error("error", error);
});
} else {
let queryUrl = `https://${config.booru}.donmai.us/posts.json?tags=${config.tags}&z=1`;
got(queryUrl)
.then((response) => {
const jsonRes = JSON.parse(response.body);
const randPost = Math.floor(Math.random() * jsonRes.length);
const postID = jsonRes[randPost].id;
getPost(postID);
})
.catch((error) => {
console.error("error", error);
});
}
});
}
function getPost(postID) {
readConfig(configFile, (err, config) => {
if (err) {
console.log(err);
return;
}
got(`https://${config.booru}.donmai.us/posts/${postID}.json`)
.then((response) => {
let jsonRes = JSON.parse(response.body);
const randBooru = Math.floor(Math.random() * config.booru.length);
Booru.search(config.booru[randBooru], config.tags, { limit: 1, random: true }).then(
(posts) => {
(async () => {
await downloadFromBooru(
jsonRes.file_url,
posts[0].fileUrl,
"./src/public/assets/waifu.png"
);
})();
})
.catch((error) => {
console.error("Cannot display waifu: ", error);
});
}
);
});
}
}

View file

@ -15,5 +15,5 @@ app.listen(port, () => {
});
function main() {
search();
getFromBooru();
}