Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify, render_template_string, send_from_directory | |
| import subprocess | |
| import os | |
| app = Flask(__name__) | |
| # HTML para la interfaz web | |
| html_form = """ | |
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
| <title>SteamCMD Workshop Downloader</title> | |
| </head> | |
| <body> | |
| <h1>Bienvenido al descargador de la Steam Workshop con SteamCMD!</h1> | |
| <form action="/execute" method="post"> | |
| <label for="command">Ingresa el comando de SteamCMD:</label><br> | |
| <input type="text" id="command" name="command" required><br><br> | |
| <input type="submit" value="Ejecutar"> | |
| </form> | |
| <div id="output"> | |
| {% if output %} | |
| <h3>Resultado:</h3> | |
| <pre>{{ output }}</pre> | |
| {% endif %} | |
| {% if downloads %} | |
| <h3>Archivos descargados:</h3> | |
| <ul> | |
| {% for file in downloads %} | |
| <li><a href="/download/{{ file }}" download>{{ file }}</a></li> | |
| {% endfor %} | |
| </ul> | |
| {% endif %} | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| # Ruta principal para mostrar el formulario HTML | |
| def home(): | |
| return render_template_string(html_form) | |
| # Ruta para ejecutar comandos de steamcmd | |
| def execute(): | |
| try: | |
| command = request.form.get('command') | |
| if not command: | |
| return render_template_string(html_form, output='No se proporcionó ningún comando') | |
| # Añadir el comando base para steamcmd y asegurar que se ejecuta en el directorio correcto | |
| steamcmd_command = f"cd /steamcmd && chmod -R 755 ./linux32 && ./steamcmd.sh +login anonymous {command} +quit" | |
| # Ejecutar el comando en la terminal | |
| result = subprocess.run(steamcmd_command, shell=True, capture_output=True, text=True) | |
| # Obtener la lista de archivos descargados | |
| download_path = "/steamcmd/steamapps/workshop/content" | |
| downloads = [] | |
| if os.path.exists(download_path): | |
| for root, dirs, files in os.walk(download_path): | |
| for file in files: | |
| downloads.append(os.path.relpath(os.path.join(root, file), start=download_path)) | |
| # Retornar la salida del comando y los archivos descargados | |
| if result.returncode == 0: | |
| return render_template_string(html_form, output=result.stdout, downloads=downloads) | |
| else: | |
| return render_template_string(html_form, output=result.stderr, downloads=downloads) | |
| except Exception as e: | |
| return render_template_string(html_form, output=str(e)) | |
| # Ruta para descargar los archivos | |
| def download_file(filename): | |
| download_path = "/steamcmd/steamapps/workshop/content" | |
| return send_from_directory(download_path, filename, as_attachment=True) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) |