modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 19:58:49 +01:00
parent 6dec96ebd9
commit e4a2a8c427

71
app.py
View File

@@ -106,57 +106,48 @@ def make_discord_session(token=None, state=None):
) )
def fetch_and_cache_profile_picture(user_id, avatar_url): def fetch_and_cache_profile_picture(user_id, avatar_url):
"""Lädt das Profilbild herunter und speichert es lokal und in Redis.""" """Lädt das Profilbild herunter, wenn es nicht lokal vorhanden ist, und speichert es in Redis."""
if not user_id or not avatar_url: if not user_id:
# Fallback auf ein Standardprofilbild, wenn keine gültigen Werte vorhanden sind print(f"Invalid user_id: {user_id}. Using default profile picture.")
print(f"Skipping user {user_id}: Missing avatar URL.")
return "/static/default_profile.png" return "/static/default_profile.png"
# Erstellen des lokalen Pfads für das Bild
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
# Prüfen, ob das Bild bereits lokal gespeichert ist # Überprüfen, ob das Bild lokal gespeichert ist
if os.path.exists(local_image_path): if os.path.exists(local_image_path):
print(f"Profile picture for user {user_id} already exists locally.") print(f"Local image already exists for user {user_id}: {local_image_path}")
r.set(str(user_id), local_image_path) r.set(str(user_id), local_image_path)
return local_image_path return local_image_path
try: # Wenn das Bild nicht lokal vorhanden ist, versuche, es herunterzuladen
# Bild von der URL herunterladen und speichern if avatar_url:
response = requests.get(avatar_url, timeout=10) try:
response.raise_for_status() print(f"Downloading profile picture for user {user_id} from {avatar_url}")
response = requests.get(avatar_url, timeout=10)
with open(local_image_path, 'wb') as img_file: response.raise_for_status()
img_file.write(response.content)
# Speichere das Bild lokal
# Pfad in Redis speichern with open(local_image_path, 'wb') as img_file:
r.set(str(user_id), local_image_path) img_file.write(response.content)
print(f"Profile picture for user {user_id} downloaded and cached.")
# Speichere den Pfad in Redis
return local_image_path r.set(str(user_id), local_image_path)
except requests.RequestException as e: print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}")
print(f"Error fetching profile picture for user {user_id}: {e}") return local_image_path
return "/static/default_profile.png" except requests.RequestException as e:
print(f"Failed to download profile picture for user {user_id}: {e}")
return "/static/default_profile.png"
print(f"No avatar URL provided for user {user_id}. Using default profile picture.")
return "/static/default_profile.png"
def get_profile_picture(user_id, avatar_url=None): def get_profile_picture(user_id, avatar_url):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück. Nutzt Redis oder den lokalen Speicher.""" """Gibt den Pfad zum Profilbild zurück oder lädt es herunter, wenn es nicht in Redis oder lokal vorhanden ist."""
# 1. Versuche, den Pfad aus Redis abzurufen
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
if cached_image: if cached_image:
local_path = cached_image.decode('utf-8') return cached_image.decode('utf-8')
print(f"Redis cache hit for user {user_id}: {local_path}") return fetch_and_cache_profile_picture(user_id, avatar_url)
return local_path
# 2. Überprüfen, ob das Bild lokal im `profile_images`-Ordner existiert
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
if os.path.exists(local_image_path):
print(f"Local image found for user {user_id}: {local_image_path}")
# Speichere den Pfad in Redis, um ihn für zukünftige Anfragen zwischenspeichern
r.set(str(user_id), local_image_path)
return local_image_path
# 3. Wenn weder Redis noch ein lokales Bild vorhanden ist, gib das Standardbild zurück
print(f"No profile picture found for user {user_id}, using default image.")
return "/static/default_profile.png"
@app.context_processor @app.context_processor
def utility_processor(): def utility_processor():