modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 20:07:26 +01:00
parent e4a2a8c427
commit 834e467048

21
app.py
View File

@@ -106,21 +106,27 @@ 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, wenn es nicht lokal vorhanden ist, und speichert es in Redis.""" """Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist."""
if not user_id: if not user_id:
print(f"Invalid user_id: {user_id}. Using default profile picture.") print(f"Invalid user_id: {user_id}. Using default profile picture.")
return "/static/default_profile.png" return "/static/default_profile.png"
# Erstellen des lokalen Pfads für das Bild # Pfad zum lokalen 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")
# Überprüfen, ob das Bild lokal gespeichert ist # Prüfe, ob das Bild lokal vorhanden ist
if os.path.exists(local_image_path): if os.path.exists(local_image_path):
print(f"Local image already exists for user {user_id}: {local_image_path}") 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) # Speichere in Redis für zukünftige Anfragen
return local_image_path return local_image_path
# Wenn das Bild nicht lokal vorhanden ist, versuche, es herunterzuladen # Prüfe Redis für den Pfad
cached_image = r.get(str(user_id))
if cached_image:
print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}")
return cached_image.decode('utf-8')
# Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild herunterzuladen
if avatar_url: if avatar_url:
try: try:
print(f"Downloading profile picture for user {user_id} from {avatar_url}") print(f"Downloading profile picture for user {user_id} from {avatar_url}")
@@ -143,10 +149,11 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
return "/static/default_profile.png" return "/static/default_profile.png"
def get_profile_picture(user_id, avatar_url): def get_profile_picture(user_id, avatar_url):
"""Gibt den Pfad zum Profilbild zurück oder lädt es herunter, wenn es nicht in Redis oder lokal vorhanden ist.""" """Gibt den Pfad zum Profilbild eines Benutzers zurück oder lädt es herunter, falls es nicht in Redis oder lokal vorhanden ist."""
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
if cached_image: if cached_image:
return cached_image.decode('utf-8') print(f"Using cached image for user {user_id}")
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
return fetch_and_cache_profile_picture(user_id, avatar_url) return fetch_and_cache_profile_picture(user_id, avatar_url)
@app.context_processor @app.context_processor