Files
2026-07-02 05:52:57 +02:00

48 lines
1.7 KiB
Python

from flask import Flask, request, jsonify
import logging
import threading
# Import your existing script
# NOTE: Ensure octotrigger.py is in the same folder
import octotrigger
# Add this to server.py to see if the logger is alive
logger = logging.getLogger("OctoTrigger")
app = Flask(__name__)
@app.route('/trigger', methods=['POST'])
def trigger_download():
# 1. Get JSON from the request
try:
data = request.get_json(force=True)
logger.info(f"Received request for playlist: {data.get('playlist_name')}")
# Validate input using the function we wrote in octotrigger.py
octotrigger.validate_input(data)
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 400
# 2. Run the script logic in a background thread
# (so the web request doesn't time out while downloading)
def run_job(json_data):
# We need to hack sys.argv or modify main() to accept args directly.
# The cleanest way without rewriting octotrigger.py is to modify
# octotrigger.py slightly to accept a dict in main(),
# but here is a wrapper approach:
# We will create a temporary version of the main logic here
# Or ideally, modify octotrigger.py's main to look like this:
# def run(playlist_data): ...
# For now, let's assume we update octotrigger.py slightly (see below)
octotrigger.run_process(json_data)
thread = threading.Thread(target=run_job, args=(data,))
thread.start()
return jsonify({"status": "accepted", "message": "Job started in background"}), 202
if __name__ == '__main__':
# Listen on all network interfaces (0.0.0.0) on port 5000
app.run(host='0.0.0.0', port=5000)