modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 21:48:31 +01:00
parent 86e8c5f755
commit 44f93d284a

25
app.py
View File

@@ -107,29 +107,29 @@ 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 fügt es zu Redis hinzu.""" """Lädt das Profilbild herunter, speichert es lokal und fügt es zu Redis hinzu."""
if not avatar_url: if not user_id or not avatar_url:
print(f"Skipping download for user {user_id}: No avatar URL provided.") print(f"Skipping download for user {user_id}: Invalid user_id or missing avatar URL.")
return "/static/default_profile.png" return "/static/default_profile.png"
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png") local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
# Überprüfen, ob das Bild lokal bereits gespeichert ist # Überprüfen, ob das Bild bereits 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}.") 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 r.set(str(user_id), local_image_path) # In Redis hinzufügen
return local_image_path return local_image_path
# Falls kein lokales Bild vorhanden ist, wird es heruntergeladen # Falls kein lokales Bild vorhanden, herunterladen
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}")
response = requests.get(avatar_url, timeout=10) response = requests.get(avatar_url, timeout=10)
response.raise_for_status() response.raise_for_status()
# Speichern des Bildes im lokalen Verzeichnis # Speichere das Bild lokal
with open(local_image_path, 'wb') as img_file: with open(local_image_path, 'wb') as img_file:
img_file.write(response.content) img_file.write(response.content)
# In Redis speichern # Speichere den Pfad in Redis
r.set(str(user_id), local_image_path) r.set(str(user_id), local_image_path)
print(f"Profile picture for user {user_id} downloaded and cached.") print(f"Profile picture for user {user_id} downloaded and cached.")
@@ -140,9 +140,12 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
def get_profile_picture(user_id, avatar_url=None): 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.""" """Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf."""
cached_image = r.get(str(user_id)) 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
# Wenn in Redis gespeichert, Rückgabe des zwischengespeicherten Pfads # In Redis nach dem Bild suchen
cached_image = r.get(str(user_id))
if cached_image: if cached_image:
local_path = cached_image.decode('utf-8') local_path = cached_image.decode('utf-8')
print(f"Redis cache hit for user {user_id}: {local_path}") print(f"Redis cache hit for user {user_id}: {local_path}")
@@ -152,10 +155,10 @@ def get_profile_picture(user_id, avatar_url=None):
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png") local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
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 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 r.set(str(user_id), local_image_path) # In Redis zwischenspeichern
return local_image_path return local_image_path
# Falls weder Redis noch lokal vorhanden, wird der Download initiiert # Wenn weder Redis noch lokal vorhanden, dann herunterladen
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