diff --git a/config.json b/config.json index cc75498..bb999c6 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { "booru": "danbooru", - "tags": ["onodera_kosaki"], + "tags": ["tohsaka_rin"], "rating": "safe" } diff --git a/src/helpers/API.js b/src/helpers/API.js index e2e20b2..4919161 100644 --- a/src/helpers/API.js +++ b/src/helpers/API.js @@ -48,10 +48,10 @@ function getPost(postID) { if (err) { return console.log(err); } - downloadFromBooru.downloadFromBooru( - body.file_url, - "./src/public/assets/waifu.png" - ); + (async () => { + await downloadFromBooru + .downloadFromBooru(body.file_url, "./src/public/assets/waifu.png") + })(); } ); }); diff --git a/src/helpers/download.js b/src/helpers/download.js index 2cecd86..cf68183 100644 --- a/src/helpers/download.js +++ b/src/helpers/download.js @@ -1,20 +1,12 @@ const fs = require("fs"); -const client = require("https"); +const { promisify } = require("util"); +const got = require("got"); +const stream = require("stream"); -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}`)); - } - }); - }); +const pipeline = promisify(stream.pipeline); + +async function downloadFromBooru(url, filepath) { + await pipeline(got.stream(url), fs.createWriteStream(filepath)); } module.exports = { downloadFromBooru }; diff --git a/src/index.js b/src/index.js index f6109cd..fbe6d79 100644 --- a/src/index.js +++ b/src/index.js @@ -1,33 +1,22 @@ const search = require("./helpers/API"); -const serveStatic = require("serve-static"); const express = require("express"); -const path = require('path'); -const router = express.Router(); - +const path = require("path"); const app = express(); const port = 3000; -app.set("view engine", "pug"); -app.set("views", path.join(__dirname, "views")); +app.use("/assets", express.static(path.join(__dirname, "/public/assets"))); -const options = { - headers: { - 'Cache-Control': 'no-cache', - } -}; - -//app.use(express.static('./src/public')); -app.use("/", router) -router.get("/", (req, res) => { - res.send(main()); +app.get("/", (req, res) => { + res.set("Cache-Control", "no-store"); + main(); + res.sendFile(path.join(__dirname, "/public/index.html")); }); - app.listen(port, () => { console.log(`listening on port ${port}`); }); -async function main() { +function main() { search.search(); }