modified: app.py
This commit is contained in:
63
app.py
63
app.py
@@ -34,11 +34,9 @@ DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token"
|
|||||||
DISCORD_API_URL = "https://discord.com/api/users/@me"
|
DISCORD_API_URL = "https://discord.com/api/users/@me"
|
||||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
||||||
|
|
||||||
# Redis-Verbindung
|
|
||||||
redis_url = "redis://:Uo28JDAlM3LiwjKyaOgyt08PCjJUKsW8dFKrcmZn3jN2TF074dNkFnQoSpyLo8WL@skgwsss0wc8040o4sgg044gs:6379/0"
|
redis_url = "redis://:Uo28JDAlM3LiwjKyaOgyt08PCjJUKsW8dFKrcmZn3jN2TF074dNkFnQoSpyLo8WL@skgwsss0wc8040o4sgg044gs:6379/0"
|
||||||
r = redis.Redis.from_url(redis_url)
|
r = redis.Redis.from_url(redis_url)
|
||||||
|
|
||||||
# Verzeichnis für Profilbilder
|
|
||||||
PROFILE_IMAGE_DIR = 'static/profile_images'
|
PROFILE_IMAGE_DIR = 'static/profile_images'
|
||||||
os.makedirs(PROFILE_IMAGE_DIR, exist_ok=True)
|
os.makedirs(PROFILE_IMAGE_DIR, exist_ok=True)
|
||||||
|
|
||||||
@@ -105,63 +103,39 @@ def make_discord_session(token=None, state=None):
|
|||||||
token_updater=token_updater
|
token_updater=token_updater
|
||||||
)
|
)
|
||||||
|
|
||||||
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."""
|
|
||||||
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
|
|
||||||
|
|
||||||
print(f"Fetching profile picture for user_id: {user_id}, avatar_url: {avatar_url}")
|
|
||||||
|
|
||||||
# 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}")
|
|
||||||
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) # In Redis zwischenspeichern
|
|
||||||
return local_image_path
|
|
||||||
|
|
||||||
# Wenn weder Redis noch lokal vorhanden, dann herunterladen
|
|
||||||
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
|
||||||
|
|
||||||
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 und speichert es lokal und in Redis."""
|
||||||
if not user_id or not avatar_url:
|
if not user_id or not avatar_url:
|
||||||
print(f"Skipping download for user {user_id}: Invalid user_id or missing avatar URL.")
|
# Fallback auf ein Standardprofilbild, wenn keine gültigen Werte vorhanden sind
|
||||||
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(PROFILE_IMAGE_DIR, f"{user_id}.png")
|
||||||
|
|
||||||
# Überprüfen, ob das Bild bereits lokal vorhanden ist
|
# Checken, ob das Bild schon 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}.")
|
r.set(str(user_id), local_image_path) # Sicherstellen, dass der Key ein String ist
|
||||||
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, herunterladen
|
|
||||||
try:
|
try:
|
||||||
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
|
# Bild von Discord herunterladen und speichern
|
||||||
response = requests.get(avatar_url, timeout=10)
|
response = requests.get(avatar_url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
||||||
# Speichere den Pfad in Redis
|
# Pfad in Redis speichern
|
||||||
r.set(str(user_id), local_image_path)
|
r.set(str(user_id), local_image_path) # Konvertiere user_id zu einem String
|
||||||
print(f"Profile picture for user {user_id} downloaded and cached.")
|
|
||||||
|
|
||||||
return local_image_path
|
return local_image_path
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(f"Failed to download profile picture for user {user_id}: {e}")
|
print(f"Error fetching profile picture for user {user_id}: {e}")
|
||||||
return "/static/default_profile.png"
|
return "/static/default_profile.png" # Standardbild verwenden
|
||||||
|
|
||||||
|
def get_profile_picture(user_id, avatar_url):
|
||||||
|
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis oder durch Abruf."""
|
||||||
|
cached_image = r.get(user_id)
|
||||||
|
if cached_image:
|
||||||
|
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
|
||||||
|
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def utility_processor():
|
def utility_processor():
|
||||||
@@ -169,6 +143,7 @@ def utility_processor():
|
|||||||
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
||||||
return dict(get_profile_picture=get_profile_picture)
|
return dict(get_profile_picture=get_profile_picture)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/update_all_profile_pictures")
|
@app.route("/update_all_profile_pictures")
|
||||||
def update_all_profile_pictures():
|
def update_all_profile_pictures():
|
||||||
"""Aktualisiert alle Profilbilder der Benutzer."""
|
"""Aktualisiert alle Profilbilder der Benutzer."""
|
||||||
|
|||||||
Reference in New Issue
Block a user