modified: app.py
This commit is contained in:
25
app.py
25
app.py
@@ -107,29 +107,29 @@ 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 fügt es zu Redis hinzu."""
|
||||
if not avatar_url:
|
||||
print(f"Skipping download for user {user_id}: No avatar URL provided.")
|
||||
if not user_id or not avatar_url:
|
||||
print(f"Skipping download for user {user_id}: Invalid user_id or missing avatar URL.")
|
||||
return "/static/default_profile.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):
|
||||
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
|
||||
|
||||
# Falls kein lokales Bild vorhanden ist, wird es heruntergeladen
|
||||
# Falls kein lokales Bild vorhanden, herunterladen
|
||||
try:
|
||||
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
|
||||
response = requests.get(avatar_url, timeout=10)
|
||||
response.raise_for_status()
|
||||
|
||||
# Speichern des Bildes im lokalen Verzeichnis
|
||||
# Speichere das Bild lokal
|
||||
with open(local_image_path, 'wb') as img_file:
|
||||
img_file.write(response.content)
|
||||
|
||||
# In Redis speichern
|
||||
# Speichere den Pfad in Redis
|
||||
r.set(str(user_id), local_image_path)
|
||||
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):
|
||||
"""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:
|
||||
local_path = cached_image.decode('utf-8')
|
||||
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")
|
||||
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
|
||||
r.set(str(user_id), local_image_path) # In Redis zwischenspeichern
|
||||
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)
|
||||
|
||||
@app.context_processor
|
||||
|
||||
Reference in New Issue
Block a user