From 623edd4752fe968fbd562d40fe3547575a1e317e Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:20:25 +0100 Subject: [PATCH] modified: app.py modified: requirements.txt modified: templates/global_admin_dashboard.html modified: templates/leaderboard.html --- app.py | 66 ++++++++++++++++++++++++++- requirements.txt | 3 +- templates/global_admin_dashboard.html | 1 + templates/leaderboard.html | 4 +- 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index da24d07..4bdea0a 100644 --- a/app.py +++ b/app.py @@ -6,10 +6,11 @@ from flask import Flask, render_template, redirect, url_for, request, session, j from requests_oauthlib import OAuth2Session import os import subprocess -import psutil import mysql.connector from datetime import datetime, timedelta from flask_session import Session +import redis +import requests app = Flask(__name__) app.secret_key = os.getenv("FLASK_SECRET_KEY") @@ -33,6 +34,11 @@ DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token" DISCORD_API_URL = "https://discord.com/api/users/@me" os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' +r = redis.Redis(host='localhost', port=6379, db=0) # Redis-Verbindung + +PROFILE_IMAGE_DIR = 'static/profile_images' +os.makedirs(PROFILE_IMAGE_DIR, exist_ok=True) + bot_process = None def bot_status(): @@ -88,6 +94,64 @@ def make_discord_session(token=None, state=None): token_updater=token_updater ) +def fetch_and_cache_profile_picture(user_id, avatar_url): + """Lädt das Profilbild herunter und speichert es lokal und in Redis.""" + local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") + + # Checken, ob das Bild schon lokal vorhanden ist + if os.path.exists(local_image_path): + r.set(user_id, local_image_path) + return local_image_path + + try: + # Bild von Discord herunterladen und speichern + response = requests.get(avatar_url) + response.raise_for_status() + with open(local_image_path, 'wb') as img_file: + img_file.write(response.content) + + # Pfad in Redis speichern + r.set(user_id, local_image_path) + return local_image_path + except requests.RequestException as e: + print(f"Error fetching profile picture for user {user_id}: {e}") + return "/static/default_profile.png" # Standardbild + +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 +def utility_processor(): + def get_profile_picture(user_id, avatar_url): + return fetch_and_cache_profile_picture(user_id, avatar_url) + return dict(get_profile_picture=get_profile_picture) + + +@app.route("/update_all_profile_pictures") +def update_all_profile_pictures(): + """Aktualisiert alle Profilbilder der Benutzer.""" + connection = get_db_connection() + cursor = connection.cursor(dictionary=True) + + # Alle Benutzer mit Avatar-URL abrufen + cursor.execute("SELECT user_id, profile_picture FROM user_data") + users = cursor.fetchall() + + for user in users: + user_id = user['user_id'] + avatar_url = user['profile_picture'] + if avatar_url: + fetch_and_cache_profile_picture(user_id, avatar_url) + + cursor.close() + connection.close() + flash("Profile pictures updated successfully!", "success") + return redirect(url_for("global_admin_dashboard")) + @app.route("/logs") def view_logs(): """Zeigt die Logs des Bots im Admin-Panel an.""" diff --git a/requirements.txt b/requirements.txt index 649da83..52bd315 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,5 @@ python-dotenv flask psutil requests_oauthlib -Flask-Session \ No newline at end of file +Flask-Session +redis \ No newline at end of file diff --git a/templates/global_admin_dashboard.html b/templates/global_admin_dashboard.html index 7744673..e003b95 100644 --- a/templates/global_admin_dashboard.html +++ b/templates/global_admin_dashboard.html @@ -38,6 +38,7 @@ Start Bot Stop Bot View Logs + Update Profile Pictures diff --git a/templates/leaderboard.html b/templates/leaderboard.html index cf02f07..448d9b6 100644 --- a/templates/leaderboard.html +++ b/templates/leaderboard.html @@ -39,7 +39,9 @@ {{ index }} - Profile Picture + + Profile Picture + {{ user.nickname or 'Unknown User' }} {{ user.level }}