From afc95836e7ae509e515f270f15d1100f7a1bd3b2 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sat, 2 Apr 2022 20:19:54 -0400 Subject: [PATCH] Download from booru --- src/helpers/API.js | 40 ++++++++++++++++++++-------------------- src/helpers/config.js | 7 +++---- src/helpers/download.js | 20 ++++++++++++++++++++ src/index.js | 4 ++-- 4 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 src/helpers/download.js diff --git a/src/helpers/API.js b/src/helpers/API.js index f082da2..4f08b1d 100644 --- a/src/helpers/API.js +++ b/src/helpers/API.js @@ -1,28 +1,28 @@ const request = require("request"); -const parseConfig = require("./config") +const parseConfig = require("./config"); function search() { - let configFile = "./config.json"; - parseConfig.readConfig(configFile, (err, config)=>{ + let configFile = "./config.json"; + parseConfig.readConfig(configFile, (err, config) => { + if (err) { + console.log(err); + return; + } + request( + `https://${config.booru}.donmai.us/posts/6.json`, + { json: true }, + (err, res, body) => { if (err) { - console.log(err); - return; + return console.log(err); } - request( - `https://${config.booru}.donmai.us/posts/6.json`, - { json: true }, - (err, res, body) => { - if (err) { - return console.log(err); - } - - console.log(body.id); - console.log(body.created_at); - console.log(body.rating); - console.log(body.large_file_url); - } - ); - }); + + console.log(body.id); + console.log(body.created_at); + console.log(body.rating); + console.log(body.large_file_url); + } + ); + }); } module.exports = { search }; diff --git a/src/helpers/config.js b/src/helpers/config.js index 5271611..9bfe299 100644 --- a/src/helpers/config.js +++ b/src/helpers/config.js @@ -1,16 +1,15 @@ const fs = require("fs"); - function readConfig(configPath, callBack) { - fs.readFile(configPath, (err, configData) =>{ - if (err){ + fs.readFile(configPath, (err, configData) => { + if (err) { return callBack && callBack(err); } try { const configObject = JSON.parse(configData); return callBack && callBack(null, configObject); } catch (err) { - return callBack && callBack(err) + return callBack && callBack(err); } }); } diff --git a/src/helpers/download.js b/src/helpers/download.js new file mode 100644 index 0000000..2cecd86 --- /dev/null +++ b/src/helpers/download.js @@ -0,0 +1,20 @@ +const fs = require("fs"); +const client = require("https"); + +function downloadFromBooru(url, filepath) { + return new Promise((resolve, reject) => { + client.get(url, (res) => { + if (res.statusCode === 200) { + res + .pipe(fs.createWriteStream(filepath)) + .on("error", reject) + .once("close", () => resolve(filepath)); + } else { + res.resume(); + reject(new Error(`Request failed with status code: ${res.statusCode}`)); + } + }); + }); +} + +module.exports = { downloadFromBooru }; diff --git a/src/index.js b/src/index.js index f419f94..ee80924 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ -const search = require("./helpers/API") +const search = require("./helpers/API"); async function main() { search.search(); } -main(); \ No newline at end of file +main();