modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 21:44:03 +01:00
parent 38093149c2
commit 86e8c5f755

85
app.py
View File

@@ -106,61 +106,56 @@ 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 fügt es zu Redis hinzu."""
if not user_id: if not avatar_url:
print(f"Invalid user_id: {user_id}. Using default profile picture.") print(f"Skipping download for user {user_id}: No avatar URL provided.")
return "/static/default_profile.png" return "/static/default_profile.png"
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
# Pfad zum lokalen Bild # Überprüfen, ob das Bild lokal bereits gespeichert ist
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
# Prüfe, ob das Bild lokal gespeichert ist
if os.path.exists(local_image_path): if os.path.exists(local_image_path):
print(f"Local image found for user {user_id}: {local_image_path}") print(f"Local image already exists for user {user_id}.")
r.set(str(user_id), local_image_path) # Speichere in Redis für zukünftige Anfragen r.set(str(user_id), local_image_path) # Falls lokal vorhanden, zu Redis hinzufügen
return local_image_path return local_image_path
# Prüfe Redis für den Pfad # Falls kein lokales Bild vorhanden ist, wird es heruntergeladen
cached_image = r.get(str(user_id)) try:
if cached_image: print(f"Downloading profile picture for user {user_id} from {avatar_url}")
print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}") response = requests.get(avatar_url, timeout=10)
return cached_image.decode('utf-8') response.raise_for_status()
# Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild herunterzuladen # Speichern des Bildes im lokalen Verzeichnis
if avatar_url: with open(local_image_path, 'wb') as img_file:
try: img_file.write(response.content)
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
response = requests.get(avatar_url, timeout=10) # In Redis speichern
response.raise_for_status() r.set(str(user_id), local_image_path)
print(f"Profile picture for user {user_id} downloaded and cached.")
# Speichere das Bild lokal
with open(local_image_path, 'wb') as img_file: return local_image_path
img_file.write(response.content) except requests.RequestException as e:
print(f"Failed to download profile picture for user {user_id}: {e}")
# Speichere den Pfad in Redis
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:
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):
"""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" return "/static/default_profile.png"
# Prüfe Redis für den Pfad 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."""
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
# Wenn in Redis gespeichert, Rückgabe des zwischengespeicherten Pfads
if cached_image: if cached_image:
print(f"Using cached image for user {user_id}") local_path = cached_image.decode('utf-8')
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis print(f"Redis cache hit for user {user_id}: {local_path}")
return local_path
# Falls kein Cache vorhanden ist, überprüfe und lade das Bild herunter # 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) # Speichere den Pfad in Redis für zukünftige Anfragen
return local_image_path
# Falls weder Redis noch lokal vorhanden, wird der Download initiiert
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