modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 20:16:32 +01:00
parent 834e467048
commit fc3ce9e7d1

20
app.py
View File

@@ -107,26 +107,28 @@ 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, speichert es lokal und in Redis, wenn es nicht vorhanden ist.""" """Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist."""
if not user_id: # Überprüfen auf gültige user_id
print(f"Invalid user_id: {user_id}. Using default profile picture.") if not user_id or user_id == "":
print(f"Invalid or empty user_id: {user_id}. Using default profile picture.")
return "/static/default_profile.png" return "/static/default_profile.png"
# Pfad zum lokalen Bild # Konvertiere user_id sicher zu einem String
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") user_id_str = str(user_id)
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id_str}.png")
# Prüfe, ob das Bild lokal vorhanden 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 found for user {user_id}: {local_image_path}")
r.set(str(user_id), local_image_path) # Speichere in Redis für zukünftige Anfragen r.set(user_id_str, local_image_path) # Speichere den Pfad in Redis für zukünftige Anfragen
return local_image_path return local_image_path
# Prüfe Redis für den Pfad # Prüfe Redis für den Pfad
cached_image = r.get(str(user_id)) cached_image = r.get(user_id_str)
if cached_image: if cached_image:
print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}") print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}")
return 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 # Wenn das Bild weder lokal noch in Redis vorhanden ist, versuche, es 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}")
@@ -138,7 +140,7 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
img_file.write(response.content) img_file.write(response.content)
# Speichere den Pfad in Redis # Speichere den Pfad in Redis
r.set(str(user_id), local_image_path) r.set(user_id_str, local_image_path)
print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}") print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}")
return local_image_path return local_image_path
except requests.RequestException as e: except requests.RequestException as e: