modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 20:22:56 +01:00
parent fc3ce9e7d1
commit 38093149c2

31
app.py
View File

@@ -107,28 +107,26 @@ def make_discord_session(token=None, state=None):
def fetch_and_cache_profile_picture(user_id, avatar_url):
"""Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist."""
# Überprüfen auf gültige user_id
if not user_id or user_id == "":
print(f"Invalid or empty user_id: {user_id}. Using default profile picture.")
if not user_id:
print(f"Invalid user_id: {user_id}. Using default profile picture.")
return "/static/default_profile.png"
# Konvertiere user_id sicher zu einem String
user_id_str = str(user_id)
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id_str}.png")
# Pfad zum lokalen Bild
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
# Prüfe, ob das Bild lokal vorhanden ist
# Prüfe, ob das Bild lokal gespeichert ist
if os.path.exists(local_image_path):
print(f"Local image found for user {user_id}: {local_image_path}")
r.set(user_id_str, local_image_path) # Speichere den Pfad in Redis für zukünftige Anfragen
r.set(str(user_id), local_image_path) # Speichere in Redis für zukünftige Anfragen
return local_image_path
# Prüfe Redis für den Pfad
cached_image = r.get(user_id_str)
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')
# Wenn das Bild weder lokal noch in Redis vorhanden ist, versuche, es herunterzuladen
# Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild herunterzuladen
if avatar_url:
try:
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
@@ -140,7 +138,7 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
img_file.write(response.content)
# Speichere den Pfad in Redis
r.set(user_id_str, local_image_path)
r.set(str(user_id), local_image_path)
print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}")
return local_image_path
except requests.RequestException as e:
@@ -150,12 +148,19 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
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):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück oder lädt es herunter, falls es nicht in Redis oder lokal vorhanden ist."""
def get_profile_picture(user_id, avatar_url=None):
"""Gibt den Pfad zum Profilbild zurück oder lädt es herunter, falls es nicht in Redis oder lokal vorhanden ist."""
if not user_id:
print(f"Invalid user_id passed to get_profile_picture: {user_id}")
return "/static/default_profile.png"
# Prüfe Redis für den Pfad
cached_image = r.get(str(user_id))
if cached_image:
print(f"Using cached image for user {user_id}")
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
# Falls kein Cache vorhanden ist, überprüfe und lade das Bild herunter
return fetch_and_cache_profile_picture(user_id, avatar_url)
@app.context_processor