app.py aktualisiert
This commit is contained in:
111
app.py
111
app.py
@@ -1,82 +1,53 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash
|
||||
from pytube import YouTube
|
||||
# app.py
|
||||
from flask import Flask, jsonify, request
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import re # Für das Bereinigen von Dateinamen
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'dein_super_geheimer_schlüssel' # Wichtig für Flash-Nachrichten
|
||||
|
||||
DOWNLOAD_FOLDER = 'downloads'
|
||||
if not os.path.exists(DOWNLOAD_FOLDER):
|
||||
os.makedirs(DOWNLOAD_FOLDER)
|
||||
SCRIPT_MAP = {
|
||||
"run_duplicate_check": {
|
||||
"script": "duplicate_checker.py",
|
||||
"args": []
|
||||
},
|
||||
"run_reclassify_branches": {
|
||||
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
|
||||
"args": ["--mode", "reclassify_branches"]
|
||||
},
|
||||
"run_predict_technicians": {
|
||||
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
|
||||
"args": ["--mode", "predict_technicians"]
|
||||
}
|
||||
}
|
||||
|
||||
def sanitize_filename(filename):
|
||||
"""Entfernt ungültige Zeichen aus Dateinamen."""
|
||||
return re.sub(r'[\\/*?:"<>|]', "", filename)
|
||||
@app.route('/run-script', methods=['POST']) # <-- Der korrekte Endpunkt
|
||||
def run_script():
|
||||
data = request.get_json()
|
||||
action = data.get('action')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
try:
|
||||
# Liste nur .mp4 Dateien auf
|
||||
downloaded_files = [f for f in os.listdir(DOWNLOAD_FOLDER) if f.endswith('.mp4')]
|
||||
except FileNotFoundError:
|
||||
downloaded_files = []
|
||||
return render_template('index.html', files=downloaded_files)
|
||||
if not action or action not in SCRIPT_MAP:
|
||||
return jsonify({"status": "error", "message": "Ungültige oder fehlende Aktion."}), 400
|
||||
|
||||
@app.route('/download', methods=['POST'])
|
||||
def download_video():
|
||||
video_url = request.form.get('youtube_url')
|
||||
if not video_url:
|
||||
flash('Bitte gib eine YouTube URL ein.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
script_config = SCRIPT_MAP[action]
|
||||
script_name = script_config["script"]
|
||||
script_args = script_config["args"]
|
||||
|
||||
if not os.path.exists(script_name):
|
||||
logging.error(f"Skript '{script_name}' für Aktion '{action}' nicht gefunden.")
|
||||
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
|
||||
|
||||
try:
|
||||
yt = YouTube(video_url)
|
||||
|
||||
# Wähle den Stream mit der höchsten Auflösung, der progressiv ist (Audio+Video) und mp4 ist
|
||||
stream = yt.streams.filter(progressive=True, file_extension='mp4')\
|
||||
.order_by('resolution')\
|
||||
.desc()\
|
||||
.first()
|
||||
|
||||
if not stream:
|
||||
flash(f'Kein passender MP4 Stream für {yt.title} gefunden.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
# Bereinige den Titel für den Dateinamen
|
||||
filename = sanitize_filename(yt.title) + ".mp4"
|
||||
filepath = os.path.join(DOWNLOAD_FOLDER, filename)
|
||||
|
||||
# Überprüfen, ob die Datei bereits existiert
|
||||
if os.path.exists(filepath):
|
||||
flash(f'Video "{yt.title}" wurde bereits heruntergeladen.', 'info')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
flash(f'Starte Download für: {yt.title}...', 'info')
|
||||
# Wichtig: Um Flask nicht zu blockieren, könnte man hier Threading/Async verwenden,
|
||||
# aber für ein "einfaches" Skript ist dies der direkteste Weg.
|
||||
# Bei langen Downloads kann der Browser einen Timeout anzeigen, obwohl der Download im Hintergrund läuft.
|
||||
stream.download(output_path=DOWNLOAD_FOLDER, filename=filename)
|
||||
flash(f'Video "{yt.title}" erfolgreich heruntergeladen!', 'success')
|
||||
|
||||
logging.info(f"Starte Aktion '{action}' -> Skript: '{script_name}' mit Argumenten: {script_args}")
|
||||
python_executable = sys.executable
|
||||
command = [python_executable, script_name] + script_args
|
||||
subprocess.Popen(command)
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet."}), 200
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print("---------- ERROR DETAILS ----------")
|
||||
print(f"Error type: {type(e)}")
|
||||
print(f"Error message: {str(e)}")
|
||||
print("Traceback:")
|
||||
traceback.print_exc() # Gibt den kompletten Traceback auf der Konsole aus
|
||||
print("---------------------------------")
|
||||
flash(f'Ein Fehler ist aufgetreten: {str(e)}', 'error')
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/play/<filename>')
|
||||
def play_video(filename):
|
||||
# send_from_directory ermöglicht das direkte Abspielen/Herunterladen der Datei
|
||||
return send_from_directory(DOWNLOAD_FOLDER, filename, as_attachment=False) # as_attachment=False versucht, es im Browser abzuspielen
|
||||
logging.error(f"Fehler beim Starten des Skripts für Aktion '{action}': {e}")
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5555) # host='0.0.0.0' macht es im lokalen Netzwerk erreichbar
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user