Update renewer.py
Browse files- renewer.py +39 -9
renewer.py
CHANGED
|
@@ -26,7 +26,6 @@ def save_accounts(accounts):
|
|
| 26 |
|
| 27 |
|
| 28 |
def get_host(username, token):
|
| 29 |
-
"""Detect if account is on US or EU server."""
|
| 30 |
for host in ["www.pythonanywhere.com", "eu.pythonanywhere.com"]:
|
| 31 |
try:
|
| 32 |
r = requests.get(
|
|
@@ -42,32 +41,63 @@ def get_host(username, token):
|
|
| 42 |
|
| 43 |
|
| 44 |
def extend_webapp(username, token, host):
|
| 45 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
results = []
|
| 47 |
try:
|
|
|
|
| 48 |
r = requests.get(
|
| 49 |
f"https://{host}/api/v0/user/{username}/webapps/",
|
| 50 |
headers={"Authorization": f"Token {token}"},
|
| 51 |
timeout=10
|
| 52 |
)
|
| 53 |
if r.status_code != 200:
|
| 54 |
-
logger.warning(f"[{username}] Failed to list webapps: {r.status_code}")
|
| 55 |
return results
|
| 56 |
|
| 57 |
webapps = r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
for app in webapps:
|
| 59 |
domain = app.get("domain_name")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
headers={"Authorization": f"Token {token}"},
|
| 64 |
timeout=10
|
| 65 |
)
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
results.append({"domain": domain, "status": status})
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
logger.error(f"[{username}] Error: {e}")
|
|
|
|
| 71 |
return results
|
| 72 |
|
| 73 |
|
|
@@ -117,4 +147,4 @@ def run_renewals():
|
|
| 117 |
|
| 118 |
save_accounts(accounts)
|
| 119 |
logger.info(f"Done. Renewed {renewed_count}/{len(accounts)} accounts.")
|
| 120 |
-
return accounts
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def get_host(username, token):
|
|
|
|
| 29 |
for host in ["www.pythonanywhere.com", "eu.pythonanywhere.com"]:
|
| 30 |
try:
|
| 31 |
r = requests.get(
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def extend_webapp(username, token, host):
|
| 44 |
+
"""
|
| 45 |
+
ุงููุธููุฉ ุงูุญููููุฉ ูุฒุฑ 'Run until 1 month from today':
|
| 46 |
+
1. POST /enable/ โ ูููุนูู ุงูู webapp ููู
ุฏุฏู ุดูุฑ (= ุงูุฒุฑ ูู ุงููุงุฌูุฉ)
|
| 47 |
+
2. POST /reload/ โ ููุนูุฏ ุชุญู
ูู ุงูููุฏ ููุท (ูุง ุนูุงูุฉ ูู ุจุงูุชู
ุฏูุฏ)
|
| 48 |
+
"""
|
| 49 |
results = []
|
| 50 |
try:
|
| 51 |
+
# ุงูุฎุทูุฉ 1: ุฌูุจ ูุงุฆู
ุฉ ุงูู webapps
|
| 52 |
r = requests.get(
|
| 53 |
f"https://{host}/api/v0/user/{username}/webapps/",
|
| 54 |
headers={"Authorization": f"Token {token}"},
|
| 55 |
timeout=10
|
| 56 |
)
|
| 57 |
if r.status_code != 200:
|
| 58 |
+
logger.warning(f"[{username}] Failed to list webapps: {r.status_code} - {r.text}")
|
| 59 |
return results
|
| 60 |
|
| 61 |
webapps = r.json()
|
| 62 |
+
if not webapps:
|
| 63 |
+
logger.warning(f"[{username}] No webapps found.")
|
| 64 |
+
return results
|
| 65 |
+
|
| 66 |
for app in webapps:
|
| 67 |
domain = app.get("domain_name")
|
| 68 |
+
logger.info(f"[{username}] Processing webapp: {domain}")
|
| 69 |
+
|
| 70 |
+
# ุงูุฎุทูุฉ 2: POST /enable/ = "Run until 1 month from today"
|
| 71 |
+
# ูุฐุง ูู ุงูู endpoint ุงูุตุญูุญ ุงูุฐู ูุถุบุท ุงูุฒุฑ
|
| 72 |
+
enable_r = requests.post(
|
| 73 |
+
f"https://{host}/api/v0/user/{username}/webapps/{domain}/enable/",
|
| 74 |
headers={"Authorization": f"Token {token}"},
|
| 75 |
timeout=10
|
| 76 |
)
|
| 77 |
+
|
| 78 |
+
if enable_r.status_code == 200:
|
| 79 |
+
status = "โ
ู
ู
ุฏูุฏ (enabled)"
|
| 80 |
+
logger.info(f"[{username}] {domain}: ุชู
ุงูุชู
ุฏูุฏ ุจูุฌุงุญ โ
")
|
| 81 |
+
else:
|
| 82 |
+
# ุฅุฐุง ูุงู ู
ูุนููุงู ู
ุณุจูุงู status=409 ุฃู ุบูุฑูุ ูุฌุฑุจ reload
|
| 83 |
+
logger.warning(f"[{username}] enable/ returned {enable_r.status_code}: {enable_r.text}")
|
| 84 |
+
|
| 85 |
+
# ุงูุฎุทูุฉ 3: POST /reload/ ูู fallback ููู webapps ุงูู
ูุนููุฉ ุฃุตูุงู
|
| 86 |
+
reload_r = requests.post(
|
| 87 |
+
f"https://{host}/api/v0/user/{username}/webapps/{domain}/reload/",
|
| 88 |
+
headers={"Authorization": f"Token {token}"},
|
| 89 |
+
timeout=10
|
| 90 |
+
)
|
| 91 |
+
if reload_r.status_code == 200:
|
| 92 |
+
status = "โ
Reloaded (ูุงู ู
ูุนููุงู)"
|
| 93 |
+
else:
|
| 94 |
+
status = f"โ ูุดู ({enable_r.status_code})"
|
| 95 |
+
|
| 96 |
results.append({"domain": domain, "status": status})
|
| 97 |
+
|
| 98 |
except Exception as e:
|
| 99 |
logger.error(f"[{username}] Error: {e}")
|
| 100 |
+
|
| 101 |
return results
|
| 102 |
|
| 103 |
|
|
|
|
| 147 |
|
| 148 |
save_accounts(accounts)
|
| 149 |
logger.info(f"Done. Renewed {renewed_count}/{len(accounts)} accounts.")
|
| 150 |
+
return accounts
|