modified: app.py

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

59
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):
"""Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist."""
if not user_id:
print(f"Invalid user_id: {user_id}. Using default profile picture.")
"""Lädt das Profilbild herunter, speichert es lokal und fügt es zu Redis hinzu."""
if not avatar_url:
print(f"Skipping download for user {user_id}: No avatar URL provided.")
return "/static/default_profile.png"
# Pfad zum lokalen Bild
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
# Prüfe, ob das Bild lokal gespeichert ist
# Überprüfen, ob das Bild lokal bereits gespeichert ist
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 in Redis für zukünftige Anfragen
print(f"Local image already exists for user {user_id}.")
r.set(str(user_id), local_image_path) # Falls lokal vorhanden, zu Redis hinzufügen
return local_image_path
# 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:
# Falls kein lokales Bild vorhanden ist, wird es heruntergeladen
try:
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
response = requests.get(avatar_url, timeout=10)
response.raise_for_status()
# Speichere das Bild lokal
# Speichern des Bildes im lokalen Verzeichnis
with open(local_image_path, 'wb') as img_file:
img_file.write(response.content)
# Speichere den Pfad in Redis
# In Redis speichern
r.set(str(user_id), 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.")
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"
# Prüfe Redis für den Pfad
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf."""
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
# Wenn in Redis gespeichert, Rückgabe des zwischengespeicherten Pfads
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) # 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)
@app.context_processor