-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_pkgdb.py
More file actions
111 lines (86 loc) · 3.1 KB
/
Copy pathupdate_pkgdb.py
File metadata and controls
111 lines (86 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""
update_pkgdb.py — دانلود و ذخیره دیتابیس پکیجهای ترموکس به صورت لوکال
اجرا: python update_pkgdb.py
"""
import urllib.request
import json
import os
import sys
import time
SOURCE_URL = "https://termux-packages.ajam.dev/pkgs.json"
OUT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static", "pkgdb.json")
def download():
os.makedirs(os.path.dirname(OUT_FILE), exist_ok=True)
print(f"[*] downloading from:\n {SOURCE_URL}\n")
start = time.time()
req = urllib.request.Request(SOURCE_URL, headers={"User-Agent": "termux-panel/1.0"})
try:
with urllib.request.urlopen(req, timeout=30) as resp:
raw = resp.read()
except Exception as e:
print(f"[!] download failed: {e}")
sys.exit(1)
elapsed = round(time.time() - start, 2)
size_kb = round(len(raw) / 1024, 1)
print(f"[+] downloaded {size_kb} KB in {elapsed}s")
try:
data = json.loads(raw)
except json.JSONDecodeError as e:
print(f"[!] invalid JSON: {e}")
sys.exit(1)
# ساختار واقعی: آرایهای از {Package, Version, Description, Homepage}
if isinstance(data, list):
pkgs = data
elif isinstance(data, dict):
pkgs = list(data.values())
else:
print("[!] unexpected JSON structure")
sys.exit(1)
print(f"[*] raw entries: {len(pkgs)}")
# debug: نمایش اولین آیتم برای بررسی کلیدها
if pkgs:
first = pkgs[0]
print(f"[*] sample keys: {list(first.keys()) if isinstance(first, dict) else type(first)}")
cleaned = []
for p in pkgs:
if not isinstance(p, dict):
continue
# کلیدها ممکنه Capital باشن یا lowercase — هر دو رو چک میکنیم
name = (
p.get("Package") or p.get("package") or
p.get("Name") or p.get("name") or
p.get("pkg") or ""
).strip()
if not name:
continue
version = (
p.get("Version") or p.get("version") or ""
).strip()
description = (
p.get("Description") or p.get("description") or
p.get("desc") or ""
).strip()
homepage = (
p.get("Homepage") or p.get("homepage") or
p.get("url") or ""
).strip()
cleaned.append({
"name": name,
"version": version,
"description": description,
"homepage": homepage,
})
cleaned.sort(key=lambda x: x["name"].lower())
output = {
"updated_at": int(time.time()),
"count": len(cleaned),
"packages": cleaned,
}
with open(OUT_FILE, "w", encoding="utf-8") as f:
json.dump(output, f, ensure_ascii=False, separators=(",", ":"))
final_kb = round(os.path.getsize(OUT_FILE) / 1024, 1)
print(f"[+] saved {len(cleaned)} packages → {OUT_FILE} ({final_kb} KB)")
print(f"\n[✓] done. run 'python app.py' to start the panel.")
if __name__ == "__main__":
download()