modified: app.py
modified: requirements.txt modified: templates/global_admin_dashboard.html modified: templates/leaderboard.html
This commit is contained in:
66
app.py
66
app.py
@@ -6,10 +6,11 @@ from flask import Flask, render_template, redirect, url_for, request, session, j
|
|||||||
from requests_oauthlib import OAuth2Session
|
from requests_oauthlib import OAuth2Session
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import psutil
|
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from flask_session import Session
|
from flask_session import Session
|
||||||
|
import redis
|
||||||
|
import requests
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = os.getenv("FLASK_SECRET_KEY")
|
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"
|
DISCORD_API_URL = "https://discord.com/api/users/@me"
|
||||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
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
|
bot_process = None
|
||||||
|
|
||||||
def bot_status():
|
def bot_status():
|
||||||
@@ -88,6 +94,64 @@ def make_discord_session(token=None, state=None):
|
|||||||
token_updater=token_updater
|
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")
|
@app.route("/logs")
|
||||||
def view_logs():
|
def view_logs():
|
||||||
"""Zeigt die Logs des Bots im Admin-Panel an."""
|
"""Zeigt die Logs des Bots im Admin-Panel an."""
|
||||||
|
|||||||
@@ -14,4 +14,5 @@ python-dotenv
|
|||||||
flask
|
flask
|
||||||
psutil
|
psutil
|
||||||
requests_oauthlib
|
requests_oauthlib
|
||||||
Flask-Session
|
Flask-Session
|
||||||
|
redis
|
||||||
@@ -38,6 +38,7 @@
|
|||||||
<a href="{{ url_for('start') }}" class="btn btn-primary btn-block {{ 'disabled' if g.bot_running else '' }}">Start Bot</a>
|
<a href="{{ url_for('start') }}" class="btn btn-primary btn-block {{ 'disabled' if g.bot_running else '' }}">Start Bot</a>
|
||||||
<a href="{{ url_for('stop') }}" class="btn btn-danger btn-block {{ 'disabled' if not g.bot_running else '' }}">Stop Bot</a>
|
<a href="{{ url_for('stop') }}" class="btn btn-danger btn-block {{ 'disabled' if not g.bot_running else '' }}">Stop Bot</a>
|
||||||
<a href="{{ url_for('view_logs') }}" class="btn btn-warning btn-block">View Logs</a>
|
<a href="{{ url_for('view_logs') }}" class="btn btn-warning btn-block">View Logs</a>
|
||||||
|
<a href="{{ url_for('update_all_profile_pictures') }}" class="btn btn-primary mt-3">Update Profile Pictures</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -39,7 +39,9 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{ index }}</td>
|
<td>{{ index }}</td>
|
||||||
<td>
|
<td>
|
||||||
<img src="{{ user.profile_picture or '/static/default_profile.png' }}" alt="Profile Picture" class="profile-picture">
|
<td>
|
||||||
|
<img src="{{ get_profile_picture(user.user_id, user.profile_picture) }}" alt="Profile Picture" class="profile-picture">
|
||||||
|
</td>
|
||||||
</td>
|
</td>
|
||||||
<td>{{ user.nickname or 'Unknown User' }}</td>
|
<td>{{ user.nickname or 'Unknown User' }}</td>
|
||||||
<td>{{ user.level }}</td>
|
<td>{{ user.level }}</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user