made it simpler and to be run within a webserver on scrapy

This commit is contained in:
maru21 2024-11-08 21:25:14 +01:00
parent 0f9d83a5c1
commit a6b5e560fb
8 changed files with 112 additions and 16 deletions

21
Dockerfile Normal file
View File

@ -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"]

13
amznMailConverter.service Normal file
View File

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

View File

@ -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__":

View File

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

View File

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

View File

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

7
requirements.txt Normal file
View File

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

54
webSimple.py Normal file
View File

@ -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'''<html>
<head>
<title>PDF Files</title>
<style>
body {{ font-family: Arial, sans-serif; background-color: #121212; color: #e0e0e0; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }}
.container {{ max-width: 600px; background: #1e1e1e; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.5); border-radius: 8px; padding: 20px; text-align: center; }}
h2 {{ color: #bb86fc; font-size: 24px; margin-bottom: 20px; }}
ul {{ list-style-type: none; padding: 0; }}
li {{ margin: 10px 0; }}
a {{ text-decoration: none; color: #03dac6; font-weight: bold; padding: 10px 15px; border-radius: 5px; transition: background-color 0.2s ease; }}
a:hover {{ background-color: #333; color: #bb86fc; }}
.footer {{ margin-top: 20px; font-size: 12px; color: #666; }}
</style>
</head>
<body>
<div class="container">
<h2>Available PDF Files for Download</h2>
<ul>'''
for name in file_list:
html_content += f'<li><a href="data/{name}" download>{name}</a></li>'
html_content += f'''</ul>
<div class="footer">Generated on: {generation_time}</div>
</div>
</body>
</html>'''
# 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}")