sghorbal commited on
Commit
6f66537
·
1 Parent(s): 9845666

Initial commit

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +81 -0
  3. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ url.json
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import requests
4
+ import threading
5
+ from fastapi import FastAPI
6
+ from fastapi.responses import JSONResponse
7
+ import gradio as gr
8
+
9
+ URLS_FILE = "urls.json"
10
+ INTERVAL = 3600 # 1 heure en secondes
11
+
12
+ app = FastAPI()
13
+
14
+ # --- Gestion des URLs ---
15
+ def load_urls():
16
+ if os.path.exists(URLS_FILE):
17
+ with open(URLS_FILE, "r") as f:
18
+ return json.load(f)
19
+ return []
20
+
21
+ def save_urls(urls):
22
+ with open(URLS_FILE, "w") as f:
23
+ json.dump(urls, f)
24
+
25
+ def add_url(url):
26
+ urls = load_urls()
27
+ if url and url not in urls:
28
+ urls.append(url)
29
+ save_urls(urls)
30
+ return urls
31
+
32
+ def remove_url(url):
33
+ urls = load_urls()
34
+ if url in urls:
35
+ urls.remove(url)
36
+ save_urls(urls)
37
+ return urls
38
+
39
+ def ping_all():
40
+ urls = load_urls()
41
+ for url in urls:
42
+ try:
43
+ requests.get(url, timeout=5)
44
+ print(f"[OK] Ping: {url}")
45
+ except Exception as e:
46
+ print(f"[ERR] {url} -> {e}")
47
+
48
+ # --- Scheduler (appel régulier toutes les heures) ---
49
+ def schedule_ping():
50
+ ping_all()
51
+ threading.Timer(INTERVAL, schedule_ping).start()
52
+
53
+ # Démarrage automatique au lancement
54
+ schedule_ping()
55
+
56
+ # --- Endpoint de vérification ---
57
+ @app.get("/check_health")
58
+ async def check_health():
59
+ return JSONResponse(content={"status": "ok", "message": "App is running"})
60
+
61
+ # --- Interface Gradio ---
62
+ with gr.Blocks() as interface:
63
+ gr.Markdown("## 🌐 Gestionnaire de Keep-Alive")
64
+
65
+ with gr.Row():
66
+ input_url = gr.Textbox(label="Ajouter une URL")
67
+ add_btn = gr.Button("Ajouter")
68
+
69
+ url_list = gr.List(label="URLs suivies", value=load_urls())
70
+
71
+ with gr.Row():
72
+ input_remove = gr.Textbox(label="Supprimer une URL")
73
+ remove_btn = gr.Button("Supprimer")
74
+
75
+ add_btn.click(fn=add_url, inputs=input_url, outputs=url_list)
76
+ remove_btn.click(fn=remove_url, inputs=input_remove, outputs=url_list)
77
+
78
+ # --- Lien entre FastAPI et Gradio ---
79
+ @app.get("/")
80
+ def gradio_ui():
81
+ return gr.mount_gradio_app(app, interface)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ fastapi
3
+ uvicorn
4
+ requests