From c99f2209fcc9cc737f8af10a17624dbda2e0c992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4dorf?= Date: Wed, 8 Feb 2023 17:32:05 +0100 Subject: [PATCH] Add automatic mode --- .gitignore | 1 + overlay.nix | 9 ++++--- src/main.ts | 67 ++++++++++++++++++++++++++++++++--------------------- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index a977291..859ed72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ tags .direnv _site +result diff --git a/overlay.nix b/overlay.nix index 721c7ba..4bee158 100644 --- a/overlay.nix +++ b/overlay.nix @@ -17,10 +17,13 @@ final: prev: python39Packages.img2pdf ]; + additionalDenoFlags = "--allow-write --allow-env --allow-run"; + + # TODO: Find out why allow does not work here allow = { - "write" = true; - "env" = true; - "run" = true; + write = true; + env = true; + run = true; }; }; }; diff --git a/src/main.ts b/src/main.ts index e92d4b2..5c54644 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,44 +3,46 @@ import { iterateReader } from "std/streams/iterate_reader"; const getRandomString = () => `${+(new Date())}-${Math.round(Math.random() * 100000)}`; -(async () => { - const tempDir = await Deno.makeTempDir(); +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); +(async () => { const args = parse(Deno.args); if (args.help || args.h) { - console.log(` - Usage: scan2paperless [OPTION] [NAME] + console.log(`Usage: scan2paperless [OPTION] [NAME] - Scan one or multiple pages into a PDF document ready for consumption by paperless +Scan one or multiple pages into a PDF document ready for consumption by paperless - NAME will be the name of the document without extension. +NAME will be the name of the document without extension. - Available options: - -d | --device=DEVICE Scanner device to use. Check \`scanimage -L\` for available devices. - This defaults to \$SCANNER_DEFAULT_DEVICE - -o | --output-dir=DIR Directory to write final PDF to. This defaults to \$SCANNER_OUTPUT_DIR - -h | --help show this help message +Available options: + -d | --device=DEVICE Scanner device to use. Check \`scanimage -L\` for available devices. + This defaults to \$SCANNER_DEFAULT_DEVICE + -n | --pages=NUMBER Number of pages to scan. If you don't specify this, scan2paperless + will ask you if you want to continue. + -a | --auto Don't pause between pages. Only has an effect if --pages is set. + -o | --output-dir=DIR Directory to write final PDF to. This defaults to \$SCANNER_OUTPUT_DIR + -h | --help show this help message - Example: +Example: - scan2paperless \ - -d hp3900:libusb:005:002 \ - -n 5 \ - -o /var/lib/paperless/consume \ - bank-statement-01-2023 +scan2paperless \\ + -d hp3900:libusb:005:002 \\ + -n 5 \\ + -o /var/lib/paperless/consume \\ + bank-statement-01-2023 `); Deno.exit(0); } + const tempDir = await Deno.makeTempDir(); const device = args.d || args.device || Deno.env.get('SCANNER_DEFAULT_DEVICE'); const outputDir = args.d || args.device || Deno.env.get('SCANNER_OUTPUT_DIR'); const outputName = args._[0] || getRandomString(); - /* const pagesToScanStr = args.n || args.pages; - const pagesToScan = (() => { if (!pagesToScanStr) { return Infinity; @@ -55,13 +57,19 @@ const getRandomString = () => `${+(new Date())}-${Math.round(Math.random() * 10 return pagesToScanInt; })(); - let pagesScanned = 0; - */ + + const automatic = (!!(args.a || args.auto) && pagesToScan < Infinity); const files = []; + let pagesScanned = 0; let cancelled = false; + do { const fileName = `${tempDir}/${getRandomString()}.jpg`; + + console.log(`Start scanning page ${pagesScanned + 1}`); + console.log(`Writing to ${fileName}`); + const scanimageProcess = Deno.run({ cmd: [ "scanimage", @@ -75,18 +83,21 @@ const getRandomString = () => `${+(new Date())}-${Math.round(Math.random() * 10 stderr: 'piped', }); - const decoder = new TextDecoder(); - const encoder = new TextEncoder(); - let inScan = false; for await (const chunk of iterateReader(scanimageProcess.stderr)) { const line = decoder.decode(chunk); - if (!inScan && line.startsWith('Progress: ')) { + if ( + !automatic + && pagesScanned !== 0 + && !inScan + && line.startsWith('Progress: ') + ) { inScan = true; + Deno.kill(scanimageProcess.pid, 'SIGSTOP'); - const answer = prompt("Scan next page? [Y/n]"); + const answer = prompt(`Run another scan? (number ${pagesScanned + 1}) [Y/n]`); if (answer && !['y', 'yes'].includes(answer.toLowerCase())) { Deno.kill(scanimageProcess.pid, 'SIGKILL'); @@ -108,10 +119,12 @@ const getRandomString = () => `${+(new Date())}-${Math.round(Math.random() * 10 console.error('Scan seems to have failed with status ', status.code); } + pagesScanned++; + if (!cancelled) { files.push(fileName); } - } while(!cancelled); + } while(!cancelled && (automatic ? pagesScanned < pagesToScan : true)); const pdfFile = `${outputDir}/${outputName}.pdf`; const img2pdfProcess = Deno.run({