88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
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() |