From 763d2dc0df89c792cb05c82a2fd0a01ae8826c73 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sun, 3 Apr 2022 09:43:53 -0400 Subject: [PATCH 1/2] Mostly functional --- src/helpers/API.js | 8 ++++---- src/helpers/download.js | 2 +- src/index.js | 25 +++++++------------------ 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/helpers/API.js b/src/helpers/API.js index e2e20b2..207a223 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" - ); + downloadFromBooru + .downloadFromBooru(body.file_url, "./src/public/assets/waifu.png") + .then(console.log) + .catch(console.error); } ); }); diff --git a/src/helpers/download.js b/src/helpers/download.js index 2cecd86..e39a22d 100644 --- a/src/helpers/download.js +++ b/src/helpers/download.js @@ -1,7 +1,7 @@ const fs = require("fs"); const client = require("https"); -function downloadFromBooru(url, filepath) { +async function downloadFromBooru(url, filepath) { return new Promise((resolve, reject) => { client.get(url, (res) => { if (res.statusCode === 200) { 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(); } From 231a6899d239b4f5f366b49269bda1f347255795 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sun, 3 Apr 2022 10:03:09 -0400 Subject: [PATCH 2/2] re-wrote download.js to use more libraries (so good) and made call to downloadFromBooru await (should make it so images don't download in parts --- config.json | 2 +- src/helpers/API.js | 8 ++++---- src/helpers/download.js | 20 ++++++-------------- 3 files changed, 11 insertions(+), 19 deletions(-) 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 207a223..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") - .then(console.log) - .catch(console.error); + (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 e39a22d..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"); + +const pipeline = promisify(stream.pipeline); async 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}`)); - } - }); - }); + await pipeline(got.stream(url), fs.createWriteStream(filepath)); } module.exports = { downloadFromBooru };