Mohammed Foud commited on
Commit
cd997c6
·
1 Parent(s): 973f606

Add application file

Browse files
Files changed (2) hide show
  1. app.py +11 -0
  2. templates/index.html +83 -0
app.py CHANGED
@@ -35,11 +35,20 @@ class ProgressCallback:
35
  self.failed_count = 0
36
 
37
  def update_progress(self, current, total, success, failed, comment_text=""):
 
 
38
  self.current_progress = current
39
  self.total_comments = total
40
  self.success_count = success
41
  self.failed_count = failed
42
 
 
 
 
 
 
 
 
43
  # Emit progress update to all connected clients
44
  self.socketio.emit('progress_update', {
45
  'current': current,
@@ -187,6 +196,8 @@ def run_tiktok_bot(video_url, comment_text, num_comments):
187
  @socketio.on('connect')
188
  def handle_connect():
189
  emit('connected', {'message': 'Connected to server'})
 
 
190
 
191
  @socketio.on('disconnect')
192
  def handle_disconnect():
 
35
  self.failed_count = 0
36
 
37
  def update_progress(self, current, total, success, failed, comment_text=""):
38
+ global bot_status
39
+
40
  self.current_progress = current
41
  self.total_comments = total
42
  self.success_count = success
43
  self.failed_count = failed
44
 
45
+ # Update global bot status
46
+ bot_status['current_progress'] = current
47
+ bot_status['total_comments'] = total
48
+ bot_status['success_count'] = success
49
+ bot_status['failed_count'] = failed
50
+ bot_status['current_comment'] = comment_text
51
+
52
  # Emit progress update to all connected clients
53
  self.socketio.emit('progress_update', {
54
  'current': current,
 
196
  @socketio.on('connect')
197
  def handle_connect():
198
  emit('connected', {'message': 'Connected to server'})
199
+ # Send current bot status to the newly connected client
200
+ emit('bot_status', bot_status)
201
 
202
  @socketio.on('disconnect')
203
  def handle_disconnect():
templates/index.html CHANGED
@@ -155,8 +155,46 @@
155
 
156
  let isRunning = false;
157
 
 
 
 
158
  socket.on('connect', function(data) {
159
  addLog('Connected to server', 'success');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  });
161
 
162
  socket.on('bot_started', function(data) {
@@ -196,6 +234,12 @@
196
  addLog(data.message, 'error');
197
  });
198
 
 
 
 
 
 
 
199
  botForm.addEventListener('submit', async function(e) {
200
  e.preventDefault();
201
 
@@ -243,6 +287,45 @@
243
  }
244
  });
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  function updateUI() {
247
  if (isRunning) {
248
  startBtn.disabled = true;
 
155
 
156
  let isRunning = false;
157
 
158
+ // Fetch current bot status when page loads
159
+ fetchCurrentStatus();
160
+
161
  socket.on('connect', function(data) {
162
  addLog('Connected to server', 'success');
163
+ // Fetch current bot status when connecting
164
+ fetchCurrentStatus();
165
+ });
166
+
167
+ socket.on('bot_status', function(data) {
168
+ // Update UI with current bot status from server
169
+ isRunning = data.is_running;
170
+
171
+ if (isRunning) {
172
+ // Update progress section
173
+ progressSection.classList.remove('hidden');
174
+
175
+ // Update progress bar
176
+ const percentage = data.total_comments > 0
177
+ ? Math.round((data.current_progress / data.total_comments) * 100)
178
+ : 0;
179
+ progressBar.style.width = `${percentage}%`;
180
+ progressText.textContent = `${data.current_progress} / ${data.total_comments} (${percentage}%)`;
181
+
182
+ // Update statistics
183
+ successCount.textContent = data.success_count;
184
+ failedCount.textContent = data.failed_count;
185
+
186
+ if (data.current_comment) {
187
+ currentComment.textContent = data.current_comment.length > 20
188
+ ? data.current_comment.substring(0, 20) + '...'
189
+ : data.current_comment;
190
+ }
191
+
192
+ // Update status
193
+ statusText.textContent = data.status_message;
194
+ addLog(`Bot is currently running: ${data.status_message}`, 'info');
195
+ }
196
+
197
+ updateUI();
198
  });
199
 
200
  socket.on('bot_started', function(data) {
 
234
  addLog(data.message, 'error');
235
  });
236
 
237
+ socket.on('bot_stopped', function(data) {
238
+ isRunning = false;
239
+ updateUI();
240
+ addLog(data.message, 'warning');
241
+ });
242
+
243
  botForm.addEventListener('submit', async function(e) {
244
  e.preventDefault();
245
 
 
287
  }
288
  });
289
 
290
+ async function fetchCurrentStatus() {
291
+ try {
292
+ const response = await fetch('/status');
293
+ const status = await response.json();
294
+
295
+ isRunning = status.is_running;
296
+
297
+ if (isRunning) {
298
+ // Update progress section
299
+ progressSection.classList.remove('hidden');
300
+
301
+ // Update progress bar
302
+ const percentage = status.total_comments > 0
303
+ ? Math.round((status.current_progress / status.total_comments) * 100)
304
+ : 0;
305
+ progressBar.style.width = `${percentage}%`;
306
+ progressText.textContent = `${status.current_progress} / ${status.total_comments} (${percentage}%)`;
307
+
308
+ // Update statistics
309
+ successCount.textContent = status.success_count;
310
+ failedCount.textContent = status.failed_count;
311
+
312
+ if (status.current_comment) {
313
+ currentComment.textContent = status.current_comment.length > 20
314
+ ? status.current_comment.substring(0, 20) + '...'
315
+ : status.current_comment;
316
+ }
317
+
318
+ // Update status
319
+ statusText.textContent = status.status_message;
320
+ addLog(`Bot is currently running: ${status.status_message}`, 'info');
321
+ }
322
+
323
+ updateUI();
324
+ } catch (error) {
325
+ addLog(`Error fetching status: ${error.message}`, 'error');
326
+ }
327
+ }
328
+
329
  function updateUI() {
330
  if (isRunning) {
331
  startBtn.disabled = true;