from flask import Flask, render_template_string, jsonify, request import requests import time import json import os from datetime import datetime from threading import Thread import schedule app = Flask(__name__) websites_file = 'websites.json' history_file = 'history.json' # Default websites - edit these to add your own default_websites = [ {"id": 1, "name": "mc", "url": "https://wpms-wpms.hf.space", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"}, {"id": 2, "name": "api", "url": "https://srinetwork-api.hf.space", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"}, {"id": 3, "name": "uptimer", "url": "https://wpms-uptime.hf.space", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"} ] def load_websites(): if os.path.exists(websites_file): try: with open(websites_file, 'r') as f: return json.load(f) except: return default_websites.copy() return default_websites.copy() def save_websites(websites): with open(websites_file, 'w') as f: json.dump(websites, f, indent=2) def load_history(): if os.path.exists(history_file): try: with open(history_file, 'r') as f: return json.load(f) except: return {} return {} def save_history(history): with open(history_file, 'w') as f: json.dump(history, f, indent=2) def check_website(website): try: start_time = time.time() response = requests.get(website['url'], timeout=30, allow_redirects=True) response_time = round((time.time() - start_time) * 1000, 2) if response.status_code < 400: return { 'status': 'up', 'response_time': response_time, 'status_code': response.status_code } else: return { 'status': 'down', 'response_time': response_time, 'status_code': response.status_code } except requests.exceptions.RequestException as e: return { 'status': 'down', 'response_time': 0, 'error': str(e) } def check_all_websites(): websites = load_websites() history = load_history() for website in websites: result = check_website(website) now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') website['last_checked'] = now website['response_time'] = result['response_time'] if result['status'] == 'up': website['status'] = 'up' else: website['status'] = 'down' site_id = str(website['id']) if site_id not in history: history[site_id] = [] history[site_id].append({ 'timestamp': now, 'status': result['status'], 'response_time': result['response_time'] }) if len(history[site_id]) > 1000: history[site_id] = history[site_id][-1000:] if history[site_id]: up_count = sum(1 for h in history[site_id] if h['status'] == 'up') website['uptime'] = round((up_count / len(history[site_id])) * 100, 2) save_websites(websites) save_history(history) print(f"[{now}] Checked {len(websites)} websites") def run_scheduler(): schedule.every(5).minutes.do(check_all_websites) while True: schedule.run_pending() time.sleep(1) # Initialize websites = load_websites() if not websites: websites = default_websites.copy() save_websites(websites) scheduler_thread = Thread(target=run_scheduler, daemon=True) scheduler_thread.start() check_all_websites() HTML_TEMPLATE = """ Uptime Robot - Website Monitor

Uptime Robot

Monitor your websites 24/7

0

Total Websites

0

Websites Up

0

Websites Down

100%

Average Uptime

Monitored Websites

Loading websites...

""" @app.route('/') def index(): return render_template_string(HTML_TEMPLATE) @app.route('/api/websites') def get_websites(): return jsonify(load_websites()) @app.route('/api/check-now', methods=['POST']) def check_now(): check_all_websites() return jsonify({'message': 'Check completed', 'websites': load_websites()}) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=False)