Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
from flask import Flask
|
| 5 |
+
import datetime
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# --- Configuration ---
|
| 10 |
+
|
| 11 |
+
# List of Hugging Face Spaces to ping every 1 minute
|
| 12 |
+
HF_SPACES_URLS = [
|
| 13 |
+
"https://suraj2008-vjvjgjgjgififi.hf.space",
|
| 14 |
+
"https://suraj2008-vjvjgjgjgififi2.hf.space",
|
| 15 |
+
"https://suraj2008-vjvjgjgjgififi3.hf.space",
|
| 16 |
+
"https://suraj2008-vjvjgjgjgififi4.hf.space",
|
| 17 |
+
"https://suraj2008-vjvjgjgjgififi5.hf.space",
|
| 18 |
+
"https://suraj2008-vjvjgjgjgififi6.hf.space",
|
| 19 |
+
"https://suraj2008-vjvjgjgjgififi7.hf.space",
|
| 20 |
+
"https://suraj2008-oikvtdvbhh.hf.space"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# Render URL to ping every 5 minutes
|
| 24 |
+
RENDER_URL = "https://file-to-stream-1-t2t9.onrender.com"
|
| 25 |
+
|
| 26 |
+
# Global logs to display on the web page
|
| 27 |
+
logs = []
|
| 28 |
+
|
| 29 |
+
def add_log(message):
|
| 30 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 31 |
+
log_entry = f"[{timestamp}] {message}"
|
| 32 |
+
print(log_entry)
|
| 33 |
+
logs.append(log_entry)
|
| 34 |
+
# Keep only last 50 logs to save memory
|
| 35 |
+
if len(logs) > 50:
|
| 36 |
+
logs.pop(0)
|
| 37 |
+
|
| 38 |
+
def worker_hf():
|
| 39 |
+
"""Function to ping HF spaces every 1 minute"""
|
| 40 |
+
while True:
|
| 41 |
+
add_log("--- Starting HF Spaces Ping Cycle ---")
|
| 42 |
+
for url in HF_SPACES_URLS:
|
| 43 |
+
try:
|
| 44 |
+
response = requests.get(url, timeout=10)
|
| 45 |
+
add_log(f"Pinged {url} | Status: {response.status_code}")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
add_log(f"Failed to ping {url} | Error: {str(e)}")
|
| 48 |
+
|
| 49 |
+
add_log("--- HF Cycle Complete. Waiting 60 seconds... ---")
|
| 50 |
+
time.sleep(60)
|
| 51 |
+
|
| 52 |
+
def worker_render():
|
| 53 |
+
"""Function to ping Render URL every 5 minutes"""
|
| 54 |
+
while True:
|
| 55 |
+
add_log("--- Starting Render Ping Cycle ---")
|
| 56 |
+
try:
|
| 57 |
+
response = requests.get(RENDER_URL, timeout=10)
|
| 58 |
+
add_log(f"Pinged {RENDER_URL} | Status: {response.status_code}")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
add_log(f"Failed to ping {RENDER_URL} | Error: {str(e)}")
|
| 61 |
+
|
| 62 |
+
add_log("--- Render Cycle Complete. Waiting 5 minutes... ---")
|
| 63 |
+
time.sleep(300)
|
| 64 |
+
|
| 65 |
+
# Start background threads
|
| 66 |
+
t1 = threading.Thread(target=worker_hf)
|
| 67 |
+
t2 = threading.Thread(target=worker_render)
|
| 68 |
+
t1.daemon = True
|
| 69 |
+
t2.daemon = True
|
| 70 |
+
t1.start()
|
| 71 |
+
t2.start()
|
| 72 |
+
|
| 73 |
+
@app.route('/')
|
| 74 |
+
def home():
|
| 75 |
+
# Simple HTML page to show the status logs
|
| 76 |
+
log_html = "<br>".join(reversed(logs))
|
| 77 |
+
return f"""
|
| 78 |
+
<html>
|
| 79 |
+
<head>
|
| 80 |
+
<title>Uptime Robot Status</title>
|
| 81 |
+
<meta http-equiv="refresh" content="30"> <style>
|
| 82 |
+
body {{ font-family: monospace; background-color: #1e1e1e; color: #00ff00; padding: 20px; }}
|
| 83 |
+
h1 {{ color: #ffffff; }}
|
| 84 |
+
.log-container {{ background-color: #000; padding: 15px; border-radius: 5px; border: 1px solid #333; }}
|
| 85 |
+
</style>
|
| 86 |
+
</head>
|
| 87 |
+
<body>
|
| 88 |
+
<h1>Uptime Robot Status</h1>
|
| 89 |
+
<p><strong>Targets:</strong> {len(HF_SPACES_URLS)} HF Spaces (1m) | 1 Render App (5m)</p>
|
| 90 |
+
<div class="log-container">
|
| 91 |
+
{log_html}
|
| 92 |
+
</div>
|
| 93 |
+
</body>
|
| 94 |
+
</html>
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
if __name__ == '__main__':
|
| 98 |
+
# Run on port 7860 which is standard for HF Spaces
|
| 99 |
+
app.run(host='0.0.0.0', port=7860)
|
| 100 |
+
|