Add automatic mode

main
Benjamin Bädorf 2023-02-08 17:32:05 +01:00
parent 215649dc32
commit c99f2209fc
No known key found for this signature in database
GPG Key ID: 4406E80E13CD656C
3 changed files with 47 additions and 30 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
tags
.direnv
_site
result

View File

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

View File

@ -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({