nopoh22 commited on
Commit
0dd1b0f
·
verified ·
1 Parent(s): 49a8bc7

Delete renewer.py

Browse files
Files changed (1) hide show
  1. renewer.py +0 -120
renewer.py DELETED
@@ -1,120 +0,0 @@
1
- import requests
2
- import json
3
- import os
4
- import logging
5
- from datetime import datetime, timedelta
6
-
7
- logging.basicConfig(
8
- level=logging.INFO,
9
- format='%(asctime)s [%(levelname)s] %(message)s'
10
- )
11
- logger = logging.getLogger(__name__)
12
-
13
- ACCOUNTS_FILE = "accounts.json"
14
-
15
-
16
- def load_accounts():
17
- if os.path.exists(ACCOUNTS_FILE):
18
- with open(ACCOUNTS_FILE, "r") as f:
19
- return json.load(f)
20
- return []
21
-
22
-
23
- def save_accounts(accounts):
24
- with open(ACCOUNTS_FILE, "w") as f:
25
- json.dump(accounts, f, indent=2)
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(
33
- f"https://{host}/api/v0/user/{username}/cpu/",
34
- headers={"Authorization": f"Token {token}"},
35
- timeout=10
36
- )
37
- if r.status_code == 200:
38
- return host
39
- except Exception:
40
- continue
41
- return None
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
-
74
- def renew_account(account):
75
- username = account["username"]
76
- token = account["token"]
77
- host = account.get("host") or get_host(username, token)
78
-
79
- if not host:
80
- logger.error(f"[{username}] Cannot connect - invalid credentials?")
81
- return False, "Cannot connect"
82
-
83
- account["host"] = host
84
- logger.info(f"[{username}] Connected to {host}")
85
-
86
- results = extend_webapp(username, token, host)
87
- account["last_renewed"] = datetime.utcnow().isoformat()
88
- account["next_renewal"] = (datetime.utcnow() + timedelta(days=10)).isoformat()
89
- account["last_results"] = results
90
- return True, results
91
-
92
-
93
- def run_renewals():
94
- accounts = load_accounts()
95
- if not accounts:
96
- logger.info("No accounts configured yet.")
97
- return
98
-
99
- now = datetime.utcnow()
100
- renewed_count = 0
101
-
102
- for account in accounts:
103
- username = account["username"]
104
- next_renewal_str = account.get("next_renewal")
105
-
106
- if next_renewal_str:
107
- next_renewal = datetime.fromisoformat(next_renewal_str)
108
- if now < next_renewal:
109
- remaining = (next_renewal - now).days
110
- logger.info(f"[{username}] Skipping - next renewal in {remaining} days")
111
- continue
112
-
113
- logger.info(f"[{username}] Starting renewal...")
114
- success, results = renew_account(account)
115
- if success:
116
- renewed_count += 1
117
-
118
- save_accounts(accounts)
119
- logger.info(f"Done. Renewed {renewed_count}/{len(accounts)} accounts.")
120
- return accounts