From 79d8c9f74ec5cb6e5d84b7ff396c7db4aa3f30b3 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sat, 2 Apr 2022 19:57:42 -0400 Subject: [PATCH] Working api call - it only took an hour hehe haha --- config.json | 5 +++++ src/helpers/API.js | 28 ++++++++++++++++++++++++++++ src/helpers/config.js | 18 ++++++++++++++++++ src/index.js | 13 +++++-------- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 config.json create mode 100644 src/helpers/API.js create mode 100644 src/helpers/config.js diff --git a/config.json b/config.json new file mode 100644 index 0000000..a17ba2b --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "booru": "testbooru", + "tags": ["onodera_kosaki", "-underwear"], + "rating": "safe" +} diff --git a/src/helpers/API.js b/src/helpers/API.js new file mode 100644 index 0000000..f082da2 --- /dev/null +++ b/src/helpers/API.js @@ -0,0 +1,28 @@ +const request = require("request"); +const parseConfig = require("./config") + +function search() { + 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) { + return console.log(err); + } + + 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 new file mode 100644 index 0000000..5271611 --- /dev/null +++ b/src/helpers/config.js @@ -0,0 +1,18 @@ +const fs = require("fs"); + + +function readConfig(configPath, callBack) { + 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) + } + }); +} + +module.exports = { readConfig }; diff --git a/src/index.js b/src/index.js index 7690fa5..f419f94 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,7 @@ -const request = require('request'); +const search = require("./helpers/API") +async function main() { + search.search(); +} -request('https://testbooru.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) -}); +main(); \ No newline at end of file