modified: app.py
modified: templates/leaderboard.html
This commit is contained in:
95
app.py
95
app.py
@@ -6,11 +6,10 @@ 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")
|
||||||
@@ -34,12 +33,6 @@ 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_url = "redis://:Uo28JDAlM3LiwjKyaOgyt08PCjJUKsW8dFKrcmZn3jN2TF074dNkFnQoSpyLo8WL@skgwsss0wc8040o4sgg044gs:6379/0"
|
|
||||||
r = redis.Redis.from_url(redis_url)
|
|
||||||
|
|
||||||
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():
|
||||||
@@ -68,22 +61,14 @@ def stop_bot():
|
|||||||
print("Bot läuft nicht.")
|
print("Bot läuft nicht.")
|
||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
"""Stellt eine Verbindung zur MySQL-Datenbank her und aktiviert die automatische Wiederverbindung."""
|
"""Stellt eine Verbindung zur MySQL-Datenbank her."""
|
||||||
try:
|
return mysql.connector.connect(
|
||||||
connection = mysql.connector.connect(
|
host=DB_HOST,
|
||||||
host=DB_HOST,
|
port=DB_PORT,
|
||||||
port=DB_PORT,
|
user=DB_USER,
|
||||||
user=DB_USER,
|
password=DB_PASS,
|
||||||
password=DB_PASS,
|
database=DB_NAME
|
||||||
database=DB_NAME,
|
)
|
||||||
connection_timeout=300, # Timeout auf 300 Sekunden setzen
|
|
||||||
autocommit=True
|
|
||||||
)
|
|
||||||
connection.reconnect(attempts=3, delay=5) # Versuche die Verbindung dreimal wiederherzustellen, mit 5 Sekunden Abstand
|
|
||||||
return connection
|
|
||||||
except mysql.connector.Error as e:
|
|
||||||
print(f"Database error: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def token_updater(token):
|
def token_updater(token):
|
||||||
session['oauth_token'] = token
|
session['oauth_token'] = token
|
||||||
@@ -103,68 +88,6 @@ 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."""
|
|
||||||
if not user_id or not avatar_url:
|
|
||||||
# Fallback auf ein Standardprofilbild, wenn keine gültigen Werte vorhanden sind
|
|
||||||
return "/static/default_profile.png"
|
|
||||||
|
|
||||||
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(str(user_id), local_image_path) # Sicherstellen, dass der Key ein String ist
|
|
||||||
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(str(user_id), local_image_path) # Konvertiere user_id zu einem String
|
|
||||||
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 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
|
|
||||||
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."""
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{ index }}</td>
|
<td>{{ index }}</td>
|
||||||
<td>
|
<td>
|
||||||
<img src="{{ get_profile_picture(user.user_id, user.profile_picture) }}" alt="Profile Picture" class="profile-picture">
|
<img src="{{'/static/default_profile.png' }}" alt="Profile Picture" class="profile-picture">
|
||||||
</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