wpms commited on
Commit
bf15325
·
verified ·
1 Parent(s): ea43850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -161
app.py CHANGED
@@ -9,14 +9,14 @@ import schedule
9
 
10
  app = Flask(__name__)
11
 
12
- # Store websites and their status
13
  websites_file = 'websites.json'
14
  history_file = 'history.json'
15
 
16
- # Default websites
17
  default_websites = [
18
  {"id": 1, "name": "Google", "url": "https://www.google.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"},
19
- {"id": 2, "name": "GitHub", "url": "https://github.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"}
 
20
  ]
21
 
22
  def load_websites():
@@ -119,13 +119,11 @@ if not websites:
119
  websites = default_websites.copy()
120
  save_websites(websites)
121
 
122
- # Start scheduler
123
  scheduler_thread = Thread(target=run_scheduler, daemon=True)
124
  scheduler_thread.start()
125
 
126
  check_all_websites()
127
 
128
- # Inline HTML - NO templates folder needed
129
  HTML_TEMPLATE = """
130
  <!DOCTYPE html>
131
  <html lang="en">
@@ -165,45 +163,6 @@ HTML_TEMPLATE = """
165
  .stat-card.uptime i { color: #ffc107; }
166
  .stat-card h3 { font-size: 2rem; margin-bottom: 5px; color: #333; }
167
  .stat-card p { color: #666; font-size: 1rem; }
168
- .add-website-form {
169
- background: white; border-radius: 15px; padding: 30px;
170
- margin-bottom: 30px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
171
- }
172
- .add-website-form h2 {
173
- margin-bottom: 20px; color: #333;
174
- display: flex; align-items: center; gap: 10px;
175
- }
176
- .form-group {
177
- display: flex; gap: 15px; flex-wrap: wrap;
178
- }
179
- .form-group input {
180
- flex: 1; min-width: 200px; padding: 12px 15px;
181
- border: 2px solid #e0e0e0; border-radius: 10px;
182
- font-size: 1rem; transition: border-color 0.3s;
183
- }
184
- .form-group input:focus { outline: none; border-color: #667eea; }
185
- .btn {
186
- padding: 12px 25px; border: none; border-radius: 10px;
187
- font-size: 1rem; cursor: pointer; transition: all 0.3s;
188
- display: inline-flex; align-items: center; gap: 8px;
189
- }
190
- .btn-primary {
191
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
192
- color: white;
193
- }
194
- .btn-primary:hover {
195
- transform: translateY(-2px);
196
- box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
197
- }
198
- .btn-success {
199
- background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
200
- color: white;
201
- }
202
- .btn-danger {
203
- background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);
204
- color: white;
205
- }
206
- .btn-sm { padding: 8px 15px; font-size: 0.9rem; }
207
  .websites-list {
208
  background: white; border-radius: 15px; padding: 30px;
209
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
@@ -257,7 +216,6 @@ HTML_TEMPLATE = """
257
  }
258
  .website-stat-value { font-size: 1.5rem; font-weight: bold; color: #333; }
259
  .website-stat-label { font-size: 0.85rem; color: #666; margin-top: 5px; }
260
- .website-actions { display: flex; gap: 10px; margin-top: 15px; }
261
  .progress-bar {
262
  width: 100%; height: 8px; background: #e9ecef;
263
  border-radius: 4px; overflow: hidden; margin-top: 10px;
@@ -284,8 +242,6 @@ HTML_TEMPLATE = """
284
  .toast.error { background: #dc3545; }
285
  @media (max-width: 768px) {
286
  .header h1 { font-size: 2rem; }
287
- .form-group { flex-direction: column; }
288
- .form-group input { width: 100%; }
289
  .website-header { flex-direction: column; align-items: flex-start; }
290
  }
291
  </style>
@@ -320,17 +276,6 @@ HTML_TEMPLATE = """
320
  </div>
321
  </div>
322
 
323
- <div class="add-website-form">
324
- <h2><i class="fas fa-plus-circle"></i> Add Website</h2>
325
- <div class="form-group">
326
- <input type="text" id="siteName" placeholder="Website Name (e.g., My Blog)">
327
- <input type="url" id="siteUrl" placeholder="Website URL (e.g., https://example.com)">
328
- <button class="btn btn-primary" onclick="addWebsite()">
329
- <i class="fas fa-plus"></i> Add Website
330
- </button>
331
- </div>
332
- </div>
333
-
334
  <div class="websites-list">
335
  <h2>
336
  <span><i class="fas fa-list"></i> Monitored Websites</span>
@@ -373,8 +318,8 @@ HTML_TEMPLATE = """
373
  container.innerHTML = `
374
  <div class="empty-state">
375
  <i class="fas fa-inbox"></i>
376
- <h3>No websites added yet</h3>
377
- <p>Add a website above to start monitoring</p>
378
  </div>
379
  `;
380
  return;
@@ -414,12 +359,6 @@ HTML_TEMPLATE = """
414
  <div class="website-stat-label">Last Checked</div>
415
  </div>
416
  </div>
417
-
418
- <div class="website-actions">
419
- <button class="btn btn-danger btn-sm" onclick="deleteWebsite(${site.id})">
420
- <i class="fas fa-trash"></i> Remove
421
- </button>
422
- </div>
423
  </div>
424
  `).join('');
425
  }
@@ -436,61 +375,6 @@ HTML_TEMPLATE = """
436
  document.getElementById('avgUptime').textContent = avgUptime + '%';
437
  }
438
 
439
- async function addWebsite() {
440
- const nameInput = document.getElementById('siteName');
441
- const urlInput = document.getElementById('siteUrl');
442
-
443
- const name = nameInput.value.trim();
444
- let url = urlInput.value.trim();
445
-
446
- if (!name || !url) {
447
- showToast('Please fill in all fields', 'error');
448
- return;
449
- }
450
-
451
- if (!url.startsWith('http://') && !url.startsWith('https://')) {
452
- url = 'https://' + url;
453
- }
454
-
455
- try {
456
- const response = await fetch('/api/websites', {
457
- method: 'POST',
458
- headers: { 'Content-Type': 'application/json' },
459
- body: JSON.stringify({ name, url })
460
- });
461
-
462
- if (response.ok) {
463
- showToast('Website added successfully!', 'success');
464
- nameInput.value = '';
465
- urlInput.value = '';
466
- loadWebsites();
467
- } else {
468
- showToast('Failed to add website', 'error');
469
- }
470
- } catch (error) {
471
- showToast('Error adding website', 'error');
472
- }
473
- }
474
-
475
- async function deleteWebsite(id) {
476
- if (!confirm('Are you sure you want to remove this website?')) return;
477
-
478
- try {
479
- const response = await fetch(`/api/websites/${id}`, {
480
- method: 'DELETE'
481
- });
482
-
483
- if (response.ok) {
484
- showToast('Website removed successfully!', 'success');
485
- loadWebsites();
486
- } else {
487
- showToast('Failed to remove website', 'error');
488
- }
489
- } catch (error) {
490
- showToast('Error removing website', 'error');
491
- }
492
- }
493
-
494
  async function checkAllNow() {
495
  const btn = document.querySelector('.btn-success');
496
  btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Checking...';
@@ -544,10 +428,6 @@ HTML_TEMPLATE = """
544
  if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
545
  return `${Math.floor(diff / 86400)}d ago`;
546
  }
547
-
548
- document.getElementById('siteUrl').addEventListener('keypress', function(e) {
549
- if (e.key === 'Enter') addWebsite();
550
- });
551
  </script>
552
  </body>
553
  </html>
@@ -561,42 +441,6 @@ def index():
561
  def get_websites():
562
  return jsonify(load_websites())
563
 
564
- @app.route('/api/websites', methods=['POST'])
565
- def add_website():
566
- data = request.json
567
- websites = load_websites()
568
-
569
- new_id = max([w['id'] for w in websites], default=0) + 1
570
-
571
- new_website = {
572
- 'id': new_id,
573
- 'name': data.get('name', 'Unnamed'),
574
- 'url': data['url'],
575
- 'status': 'unknown',
576
- 'uptime': 100,
577
- 'response_time': 0,
578
- 'last_checked': 'Never'
579
- }
580
-
581
- websites.append(new_website)
582
- save_websites(websites)
583
- check_all_websites()
584
-
585
- return jsonify(new_website), 201
586
-
587
- @app.route('/api/websites/<int:website_id>', methods=['DELETE'])
588
- def delete_website(website_id):
589
- websites = load_websites()
590
- websites = [w for w in websites if w['id'] != website_id]
591
- save_websites(websites)
592
-
593
- history = load_history()
594
- if str(website_id) in history:
595
- del history[str(website_id)]
596
- save_history(history)
597
-
598
- return jsonify({'message': 'Website deleted'})
599
-
600
  @app.route('/api/check-now', methods=['POST'])
601
  def check_now():
602
  check_all_websites()
 
9
 
10
  app = Flask(__name__)
11
 
 
12
  websites_file = 'websites.json'
13
  history_file = 'history.json'
14
 
15
+ # Default websites - edit these to add your own
16
  default_websites = [
17
  {"id": 1, "name": "Google", "url": "https://www.google.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"},
18
+ {"id": 2, "name": "GitHub", "url": "https://github.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"},
19
+ {"id": 3, "name": "YouTube", "url": "https://www.youtube.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"}
20
  ]
21
 
22
  def load_websites():
 
119
  websites = default_websites.copy()
120
  save_websites(websites)
121
 
 
122
  scheduler_thread = Thread(target=run_scheduler, daemon=True)
123
  scheduler_thread.start()
124
 
125
  check_all_websites()
126
 
 
127
  HTML_TEMPLATE = """
128
  <!DOCTYPE html>
129
  <html lang="en">
 
163
  .stat-card.uptime i { color: #ffc107; }
164
  .stat-card h3 { font-size: 2rem; margin-bottom: 5px; color: #333; }
165
  .stat-card p { color: #666; font-size: 1rem; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  .websites-list {
167
  background: white; border-radius: 15px; padding: 30px;
168
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
 
216
  }
217
  .website-stat-value { font-size: 1.5rem; font-weight: bold; color: #333; }
218
  .website-stat-label { font-size: 0.85rem; color: #666; margin-top: 5px; }
 
219
  .progress-bar {
220
  width: 100%; height: 8px; background: #e9ecef;
221
  border-radius: 4px; overflow: hidden; margin-top: 10px;
 
242
  .toast.error { background: #dc3545; }
243
  @media (max-width: 768px) {
244
  .header h1 { font-size: 2rem; }
 
 
245
  .website-header { flex-direction: column; align-items: flex-start; }
246
  }
247
  </style>
 
276
  </div>
277
  </div>
278
 
 
 
 
 
 
 
 
 
 
 
 
279
  <div class="websites-list">
280
  <h2>
281
  <span><i class="fas fa-list"></i> Monitored Websites</span>
 
318
  container.innerHTML = `
319
  <div class="empty-state">
320
  <i class="fas fa-inbox"></i>
321
+ <h3>No websites configured</h3>
322
+ <p>Edit app.py to add your websites</p>
323
  </div>
324
  `;
325
  return;
 
359
  <div class="website-stat-label">Last Checked</div>
360
  </div>
361
  </div>
 
 
 
 
 
 
362
  </div>
363
  `).join('');
364
  }
 
375
  document.getElementById('avgUptime').textContent = avgUptime + '%';
376
  }
377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  async function checkAllNow() {
379
  const btn = document.querySelector('.btn-success');
380
  btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Checking...';
 
428
  if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
429
  return `${Math.floor(diff / 86400)}d ago`;
430
  }
 
 
 
 
431
  </script>
432
  </body>
433
  </html>
 
441
  def get_websites():
442
  return jsonify(load_websites())
443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
  @app.route('/api/check-now', methods=['POST'])
445
  def check_now():
446
  check_all_websites()