From f0d626e92e9698a1842f844591cfea3df6a176a4 Mon Sep 17 00:00:00 2001 From: Matthieu Borgognon Date: Sun, 16 May 2021 18:04:39 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ Dockerfile | 7 +++++++ README.adoc | 9 +++++++++ main.py | 12 ++++++++++++ requirements.txt | 2 ++ send_email.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.adoc create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 send_email.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9d42b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +test.pdf +venv \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ba8201b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.6-slim-buster +RUN apt-get update && apt-get install -y sane-utils libsane-hpaio imagemagick tesseract-ocr-fra tesseract-ocr-deu pngquant nano +WORKDIR /usr/src/app +COPY . . +RUN pip install --no-cache-dir -r requirements.txt +RUN sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/' /etc/ImageMagick-6/policy.xml +CMD ["python", "./main.py"] \ No newline at end of file diff --git a/README.adoc b/README.adoc new file mode 100644 index 0000000..a15508d --- /dev/null +++ b/README.adoc @@ -0,0 +1,9 @@ += Auto-scan to automatically scan documents from HP, OCR files, optimize it and finally send it by email as attachment +:icons: font +ifdef::env-github[] +:tip-caption: :bulb: +:note-caption: :information_source: +:important-caption: :heavy_exclamation_mark: +:caution-caption: :fire: +:warning-caption: :warning: +endif::[] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..1865a24 --- /dev/null +++ b/main.py @@ -0,0 +1,12 @@ +import subprocess +import datetime +from send_email import * + +ts_now = '{:%Y-%m-%d_%H%M%S}'.format(datetime.datetime.now()) +file_with_ts = 'attachment_' + ts_now + '.pdf' + +subprocess.run(["scan-pdf/src/scan-pdf", "--flatbed", "--color-mode", "color", file_with_ts]) + +subprocess.run(["ocrmypdf", "-r", "--rotate-pages-threshold", "6", "-O", "3", file_with_ts, file_with_ts]) + +send_email(file_with_ts, ts_now) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4363c76 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ocrmypdf>=12.0.1<12.1.0 +python-dotenv>=0.17.1<0.18.0 \ No newline at end of file diff --git a/send_email.py b/send_email.py new file mode 100644 index 0000000..3995cc3 --- /dev/null +++ b/send_email.py @@ -0,0 +1,49 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.mime.application import MIMEApplication +import os +from dotenv import load_dotenv + + +def send_email(filename: str, ts: str) -> str: + load_dotenv() + + gmail_user = os.environ['RPI_EMAIL'] + gmail_password = os.environ['RPI_PASS'] + + sent_from = gmail_user + + message = MIMEMultipart('mixed') + message['From'] = 'RPI Scanner <{sender}>'.format(sender = sent_from) + message['To'] = os.environ['EMAIL_RECIPIENTS'] + message['CC'] = '' + message['Subject'] = 'Scan ' + ts + + msg_content = '

Hi There,
This is an automatic HP scanner message.

\n' + body = MIMEText(msg_content, 'html') + message.attach(body) + + attachmentPath = "./" + filename + + try: + with open(attachmentPath, "rb") as attachment: + p = MIMEApplication(attachment.read(),_subtype="pdf") + p.add_header('Content-Disposition', "attachment; filename= %s" % attachmentPath.split("/")[-1]) + message.attach(p) + except Exception as e: + print(str(e)) + + msg_full = message.as_string() + + try: + server = smtplib.SMTP_SSL('smtp.gmail.com', 465) + server.ehlo() + server.login(gmail_user, gmail_password) + server.sendmail(sent_from, + message['To'].split(";") + (message['CC'].split(";") if message['CC'] else []), + msg_full) + server.close() + print('Email sent!') + except: + print('Something went wrong...')