From ef43e9dcf65de6122e75547b6012b1c750345456 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Tue, 1 Aug 2023 03:13:50 +0200 Subject: [PATCH] inital commit --- Dockerfile | 15 ++++++ LICENSE | 7 +++ README.md | 19 +++++++ app.py | 131 ++++++++++++++++++++++++++++++++++++++++++++++ index.html | 16 ++++++ requirements.txt | 2 + result.html | 43 +++++++++++++++ static/styles.css | 63 ++++++++++++++++++++++ 8 files changed, 296 insertions(+) create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 app.py create mode 100644 index.html create mode 100644 requirements.txt create mode 100644 result.html create mode 100644 static/styles.css diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1e01d4c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +# Verwende das offizielle Python-Image als Basis +FROM python:3.8-slim + +# Setze das Arbeitsverzeichnis innerhalb des Containers +WORKDIR /app + +# Installiere die erforderlichen Abhängigkeiten +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Kopiere die Anwendungsdateien in den Container +COPY . . + +# Starte die Anwendung, wenn der Container gestartet wird +CMD ["python", "app.py"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c769673 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 Philipp Wagner + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..407e90a --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Email Unsubscribe Links Scraper + +This is a simple Python web application that logs into an email server, scans the inbox for unsubscribe links, and displays the results in a table. It also provides the option to move selected emails to the trash. + +## Disclaimer +This App is still in development + +## How to use + +1. Clone this repository. +2. Build the Docker image: +docker build -t username/email-unsubscribe-links-scraper . +3. Run the Docker container: +docker run -p 5000:5000 -e EMAIL_USERNAME=your_email@example.com -e EMAIL_PASSWORD=your_email_password -e EMAIL_SERVER=your_email_server username/email-unsubscribe-links-scraper +4. Access the application in your web browser at http://localhost:5000. + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/app.py b/app.py new file mode 100644 index 0000000..82cf843 --- /dev/null +++ b/app.py @@ -0,0 +1,131 @@ +import imaplib +import email +import re +import os +from flask import Flask, render_template, request, redirect, url_for +from dotenv import load_dotenv + +app = Flask(__name__) +load_dotenv() # Laden der Umgebungsvariablen aus der .env-Datei + +# Email-Server-Konfiguration +EMAIL_USERNAME = os.environ.get("EMAIL_USERNAME") +EMAIL_PASSWORD = os.environ.get("EMAIL_PASSWORD") +EMAIL_SERVER = os.environ.get("EMAIL_SERVER") + +template_dir = os.path.abspath(os.path.dirname(__file__)) +app = Flask(__name__, template_folder=template_dir) + +def get_text_from_email(email_message): + text = "" + for part in email_message.walk(): + content_type = part.get_content_type() + content_disposition = str(part.get("Content-Disposition")) + + if "text" in content_type and "attachment" not in content_disposition: + body = part.get_payload(decode=True) + if body: + text += body.decode(errors="ignore") + return text + +def find_unsubscribe_links(text): + # Erweitere die Liste der Abmeldelinks-Patterns bei Bedarf + unsubscribe_patterns = [ + r"(?i)\babbestellen\b", # Deutsch - abbestellen + r"(?i)\babbestellung\b", # Deutsch - abbestellung + r"(?i)\bdeabonnieren\b", # Deutsch - deabonnieren + r"(?i)\bunsubscribe\b", # Englisch - unsubscribe + ] + unsubscribe_links = [] + for pattern in unsubscribe_patterns: + matches = re.findall(pattern, text) + if matches: + for match in matches: + link = find_link_in_text(text) + if link: + unsubscribe_links.append(link) + return unsubscribe_links + +def find_link_in_text(text): + # Einen einfachen Ansatz zur Link-Erkennung in Texten (nicht perfekt) + link_pattern = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" + link_matches = re.findall(link_pattern, text) + return link_matches[0] if link_matches else None + +def get_subject_from_email(email_message): + subject = email_message.get("Subject") + return subject + +def move_emails_to_trash(email_ids): + + username = EMAIL_USERNAME + password = EMAIL_PASSWORD + server = EMAIL_SERVER + + mail = imaplib.IMAP4_SSL(server) + mail.login(username, password) + mail.select("inbox") + + for email_id in email_ids: + # Verschiebe die E-Mail in den Papierkorb (Trash) + mail.copy(email_id, "Trash") + # Setze die \Deleted-Flagge, um die E-Mail später zu löschen (für Mailcow) + mail.store(email_id, "+FLAGS", "\\Deleted") + + # Speichere die Änderungen auf dem IMAP-Server + mail.expunge() + mail.logout() + +@app.route("/", methods=["GET", "POST"]) +def index(): + return render_template("index.html", email_address=EMAIL_USERNAME) + +@app.route("/result") +def result(): + + username = EMAIL_USERNAME + password = EMAIL_PASSWORD + server = EMAIL_SERVER + + mail = imaplib.IMAP4_SSL(server) + mail.login(username, password) + mail.select("inbox") + + _, messages = mail.search(None, "ALL") + links_data = {} + + for message_num in messages[0].split(): + _, msg_data = mail.fetch(message_num, "(RFC822)") + msg = email.message_from_bytes(msg_data[0][1]) + body = get_text_from_email(msg) + subject = get_subject_from_email(msg) + if body: + unsubscribe_links = find_unsubscribe_links(body) + if unsubscribe_links: + links_data[message_num.decode()] = { + "subject": subject, + "links": list(set(unsubscribe_links)) + } + + mail.logout() + + return render_template("result.html", links_data=links_data) + +@app.route("/move-to-trash", methods=["POST"]) +def move_to_trash(): + if request.method == "POST": + email_id = request.form.get("email_id") + if email_id: + move_emails_to_trash([email_id]) + return redirect(url_for("result")) + +@app.route("/move-all-to-trash", methods=["POST"]) +def move_all_to_trash(): + if request.method == "POST": + email_ids = request.form.getlist("email_id") + if email_ids: + move_emails_to_trash(email_ids) + return redirect(url_for("index")) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5000, debug=True) \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..a65b39f --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ + + + + E-Mail Unsubscribe Link Finder + + + +
+

E-Mail Unsubscribe Link Finder

+

Email your emails to: {{ email_address }}

+
+ +
+
+ + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8156513 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Flask +python-dotenv diff --git a/result.html b/result.html new file mode 100644 index 0000000..12ff846 --- /dev/null +++ b/result.html @@ -0,0 +1,43 @@ + + + + E-Mail Unsubscribe Links Result + + + +
+

E-Mail Unsubscribe Links Result

+ + + + + + + + {% for email_id, data in links_data.items() %} + + + + + + + {% endfor %} +
Email AddressSubjectUnsubscribe LinkMove to Trash
{{ email_id }}{{ data.subject }} + {% for link in data.links %} + {{ link[:30] + '...' }}
+ {% endfor %} +
+
+ + +
+
+
+ +
+
+ +
+
+ + diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..0d3dd42 --- /dev/null +++ b/static/styles.css @@ -0,0 +1,63 @@ +body { + font-family: Arial, sans-serif; + background-color: #f7f7f7; + color: #333; + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; +} + +.container { + max-width: 800px; + padding: 20px; + text-align: center; +} + +h1 { + color: #007bff; + margin-bottom: 20px; +} + +table { + width: 100%; + border-collapse: collapse; + margin-bottom: 20px; +} + +table, th, td { + border: 1px solid #ddd; + padding: 10px; +} + +th { + background-color: #f2f2f2; +} + +.btn-primary { + background-color: #007bff; + color: #fff; + border: none; + padding: 20px 40px; + border-radius: 5px; + cursor: pointer; +} + +.btn-delete { + background-color: #dc3545; + color: #fff; + border: none; + padding: 10px 20px; + border-radius: 5px; + cursor: pointer; +} + +.btn-primary, .btn-delete { + margin-top: 10px; +} + +.btn-primary:hover, .btn-delete:hover { + opacity: 0.8; +}