modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 21:56:03 +01:00
parent 44f93d284a
commit 6d6efa6596

48
app.py
View File

@@ -105,6 +105,31 @@ def make_discord_session(token=None, state=None):
token_updater=token_updater
)
def get_profile_picture(user_id, avatar_url=None):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf."""
if not user_id:
print("Error: user_id is undefined or None. Returning default profile picture.")
return "/static/default_profile.png" # Rückgabe des Standardbildes bei ungültiger user_id
print(f"Fetching profile picture for user_id: {user_id}, avatar_url: {avatar_url}")
# In Redis nach dem Bild suchen
cached_image = r.get(str(user_id))
if cached_image:
local_path = cached_image.decode('utf-8')
print(f"Redis cache hit for user {user_id}: {local_path}")
return local_path
# Lokale Überprüfung, falls nicht in Redis vorhanden
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}")
r.set(str(user_id), local_image_path) # In Redis zwischenspeichern
return local_image_path
# Wenn weder Redis noch lokal vorhanden, dann herunterladen
return 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 fügt es zu Redis hinzu."""
if not user_id or not avatar_url:
@@ -138,29 +163,6 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
print(f"Failed to download profile picture for user {user_id}: {e}")
return "/static/default_profile.png"
def get_profile_picture(user_id, avatar_url=None):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf."""
if not user_id:
print("Error: user_id is undefined or None. Returning default profile picture.")
return "/static/default_profile.png" # Rückgabe des Standardbildes bei ungültiger user_id
# In Redis nach dem Bild suchen
cached_image = r.get(str(user_id))
if cached_image:
local_path = cached_image.decode('utf-8')
print(f"Redis cache hit for user {user_id}: {local_path}")
return local_path
# Lokale Überprüfung, falls nicht in Redis vorhanden
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}")
r.set(str(user_id), local_image_path) # In Redis zwischenspeichern
return local_image_path
# Wenn weder Redis noch lokal vorhanden, dann herunterladen
return fetch_and_cache_profile_picture(user_id, avatar_url)
@app.context_processor
def utility_processor():
def get_profile_picture(user_id, avatar_url):