From 2e78190d43cc873c8740cf225c56f6fcf8d5bc1b Mon Sep 17 00:00:00 2001 From: grumbulon Date: Mon, 4 Apr 2022 20:31:06 -0400 Subject: [PATCH 1/3] Using the booru library now pulls from three (3)! boorus to display more _unique_ image selection --- config.json | 4 +- package-lock.json | 120 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/helpers/API.js | 58 ++++------------------ src/index.js | 2 +- 5 files changed, 134 insertions(+), 51 deletions(-) diff --git a/config.json b/config.json index fd645da..6e33a71 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "booru": "danbooru", - "tags": ["iwakura_lain"], + "booru": ["danbooru", "safebooru", "konan"], + "tags": ["onodera_kosaki", "-underwear"], "rating": "safe" } diff --git a/package-lock.json b/package-lock.json index e27690b..ad1ad2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index f952aa7..e865db7 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "booru": "^2.5.4", "express": "^4.17.3", "got": "^12.0.3", "node": "^17.7.2" diff --git a/src/helpers/API.js b/src/helpers/API.js index d48282c..36b11ee 100644 --- a/src/helpers/API.js +++ b/src/helpers/API.js @@ -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); - }); + } + ); }); -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index b3be79f..801a03b 100644 --- a/src/index.js +++ b/src/index.js @@ -15,5 +15,5 @@ app.listen(port, () => { }); function main() { - search(); + getFromBooru(); } From d4e5257f741393d49693265159c3643e3cb8ca39 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Mon, 4 Apr 2022 21:04:28 -0400 Subject: [PATCH 2/3] Rewrite complete, added error checks and some stuff --- src/helpers/API.js | 33 +++++++++++++++++++++------------ src/index.js | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/helpers/API.js b/src/helpers/API.js index 36b11ee..59378d5 100644 --- a/src/helpers/API.js +++ b/src/helpers/API.js @@ -1,7 +1,6 @@ import { readConfig } from "./config.js"; import downloadFromBooru from "./download.js"; -import Booru from 'booru'; - +import { search } from "booru"; let configFile = "./config.json"; @@ -11,16 +10,26 @@ export function getFromBooru() { console.log(err); return; } + if (config.rating === "safe") { + config.tags.push("rating:safe"); + } + 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( - posts[0].fileUrl, - "./src/public/assets/waifu.png" - ); - })(); + search(config.booru[randBooru], config.tags, { + limit: 1, + random: true, + }).then((response) => { + if (response.length === 0) { + console.log("Error searching for posts.", Error("No posts found.")); + return; } - ); + + const post = response[0]; + + (async () => { + await downloadFromBooru(post.fileUrl, "./src/public/assets/waifu.png"); + })(); + }); }); -} \ No newline at end of file +} +getFromBooru(); diff --git a/src/index.js b/src/index.js index 801a03b..d362035 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import { search } from "./helpers/API.js"; +import { getFromBooru } from "./helpers/API.js"; import express from "express"; const app = express(); From 0fe1b36f44d34b0fafefcec73aabf77a497f2682 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Mon, 4 Apr 2022 21:06:12 -0400 Subject: [PATCH 3/3] Removed function call from debugging :0 --- src/helpers/API.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helpers/API.js b/src/helpers/API.js index 59378d5..5360bef 100644 --- a/src/helpers/API.js +++ b/src/helpers/API.js @@ -32,4 +32,3 @@ export function getFromBooru() { }); }); } -getFromBooru();