from flask import Flask, request, jsonify, render_template from flask_socketio import SocketIO, emit import json import uuid import os from datetime import datetime app = Flask(__name__) app.config['SECRET_KEY'] = 'your-secret-key' socketio = SocketIO(app) # Job storage with unique IDs and status jobs = [] connected_clients = {} def save_jobs(): """Save jobs to JSON file""" try: with open('jobs.json', 'w') as f: json.dump(jobs, f) except Exception as e: print(f"Error saving jobs: {e}") def load_jobs(): """Load jobs from JSON file""" try: if os.path.exists('jobs.json') and os.path.getsize('jobs.json') > 0: with open('jobs.json', 'r') as f: return json.load(f) except Exception as e: print(f"Error loading jobs: {e}") return [] @app.route('/') def index(): return render_template('index.html') @app.route('/submit_job', methods=['POST']) def submit_job(): youtube_url = request.form.get("youtube_url") exit_time = int(request.form.get("exit_time", 10)) mode = request.form.get("mode", "once") job_id = str(uuid.uuid4()) job = { "job_id": job_id, "youtube_url": youtube_url, "exit_time": exit_time, "mode": mode, "status": "pending" } jobs.append(job) save_jobs() return jsonify({"message": "Job submitted successfully", "job_id": job_id}) @socketio.on('client_info') def handle_client_info(data): client_id = request.sid connected_clients[client_id] = { 'ip': data.get('ip', 'Unknown'), 'country': data.get('country', 'Unknown'), 'city': data.get('city', 'Unknown'), 'connected_at': datetime.now().isoformat() } emit('client_connected', { 'client_id': client_id, 'ip': data.get('ip', 'Unknown'), 'country': data.get('country', 'Unknown'), 'city': data.get('city', 'Unknown') }, broadcast=True) @socketio.on('disconnect') def handle_disconnect(): client_id = request.sid if client_id in connected_clients: del connected_clients[client_id] emit('client_disconnected', client_id, broadcast=True) @app.route('/get_job', methods=['GET']) def get_job(): for job in jobs: if job["status"] == "pending": return jsonify(job) return jsonify({"message": "No pending jobs"}) @app.route('/update_job_status', methods=['POST']) def update_job_status(): data = request.get_json() job_id = data.get("job_id") status = data.get("status", "done") for job in jobs: if job["job_id"] == job_id: job["status"] = status break save_jobs() return jsonify({"message": f"Job {job_id} marked as {status}"}) if __name__ == "__main__": jobs = load_jobs() if not os.path.exists('templates'): os.makedirs('templates') template_path = os.path.join('templates', 'index.html') if not os.path.exists(template_path): with open(template_path, 'w') as f: f.write(''' YouTube Job Manager

YouTube Job Manager

Connected Clients
IP Address Country City Status
Submit New Job
''') socketio.run(app, host="0.0.0.0", port=7860, debug=False, allow_unsafe_werkzeug=True)