from flask import Flask, jsonify
from flask_cors import CORS
import subprocess

app = Flask(__name__)

CORS(app)

@app.route('/api/data/binance', methods=['GET'])
def binance_data():
    try:
        # Run the total.py file and capture its output
        result = subprocess.run(
            ['python3', 'total.py'],  # Command to execute total.py
            capture_output=True,     # Capture stdout and stderr
            text=True                # Return output as a string
        )

        #also jsut run orderbook_levels.py
        result2 = subprocess.run(
            ['python3', 'orderbook_levels.py']
        )

        # Check if the command executed successfully
        if result.returncode == 0:
            # Return the output of total.py as the API response
            return jsonify({"status": "success", "output": result.stdout})
        else:
            # If there was an error, return the error message
            return jsonify({"status": "error", "error": result.stderr}), 500

    except Exception as e:
        # Handle any unexpected errors
        return jsonify({"status": "error", "error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
