From a6b5e560fb1ac49e6b30b68fc50697bc1bf71384 Mon Sep 17 00:00:00 2001 From: maru21 Date: Fri, 8 Nov 2024 21:25:14 +0100 Subject: [PATCH] made it simpler and to be run within a webserver on scrapy --- Dockerfile | 21 +++++++++++++++ amznMailConverter.service | 13 ++++++++++ daemon.py | 10 +++++++- daemon.service | 11 -------- email_pdf_downloader.py | 8 +++--- pdf_server.py | 4 ++- requirements.txt | 7 +++++ webSimple.py | 54 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 Dockerfile create mode 100644 amznMailConverter.service delete mode 100644 daemon.service create mode 100644 requirements.txt create mode 100644 webSimple.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9eecf8e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# Use an official Python runtime as a base image +FROM python:3.9 + +# Set working directory in the container +WORKDIR /app + +# Copy requirements file and install dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code +COPY . . + +# Expose the port for the PDF server +EXPOSE 8000 + +# Environment variables for Google API credentials +ENV GOOGLE_APPLICATION_CREDENTIALS="/app/credentials.json" + +# Run the script as a daemon to check emails and download PDFs +CMD ["python", "daemon.py"] diff --git a/amznMailConverter.service b/amznMailConverter.service new file mode 100644 index 0000000..f24ec89 --- /dev/null +++ b/amznMailConverter.service @@ -0,0 +1,13 @@ + +[Unit] +Description=Amazon Mail Converter Daemon + +[Service] +ExecStart=/scrapy/venvs/amznMailConverter/bin/python3 /scrapy/amznMailConverter/daemon.py + +Restart=always +User=root +WorkingDirectory=/scrapy/amznMailConverter/ + +[Install] +WantedBy=multi-user.target diff --git a/daemon.py b/daemon.py index c75cfc4..02a10e7 100644 --- a/daemon.py +++ b/daemon.py @@ -1,6 +1,10 @@ +#!/scrapy/venvs/amznMailConverter/bin/python + import time import logging -from amznMailConverter.email_pdf_downloader import read_emails +import os +from webSimple import generate_pdf_list_html +from email_pdf_downloader import read_emails def run_daemon(): while True: @@ -9,6 +13,10 @@ def run_daemon(): logging.info("Finished checking emails. Sleeping for 1 minutes.") except Exception as e: logging.error(f"An error occurred: {e}") + + directory = '/scrapy/amznMailConverter/data' + output_file = os.path.join(directory, "/scrapy/amznMailConverter/index.html") + generate_pdf_list_html(directory, output_file) time.sleep(60) # Sleep for 1 minute between checks if __name__ == "__main__": diff --git a/daemon.service b/daemon.service deleted file mode 100644 index acefce7..0000000 --- a/daemon.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=Amazon Mail Converter Daemon - -[Service] -ExecStart=/usr/bin/python3 /path/to/your_script.py -Restart=always -User=your_username -WorkingDirectory=/path/to/your_script.py - -[Install] -WantedBy=multi-user.target diff --git a/email_pdf_downloader.py b/email_pdf_downloader.py index 11f0017..1c9af74 100644 --- a/email_pdf_downloader.py +++ b/email_pdf_downloader.py @@ -1,3 +1,5 @@ +#!/scrapy/venvs/amznMailConverter/bin/python + import os import base64 import requests @@ -18,15 +20,15 @@ SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) def get_credentials(): creds = None - if os.path.exists('token.json'): + if os.path.exists('/scrapy/amznMailConverter/token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: - flow = InstalledAppFlow.from_client_secrets_file('/path/to/your/credentials.json', SCOPES) + flow = InstalledAppFlow.from_client_secrets_file('/scrapy/amznMailConverter/credentials.json', SCOPES) creds = flow.run_local_server(port=0) - with open('token.json', 'w') as token: + with open('/scrapy/amznMailConverter/token.json', 'w') as token: token.write(creds.to_json()) return creds diff --git a/pdf_server.py b/pdf_server.py index 3df67f9..4d00bcc 100644 --- a/pdf_server.py +++ b/pdf_server.py @@ -1,3 +1,5 @@ +#!/scrapy/venvs/amznMailConverter/bin/python + import os from http.server import HTTPServer, SimpleHTTPRequestHandler from io import BytesIO @@ -75,6 +77,6 @@ class PDFServer: # Usage if __name__ == "__main__": - directory = '/home/maru/Dev/git/amznMailConverter/data/' + directory = '/scrapy/amznMailConverter/data' server = PDFServer(directory, port=8000) server.start_server() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..43f5924 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +google-auth +google-auth-oauthlib +google-auth-httplib2 +google-api-python-client +requests +beautifulsoup4 +python-daemon # Only if using the daemon library \ No newline at end of file diff --git a/webSimple.py b/webSimple.py new file mode 100644 index 0000000..88c5ea8 --- /dev/null +++ b/webSimple.py @@ -0,0 +1,54 @@ +#!/scrapy/venvs/amznMailConverter/bin/python + +import os + +from datetime import datetime + +def generate_pdf_list_html(directory, output_file="pdf_list.html"): + """Generate an HTML file listing PDF files in the specified directory with the generation timestamp.""" + try: + # Get a list of PDF files in the directory + file_list = [f for f in os.listdir(directory) if f.endswith('.pdf')] + file_list.sort(key=lambda a: a.lower()) + + # Get the current timestamp + generation_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + # HTML content with dark and blue-themed Material Design styling + html_content = f''' + + PDF Files + + + +
+

Available PDF Files for Download

+ + +
+ + ''' + + # Write the HTML content to the output file + with open(output_file, "w") as file: + file.write(html_content) + print(f"HTML file generated at: {os.path.abspath(output_file)}") + + except Exception as e: + print(f"An error occurred: {e}") + +