Filename nun besser lesbar

This commit is contained in:
2026-03-02 15:42:52 +00:00
parent 8e292c1216
commit 26a9a267a0
2 changed files with 36 additions and 2 deletions

37
main.py
View File

@@ -1,6 +1,9 @@
import imaplib
import email
import email.header
import email.utils
import os
import re
import shutil
from datetime import datetime, timedelta
@@ -10,12 +13,15 @@ IMAP_PORT = int(os.getenv("IMAP_PORT", "993"))
EMAIL_USER = os.getenv("EMAIL_USER", "dk2ge@t-online.de")
EMAIL_PASS = os.getenv("EMAIL_PASS", "ETBjw65tf2")
SAVE_DIR = os.getenv("SAVE_DIR", "./videospeicher")
HEARTBEAT_FILE = os.getenv("HEARTBEAT_FILE", "/app/heartbeat.txt")
HEARTBEAT_FILE = os.getenv("HEARTBEAT_FILE", "./heartbeat.txt")
# === HEARTBEAT ===
def update_heartbeat():
"""Schreibt einen Heartbeat für das Monitoring."""
try:
dir_name = os.path.dirname(HEARTBEAT_FILE)
if dir_name:
os.makedirs(dir_name, exist_ok=True)
with open(HEARTBEAT_FILE, "w") as f:
f.write(datetime.now().isoformat())
print(f"💓 Heartbeat aktualisiert: {HEARTBEAT_FILE}")
@@ -72,6 +78,31 @@ def process_mails():
month = parsed_date.strftime("%m")
day = parsed_date.strftime("%d")
# Subject dekodieren
raw_subject = msg.get("Subject", "")
decoded_subject = email.header.decode_header(raw_subject)
subject_parts = []
for part_bytes, charset in decoded_subject:
if isinstance(part_bytes, bytes):
subject_parts.append(part_bytes.decode(charset or "utf-8", errors="replace"))
else:
subject_parts.append(part_bytes)
subject = "".join(subject_parts).strip()
# Kameranummer extrahieren: z. B. "Camera1" → "Camera1"
cam_match = re.search(r'Camera(\d+)', subject, re.IGNORECASE)
cam_name = f"C{cam_match.group(1)}" if cam_match else "C0"
# Datum und Uhrzeit aus Subject extrahieren: z. B. "2026/3/2 16:26:02"
dt_match = re.search(r'(\d{4})/(\d{1,2})/(\d{1,2})\s+(\d{2}):(\d{2}):(\d{2})', subject)
if dt_match:
y, mo, d_s, h, mi, s = dt_match.groups()
subject_dt = f"{y}-{mo.zfill(2)}-{d_s.zfill(2)}_{h}{mi}{s}"
else:
subject_dt = parsed_date.strftime("%Y-%m-%d_%H%M%S")
safe_subject = f"{cam_name}_{subject_dt}"
folder_path = os.path.join(SAVE_DIR, year, month, day)
os.makedirs(folder_path, exist_ok=True)
@@ -81,6 +112,8 @@ def process_mails():
filename = part.get_filename()
if not filename:
filename = "anhang.mp4"
ext = os.path.splitext(filename)[1] or ".mp4"
filename = f"{safe_subject}{ext}"
filepath = os.path.join(folder_path, filename)
with open(filepath, "wb") as f:
@@ -90,7 +123,7 @@ def process_mails():
# Mail nach Verarbeitung löschen
mail.store(num, "+FLAGS", "\\Deleted")
mail.expunge()
# mail.expunge()
mail.logout()