File size: 7,474 Bytes
9a01763 a7c6564 9a01763 a7c6564 9a01763 ad07929 c5ddf8d 9a01763 c5ddf8d 9a01763 c5ddf8d 9a01763 c5ddf8d 9a01763 c5ddf8d 9a01763 c5ddf8d 9a01763 c5ddf8d 9a01763 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
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('''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Job Manager</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { padding: 20px; }
.connection-status { margin-bottom: 20px; }
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<h1 class="mb-4">YouTube Job Manager</h1>
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Connected Clients</h5>
</div>
<div class="card-body">
<table class="table" id="clientsTable">
<thead>
<tr>
<th>IP Address</th>
<th>Country</th>
<th>City</th>
<th>Status</th>
</tr>
</thead>
<tbody id="clientsTableBody">
</tbody>
</table>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="mb-0">Submit New Job</h5>
</div>
<div class="card-body">
<form action="/submit_job" method="post" class="needs-validation" novalidate>
<div class="mb-3">
<label for="youtube_url" class="form-label">YouTube URL:</label>
<input type="text" class="form-control" id="youtube_url" name="youtube_url" required>
</div>
<div class="mb-3">
<label for="exit_time" class="form-label">Exit Time (seconds):</label>
<input type="number" class="form-control" id="exit_time" name="exit_time" value="10" required>
</div>
<div class="mb-3">
<label for="mode" class="form-label">Mode:</label>
<select class="form-select" id="mode" name="mode">
<option value="once">Once</option>
<option value="again">Again</option>
<option value="continuous">Continuous</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Submit Job</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<script>
const socket = io();
socket.on('connect', () => {
console.log('Connected to server');
});
socket.on('client_connected', (data) => {
const tableBody = document.getElementById('clientsTableBody');
const row = document.createElement('tr');
row.setAttribute('data-client-id', data.client_id);
row.innerHTML = `
<td>${data.ip}</td>
<td>${data.country}</td>
<td>${data.city}</td>
<td><span class="badge bg-success">Connected</span></td>
`;
tableBody.appendChild(row);
});
socket.on('client_disconnected', (clientId) => {
const row = document.querySelector(`tr[data-client-id="${clientId}"]`);
if (row) {
row.querySelector('.badge').className = 'badge bg-danger';
row.querySelector('.badge').textContent = 'Disconnected';
}
});
</script>
</body>
</html>
''')
socketio.run(app, host="0.0.0.0", port=7860 debug=False) |