Booru Support #1

Merged
sam merged 7 commits from remote into master 2022-03-15 19:13:39 +00:00
5 changed files with 17 additions and 4 deletions
Showing only changes of commit 26bcdc2df7 - Show all commits

View file

@ -60,6 +60,7 @@ if (args.verbose) console.log("Running in verbose mode.\n");
if (args.writeConfig) {
writeConfig();
console.log("Wrote default config file to ./config.jsonc");
exit(0);
}

View file

@ -13,6 +13,9 @@ async function readConfig(): Promise<string> {
crashHandler("Error reading config file.", err);
return "CRASH";
});
if (args.verbose) {
console.log(`Read config file: ${args.config}\n${conf}`);
}
return conf;
}
/**

View file

@ -1,9 +1,11 @@
import { writeFileSync } from "fs";
import args from "./cli.js";
/**
* Writes the sample config file to disk
* @returns Nothing
*/
export default function writeConfig() {
if (args.verbose) console.log("Writing sample config to config.jsonc");
writeFileSync(
"./config.jsonc",
// eslint-disable-next-line prettier/prettier

View file

@ -51,7 +51,7 @@ export default async function getLocalImage(conf: config) {
sensitivity = false;
}
if (args.verbosity) {
if (args.verbose) {
console.log(`File being sent: ${file}`);
console.log(`Sensitivity: ${sensitivity}`);
}

View file

@ -1,10 +1,11 @@
import { search } from "booru";
import Post from "booru/dist/structures/Post"; // Ce n'est pas bien
import { createReadStream, createWriteStream, unlink } from "fs";
import got from "got-cjs";
import { createWriteStream, createReadStream, unlink } from "fs";
import { promisify } from "node:util";
import stream from "node:stream";
import { promisify } from "node:util";
import { exit } from "process";
import args from "./helpers/cli.js";
import crashHandler from "./helpers/error.js";
import { config } from "./helpers/types.js";
import postImage from "./post.js";
@ -29,12 +30,14 @@ export default async function getRemoteImage(conf: config) {
return;
}
const post = searchResults[0];
if (args.verbose) console.log(`Found post: ${post.id} at ${post.file_url}`);
// Set the post as sensitive if the rating is not safe
const sensitivity: boolean = post.rating !== "s";
// Make an HTTP request for the image
const filename: string = post.fileUrl?.split("/").pop() as string; // Type checks for type checks
const pipeline = promisify(stream.pipeline);
// Make the HTTP request as a stream so it can be piped to the file system
await pipeline(
got.stream(post.file_url as string),
createWriteStream(filename)
@ -42,11 +45,14 @@ export default async function getRemoteImage(conf: config) {
crashHandler("Error saving downloading image.", err);
return;
});
if (args.verbose) console.log(`Saved image to ${filename}`);
const str = createReadStream(filename).on("error", (err: Error) => {
crashHandler("Error reading downloaded image.", err);
});
if (args.verbose) {
console.log(`File being sent: ${filename}\nSensitivity: ${sensitivity}`);
}
// Make a status with the image
await postImage(str, sensitivity, conf);
@ -57,5 +63,6 @@ export default async function getRemoteImage(conf: config) {
return;
}
});
if (args.verbose) console.log(`Successfully deleted image ${filename}`);
exit(0);
}