Delete app.py
Browse files
app.py
DELETED
|
@@ -1,235 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from keep_alive import start_ping_server
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
import threading
|
| 6 |
-
import time
|
| 7 |
-
from datetime import datetime, timedelta
|
| 8 |
-
from keep_alive import start_ping_server
|
| 9 |
-
from renewer import (
|
| 10 |
-
load_accounts, save_accounts,
|
| 11 |
-
get_host, renew_account, run_renewals,
|
| 12 |
-
ACCOUNTS_FILE
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
SCHEDULER_RUNNING = False
|
| 16 |
-
SCHEDULER_THREAD = None
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# ─────────────────────────────────────────────
|
| 20 |
-
# Helpers
|
| 21 |
-
# ─────────────────────────────────────────────
|
| 22 |
-
|
| 23 |
-
def accounts_table():
|
| 24 |
-
accounts = load_accounts()
|
| 25 |
-
if not accounts:
|
| 26 |
-
return "📭 لا توجد حسابات مضافة بعد."
|
| 27 |
-
rows = []
|
| 28 |
-
for a in accounts:
|
| 29 |
-
last = a.get("last_renewed", "—")
|
| 30 |
-
nxt = a.get("next_renewal", "—")
|
| 31 |
-
host = a.get("host", "—")
|
| 32 |
-
rows.append(
|
| 33 |
-
f"| **{a['username']}** | {host} | {last[:16] if last != '—' else '—'} | {nxt[:16] if nxt != '—' else '—'} |"
|
| 34 |
-
)
|
| 35 |
-
header = "| المستخدم | الخادم | آخر تجديد | التجديد القادم |\n|---|---|---|---|"
|
| 36 |
-
return header + "\n" + "\n".join(rows)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def log_box(msg, prev=""):
|
| 40 |
-
ts = datetime.utcnow().strftime("%H:%M:%S")
|
| 41 |
-
line = f"[{ts}] {msg}"
|
| 42 |
-
return (prev + "\n" + line).strip()
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# ─────────────────────────────────────────────
|
| 46 |
-
# Actions
|
| 47 |
-
# ─────────────────────────────────────────────
|
| 48 |
-
|
| 49 |
-
def add_account(username, token, logs):
|
| 50 |
-
username = username.strip()
|
| 51 |
-
token = token.strip()
|
| 52 |
-
if not username or not token:
|
| 53 |
-
return gr.update(), accounts_table(), log_box("❌ أدخل اسم المستخدم والتوكن.", logs)
|
| 54 |
-
|
| 55 |
-
accounts = load_accounts()
|
| 56 |
-
if any(a["username"] == username for a in accounts):
|
| 57 |
-
return gr.update(), accounts_table(), log_box(f"⚠️ الحساب {username} موجود مسبقًا.", logs)
|
| 58 |
-
|
| 59 |
-
logs = log_box(f"🔍 جاري التحقق من {username}...", logs)
|
| 60 |
-
host = get_host(username, token)
|
| 61 |
-
if not host:
|
| 62 |
-
return gr.update(), accounts_table(), log_box(f"❌ فشل الاتصال بـ {username}. تحقق من البيانات.", logs)
|
| 63 |
-
|
| 64 |
-
accounts.append({
|
| 65 |
-
"username": username,
|
| 66 |
-
"token": token,
|
| 67 |
-
"host": host,
|
| 68 |
-
"last_renewed": None,
|
| 69 |
-
"next_renewal": None,
|
| 70 |
-
"last_results": []
|
| 71 |
-
})
|
| 72 |
-
save_accounts(accounts)
|
| 73 |
-
logs = log_box(f"✅ تمت إضافة {username} على {host}", logs)
|
| 74 |
-
return gr.update(value=""), accounts_table(), logs
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def remove_account(username, logs):
|
| 78 |
-
username = username.strip()
|
| 79 |
-
accounts = load_accounts()
|
| 80 |
-
before = len(accounts)
|
| 81 |
-
accounts = [a for a in accounts if a["username"] != username]
|
| 82 |
-
if len(accounts) == before:
|
| 83 |
-
return accounts_table(), log_box(f"⚠️ لم يُعثر على {username}.", logs)
|
| 84 |
-
save_accounts(accounts)
|
| 85 |
-
return accounts_table(), log_box(f"🗑️ تم حذف {username}.", logs)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def renew_now(logs):
|
| 89 |
-
logs = log_box("🚀 بدء التجديد الفوري لجميع الحسابات...", logs)
|
| 90 |
-
accounts = run_renewals()
|
| 91 |
-
if accounts is None:
|
| 92 |
-
return accounts_table(), log_box("📭 لا توجد حسابات.", logs)
|
| 93 |
-
for a in accounts:
|
| 94 |
-
for r in a.get("last_results", []):
|
| 95 |
-
logs = log_box(f" [{a['username']}] {r['domain']} → {r['status']}", logs)
|
| 96 |
-
logs = log_box("✅ انتهى التجديد.", logs)
|
| 97 |
-
return accounts_table(), logs
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def renew_single(username, logs):
|
| 101 |
-
username = username.strip()
|
| 102 |
-
accounts = load_accounts()
|
| 103 |
-
account = next((a for a in accounts if a["username"] == username), None)
|
| 104 |
-
if not account:
|
| 105 |
-
return accounts_table(), log_box(f"❌ الحساب {username} غير موجود.", logs)
|
| 106 |
-
|
| 107 |
-
# Force renewal by clearing next_renewal
|
| 108 |
-
account["next_renewal"] = None
|
| 109 |
-
save_accounts(accounts)
|
| 110 |
-
|
| 111 |
-
logs = log_box(f"🔄 تجديد {username}...", logs)
|
| 112 |
-
success, results = renew_account(account)
|
| 113 |
-
# Re-save with updated timestamps
|
| 114 |
-
accounts = [a if a["username"] != username else account for a in accounts]
|
| 115 |
-
save_accounts(accounts)
|
| 116 |
-
|
| 117 |
-
if success:
|
| 118 |
-
for r in results:
|
| 119 |
-
logs = log_box(f" {r['domain']} → {r['status']}", logs)
|
| 120 |
-
logs = log_box(f"✅ تم تجديد {username}.", logs)
|
| 121 |
-
else:
|
| 122 |
-
logs = log_box(f"❌ فشل تجديد {username}: {results}", logs)
|
| 123 |
-
return accounts_table(), logs
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# ─────────────────────────────────────────────
|
| 127 |
-
# Auto-scheduler
|
| 128 |
-
# ───────────────────────────────────────────��─
|
| 129 |
-
|
| 130 |
-
def scheduler_loop():
|
| 131 |
-
while SCHEDULER_RUNNING:
|
| 132 |
-
run_renewals()
|
| 133 |
-
time.sleep(3600) # check every hour, renewer handles the 10-day logic
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def toggle_scheduler(state, logs):
|
| 137 |
-
global SCHEDULER_RUNNING, SCHEDULER_THREAD
|
| 138 |
-
if not state: # currently OFF → turn ON
|
| 139 |
-
SCHEDULER_RUNNING = True
|
| 140 |
-
SCHEDULER_THREAD = threading.Thread(target=scheduler_loop, daemon=True)
|
| 141 |
-
SCHEDULER_THREAD.start()
|
| 142 |
-
logs = log_box("⏰ المجدول التلقائي شغّال (كل 10 أيام).", logs)
|
| 143 |
-
return True, "🟢 إيقاف المجدول", logs
|
| 144 |
-
else: # currently ON → turn OFF
|
| 145 |
-
SCHEDULER_RUNNING = False
|
| 146 |
-
logs = log_box("⏹️ تم إيقاف المجدول التلقائي.", logs)
|
| 147 |
-
return False, "▶️ تشغيل المجدول التلقائي", logs
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# ─────────────────────────────────────────────
|
| 151 |
-
# UI
|
| 152 |
-
# ─────────────────────────────────────────────
|
| 153 |
-
|
| 154 |
-
CSS = """
|
| 155 |
-
body { direction: rtl; }
|
| 156 |
-
.gr-button { font-size: 15px; }
|
| 157 |
-
#log_box textarea { font-family: monospace; font-size: 12px; direction: ltr; }
|
| 158 |
-
"""
|
| 159 |
-
|
| 160 |
-
with gr.Blocks(title="PythonAnywhere Manager", css=CSS, theme=gr.themes.Soft()) as demo:
|
| 161 |
-
|
| 162 |
-
scheduler_state = gr.State(False)
|
| 163 |
-
|
| 164 |
-
gr.Markdown("# 🐍 PythonAnywhere Auto-Renewer\nإدارة وتجديد حسابات PythonAnywhere تلقائيًا كل 10 أيام")
|
| 165 |
-
|
| 166 |
-
with gr.Tabs():
|
| 167 |
-
|
| 168 |
-
# ── Tab 1: Accounts ─────────────────────────────
|
| 169 |
-
with gr.TabItem("📋 الحسابات"):
|
| 170 |
-
tbl = gr.Markdown(accounts_table())
|
| 171 |
-
gr.Button("🔄 تحديث القائمة").click(lambda: accounts_table(), outputs=tbl)
|
| 172 |
-
|
| 173 |
-
# ── Tab 2: Add/Remove ───────────────────────────
|
| 174 |
-
with gr.TabItem("➕ إضافة / حذف"):
|
| 175 |
-
with gr.Row():
|
| 176 |
-
inp_user = gr.Textbox(label="اسم المستخدم", placeholder="nopoh55678")
|
| 177 |
-
inp_token = gr.Textbox(label="API Token", placeholder="496b360a...", type="password")
|
| 178 |
-
btn_add = gr.Button("➕ إضافة الحساب", variant="primary")
|
| 179 |
-
|
| 180 |
-
gr.Markdown("---")
|
| 181 |
-
inp_del = gr.Textbox(label="اسم المستخدم للحذف")
|
| 182 |
-
btn_del = gr.Button("🗑️ حذف الحساب", variant="stop")
|
| 183 |
-
|
| 184 |
-
# ── Tab 3: Control ──────────────────────────────
|
| 185 |
-
with gr.TabItem("🎮 التحكم"):
|
| 186 |
-
with gr.Row():
|
| 187 |
-
btn_renew_all = gr.Button("🚀 تجديد جميع الحسابات الآن", variant="primary")
|
| 188 |
-
btn_scheduler = gr.Button("▶️ تشغيل المجدول التلقائي", variant="secondary")
|
| 189 |
-
|
| 190 |
-
with gr.Row():
|
| 191 |
-
inp_single = gr.Textbox(label="تجديد حساب معين (اسم المستخدم)")
|
| 192 |
-
btn_single = gr.Button("🔄 تجديد")
|
| 193 |
-
|
| 194 |
-
# ── Shared log ──────────────────────────────────
|
| 195 |
-
log = gr.Textbox(
|
| 196 |
-
label="📜 السجل",
|
| 197 |
-
lines=12,
|
| 198 |
-
interactive=False,
|
| 199 |
-
elem_id="log_box",
|
| 200 |
-
value="مرحبًا! أضف حساباتك وشغّل المجدول."
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
# ── Wire up events ──────────────────────────────────
|
| 204 |
-
tbl2 = gr.Markdown(accounts_table(), visible=False) # shared table output
|
| 205 |
-
|
| 206 |
-
btn_add.click(
|
| 207 |
-
add_account,
|
| 208 |
-
inputs=[inp_user, inp_token, log],
|
| 209 |
-
outputs=[inp_user, tbl, log]
|
| 210 |
-
)
|
| 211 |
-
btn_del.click(
|
| 212 |
-
remove_account,
|
| 213 |
-
inputs=[inp_del, log],
|
| 214 |
-
outputs=[tbl, log]
|
| 215 |
-
)
|
| 216 |
-
btn_renew_all.click(
|
| 217 |
-
renew_now,
|
| 218 |
-
inputs=[log],
|
| 219 |
-
outputs=[tbl, log]
|
| 220 |
-
)
|
| 221 |
-
btn_single.click(
|
| 222 |
-
renew_single,
|
| 223 |
-
inputs=[inp_single, log],
|
| 224 |
-
outputs=[tbl, log]
|
| 225 |
-
)
|
| 226 |
-
btn_scheduler.click(
|
| 227 |
-
toggle_scheduler,
|
| 228 |
-
inputs=[scheduler_state, log],
|
| 229 |
-
outputs=[scheduler_state, btn_scheduler, log]
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
if __name__ == "__main__":
|
| 234 |
-
start_ping_server(port=8080)
|
| 235 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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