modified: app.py
This commit is contained in:
31
app.py
31
app.py
@@ -107,28 +107,26 @@ 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."""
|
||||||
# Überprüfen auf gültige user_id
|
if not user_id:
|
||||||
if not user_id or user_id == "":
|
print(f"Invalid user_id: {user_id}. Using default profile picture.")
|
||||||
print(f"Invalid or empty user_id: {user_id}. Using default profile picture.")
|
|
||||||
return "/static/default_profile.png"
|
return "/static/default_profile.png"
|
||||||
|
|
||||||
# Konvertiere user_id sicher zu einem String
|
# Pfad zum lokalen Bild
|
||||||
user_id_str = str(user_id)
|
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
|
||||||
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 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 found for user {user_id}: {local_image_path}")
|
||||||
r.set(user_id_str, local_image_path) # Speichere den Pfad in Redis für zukünftige Anfragen
|
r.set(str(user_id), local_image_path) # Speichere 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(user_id_str)
|
cached_image = r.get(str(user_id))
|
||||||
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')
|
||||||
|
|
||||||
# Wenn das Bild weder lokal noch in Redis vorhanden ist, versuche, es herunterzuladen
|
# Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild 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}")
|
||||||
@@ -140,7 +138,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(user_id_str, local_image_path)
|
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 at {local_image_path}")
|
||||||
return local_image_path
|
return local_image_path
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
@@ -150,12 +148,19 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
|
|||||||
print(f"No avatar URL provided for user {user_id}. Using default profile picture.")
|
print(f"No avatar URL provided for user {user_id}. Using default profile picture.")
|
||||||
return "/static/default_profile.png"
|
return "/static/default_profile.png"
|
||||||
|
|
||||||
def get_profile_picture(user_id, avatar_url):
|
def get_profile_picture(user_id, avatar_url=None):
|
||||||
"""Gibt den Pfad zum Profilbild eines Benutzers zurück oder lädt es herunter, falls es nicht in Redis oder lokal vorhanden ist."""
|
"""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
|
||||||
cached_image = r.get(str(user_id))
|
cached_image = r.get(str(user_id))
|
||||||
if cached_image:
|
if cached_image:
|
||||||
print(f"Using cached image for user {user_id}")
|
print(f"Using cached image for user {user_id}")
|
||||||
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
|
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
|
||||||
|
|
||||||
|
# Falls kein Cache vorhanden ist, überprüfe und lade das Bild herunter
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user