From 88f80252d6042578d335ba67e5361c3020c17c2d Mon Sep 17 00:00:00 2001 From: rxf Date: Mon, 29 Sep 2025 15:12:59 +0200 Subject: [PATCH] Erste Version - tut lokal aufm MAC --- .gitignore | 40 ++++++++++++++++++++++ main.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 0 3 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e802c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Projekt-Sachen +videospeicher/ + +# === Python Bytecode === +__pycache__/ +*.py[cod] +*$py.class + +# === Virtuelle Umgebungen === +.venv/ +env/ +venv/ + +# === Build & Distribution === +build/ +dist/ +*.egg-info/ +.eggs/ + +# === Logs & temporäre Dateien === +*.log +*.tmp +*.swp +*.swo + +# === IDE / Editor Dateien === +.vscode/ +.idea/ +*.iml + +# === macOS-spezifisch === +.DS_Store +.AppleDouble +.LSOverride + +# === Python Tools === +.mypy_cache/ +.pytest_cache/ +.coverage +htmlcov/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..45daa3b --- /dev/null +++ b/main.py @@ -0,0 +1,88 @@ +import imaplib +import email +import os +import shutil +from datetime import datetime, timedelta + +# === KONFIGURATION === +IMAP_SERVER = "secureimap.t-online.de" +IMAP_PORT = 993 +EMAIL_USER = "dk2ge@t-online.de" +EMAIL_PASS = "ETBjw65tf2" +SAVE_DIR = "./videospeicher" + +# === ALTE DATEIEN LÖSCHEN (älter als 1 Jahr) === +def cleanup_old_files(base_dir, days=365): + cutoff = datetime.now() - timedelta(days=days) + for root, dirs, files in os.walk(base_dir): + for file in files: + filepath = os.path.join(root, file) + try: + mtime = datetime.fromtimestamp(os.path.getmtime(filepath)) + if mtime < cutoff: + print(f"Lösche altes Video: {filepath}") + os.remove(filepath) + except Exception as e: + print(f"Fehler beim Löschen von {filepath}: {e}") + + # leere Ordner aufräumen + for root, dirs, files in os.walk(base_dir, topdown=False): + for d in dirs: + dirpath = os.path.join(root, d) + if not os.listdir(dirpath): + shutil.rmtree(dirpath) + +# === MAILS VERARBEITEN === +def process_mails(): + mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT) + mail.login(EMAIL_USER, EMAIL_PASS) + mail.select("INBOX") + + status, messages = mail.search(None, "ALL") + if status != "OK": + print("Keine Mails gefunden.") + return + + for num in messages[0].split(): + status, msg_data = mail.fetch(num, "(RFC822)") + if status != "OK": + continue + + msg = email.message_from_bytes(msg_data[0][1]) + + # Datum für Ordnerstruktur bestimmen + mail_date = msg["Date"] + try: + parsed_date = email.utils.parsedate_to_datetime(mail_date) + except Exception: + parsed_date = datetime.now() + + year = parsed_date.strftime("%Y") + month = parsed_date.strftime("%m") + day = parsed_date.strftime("%d") + + folder_path = os.path.join(SAVE_DIR, year, month, day) + os.makedirs(folder_path, exist_ok=True) + + # Anhänge speichern + for part in msg.walk(): + if part.get_content_disposition() == "attachment": + filename = part.get_filename() + if not filename: + filename = "anhang.mp4" + filepath = os.path.join(folder_path, filename) + + with open(filepath, "wb") as f: + f.write(part.get_payload(decode=True)) + print(f"Gespeichert: {filepath}") + + # Mail nach Verarbeitung löschen + mail.store(num, "+FLAGS", "\\Deleted") + + mail.expunge() + mail.logout() + + +if __name__ == "__main__": + cleanup_old_files(SAVE_DIR, days=365) + process_mails() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29