nopoh22 commited on
Commit
addce27
ยท
verified ยท
1 Parent(s): bcb31ef

Update renewer.py

Browse files
Files changed (1) hide show
  1. 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
- """Extend all webapps by 3 months (Run until 1 month from today)."""
 
 
 
 
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
- # Reload = "Run until 1 month from today" for free accounts
61
- reload_r = requests.post(
62
- f"https://{host}/api/v0/user/{username}/webapps/{domain}/reload/",
 
 
 
63
  headers={"Authorization": f"Token {token}"},
64
  timeout=10
65
  )
66
- status = "โœ… OK" if reload_r.status_code == 200 else f"โŒ {reload_r.status_code}"
67
- logger.info(f"[{username}] Reload {domain}: {status}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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