inital commit
This commit is contained in:
commit
ef43e9dcf6
15
Dockerfile
Normal file
15
Dockerfile
Normal file
@ -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"]
|
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -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.
|
19
README.md
Normal file
19
README.md
Normal file
@ -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.
|
131
app.py
Normal file
131
app.py
Normal file
@ -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)
|
16
index.html
Normal file
16
index.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>E-Mail Unsubscribe Link Finder</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>E-Mail Unsubscribe Link Finder</h1>
|
||||
<p>Email your emails to: {{ email_address }}</p>
|
||||
<form action="/result" method="get">
|
||||
<button type="submit" class="btn-primary">Start</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Flask
|
||||
python-dotenv
|
43
result.html
Normal file
43
result.html
Normal file
@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>E-Mail Unsubscribe Links Result</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>E-Mail Unsubscribe Links Result</h1>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Email Address</th>
|
||||
<th>Subject</th>
|
||||
<th>Unsubscribe Link</th>
|
||||
<th>Move to Trash</th>
|
||||
</tr>
|
||||
{% for email_id, data in links_data.items() %}
|
||||
<tr>
|
||||
<td>{{ email_id }}</td>
|
||||
<td>{{ data.subject }}</td>
|
||||
<td>
|
||||
{% for link in data.links %}
|
||||
<a href="{{ link }}" target="_blank">{{ link[:30] + '...' }}</a><br>
|
||||
{% endfor %}
|
||||
</td>
|
||||
<td>
|
||||
<form action="{{ url_for('move_to_trash') }}" method="post">
|
||||
<input type="hidden" name="email_id" value="{{ email_id }}">
|
||||
<button type="submit" class="btn-delete">Move to Trash</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<form action="/" method="get">
|
||||
<button type="submit" class="btn-primary">Back to Home</button>
|
||||
</form>
|
||||
<form action="{{ url_for('move_all_to_trash') }}" method="post">
|
||||
<button type="submit" class="btn-delete">Move All to Trash</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
63
static/styles.css
Normal file
63
static/styles.css
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user