1
0
Fork 0

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

This commit is contained in:
grumbulon 2022-04-03 10:03:09 -04:00
parent 763d2dc0df
commit 231a6899d2
3 changed files with 11 additions and 19 deletions

View file

@ -1,5 +1,5 @@
{
"booru": "danbooru",
"tags": ["onodera_kosaki"],
"tags": ["tohsaka_rin"],
"rating": "safe"
}

View file

@ -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")
})();
}
);
});

View file

@ -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 };