-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_tvbox.py
More file actions
executable file
·295 lines (238 loc) · 9.64 KB
/
Copy pathbuild_tvbox.py
File metadata and controls
executable file
·295 lines (238 loc) · 9.64 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""Build the merged TVBox subscription file."""
from __future__ import annotations
import copy
import hashlib
import json
import sys
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
SOURCES_FILE = ROOT / "sources.json"
OUTPUT_FILE = ROOT / "tvbox.json"
MANIFEST_FILE = ROOT / "manifest.json"
USER_AGENT = "SakuraByteCore-subHub-tvbox-builder/1.0 (+https://github.com/SakuraByteCore/subHub)"
URL_FIELDS = {
"api",
"ext",
"jar",
"logo",
"playUrl",
"script",
"spider",
"url",
"wallpaper",
}
ARRAY_KEYS = ("sites", "lives", "parses", "rules", "doh")
SET_KEYS = ("hosts", "flags", "ads")
SITE_NAME_OVERRIDES = {
"drpy_js_豆瓣": "subHub | 搜索入口",
}
def strip_full_line_json_comments(text: str) -> str:
"""Strip JSONC-style full-line comments without touching URLs inside strings."""
lines = []
for line in text.splitlines():
if line.lstrip().startswith("//"):
continue
lines.append(line)
return "\n".join(lines).strip()
def fetch_json(url: str) -> dict[str, Any]:
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(request, timeout=30) as response:
raw = response.read().decode("utf-8-sig")
data = json.loads(strip_full_line_json_comments(raw))
if not isinstance(data, dict):
raise ValueError(f"source root must be an object: {url}")
return data
def is_relative_path(value: str) -> bool:
if not value or value.startswith(("#", "csp_", "CSP_")):
return False
parsed = urllib.parse.urlparse(value)
if parsed.scheme or value.startswith("//"):
return False
return value.startswith(("./", "../", "/"))
def absolutize_string(value: str, base_url: str) -> str:
"""Convert relative URL/path strings to absolute URLs.
TVBox values may contain a md5 suffix, e.g. ./jar/spider.jar;md5;xxx.
Only the first segment is a path in that format.
"""
if ";" in value:
head, *tail = value.split(";")
if is_relative_path(head):
head = urllib.parse.urljoin(base_url, head)
return ";".join([head, *tail])
if is_relative_path(value):
return urllib.parse.urljoin(base_url, value)
return value
def normalize_urls(value: Any, base_url: str, field: str | None = None) -> Any:
if isinstance(value, dict):
return {key: normalize_urls(item, base_url, key) for key, item in value.items()}
if isinstance(value, list):
return [normalize_urls(item, base_url, field) for item in value]
if isinstance(value, str) and (field in URL_FIELDS or is_relative_path(value.split(";", 1)[0])):
return absolutize_string(value, base_url)
return value
def stable_json(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
def digest(value: Any) -> str:
return hashlib.sha256(stable_json(value).encode("utf-8")).hexdigest()[:12]
def tagged_name(item: dict[str, Any], source: dict[str, Any]) -> dict[str, Any]:
item = copy.deepcopy(item)
name = item.get("name")
if isinstance(name, str) and not name.startswith(f"[{source['id']}]"):
item["name"] = f"[{source['id']}] {name}"
return item
def unique_key(base: str, used: set[str], source_id: str) -> str:
candidate = f"{source_id}_{base}"
if candidate not in used:
return candidate
index = 2
while f"{candidate}_{index}" in used:
index += 1
return f"{candidate}_{index}"
def add_site(target: list[dict[str, Any]], used_keys: dict[str, str], item: dict[str, Any], source: dict[str, Any]) -> None:
item = copy.deepcopy(item)
key = item.get("key")
if not isinstance(key, str) or not key:
key = f"{source['id']}_{digest(item)}"
item["key"] = key
body = stable_json(item)
existing = used_keys.get(key)
if existing is None:
used_keys[key] = body
target.append(item)
return
if existing == body:
return
item = tagged_name(item, source)
item["key"] = unique_key(key, set(used_keys), source["id"])
used_keys[item["key"]] = stable_json(item)
target.append(item)
def add_named(target: list[dict[str, Any]], used_names: dict[str, str], item: dict[str, Any], source: dict[str, Any]) -> None:
item = copy.deepcopy(item)
name = item.get("name")
if not isinstance(name, str) or not name:
name = f"{source['id']}_{digest(item)}"
item["name"] = name
body = stable_json(item)
existing = used_names.get(name)
if existing is None:
used_names[name] = body
target.append(item)
return
if existing == body:
return
item = tagged_name(item, source)
used_names[item["name"]] = stable_json(item)
target.append(item)
def add_live(target: list[dict[str, Any]], used: set[str], item: dict[str, Any]) -> None:
item = copy.deepcopy(item)
marker = f"{item.get('name', '')}\u0000{item.get('url', '')}"
if marker in used:
return
used.add(marker)
target.append(item)
def attach_source_jar_to_sites(data: dict[str, Any]) -> None:
jar = data.get("spider")
if not isinstance(jar, str):
return
for site in data.get("sites", []):
if not isinstance(site, dict):
continue
if site.get("type") == 3 and "jar" not in site:
site["jar"] = jar
def apply_site_overrides(site: dict[str, Any]) -> dict[str, Any]:
key = site.get("key")
if isinstance(key, str) and key in SITE_NAME_OVERRIDES:
site = copy.deepcopy(site)
site["name"] = SITE_NAME_OVERRIDES[key]
return site
def load_sources() -> list[dict[str, Any]]:
sources = json.loads(SOURCES_FILE.read_text(encoding="utf-8"))
enabled = [source for source in sources if source.get("enabled", True)]
return sorted(enabled, key=lambda source: int(source.get("priority", 100)))
def build() -> tuple[dict[str, Any], dict[str, Any]]:
sources = load_sources()
merged: dict[str, Any] = {}
manifest: dict[str, Any] = {
"built_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"sources": [],
"counts": {},
}
site_keys: dict[str, str] = {}
named_keys: dict[str, dict[str, str]] = {key: {} for key in ("parses", "rules", "doh")}
live_keys: set[str] = set()
set_values: dict[str, set[str]] = {key: set() for key in SET_KEYS}
merged["sites"] = []
merged["lives"] = []
for source in sources:
source_status: dict[str, Any] = {
"id": source["id"],
"name": source.get("name", source["id"]),
"url": source["url"],
"priority": source.get("priority"),
}
try:
data = fetch_json(source["url"])
data = normalize_urls(data, source["url"])
attach_source_jar_to_sites(data)
source_status["ok"] = True
source_status["counts"] = {
key: len(data.get(key, [])) for key in (*ARRAY_KEYS, *SET_KEYS) if isinstance(data.get(key), list)
}
except (OSError, urllib.error.URLError, json.JSONDecodeError, ValueError) as exc:
source_status["ok"] = False
source_status["error"] = str(exc)
manifest["sources"].append(source_status)
continue
if "spider" not in merged and isinstance(data.get("spider"), str):
merged["spider"] = data["spider"]
if "wallpaper" not in merged and isinstance(data.get("wallpaper"), str):
merged["wallpaper"] = data["wallpaper"]
if "logo" not in merged and isinstance(data.get("logo"), str):
merged["logo"] = data["logo"]
for item in data.get("sites", []):
if isinstance(item, dict):
add_site(merged["sites"], site_keys, apply_site_overrides(item), source)
for item in data.get("lives", []):
if isinstance(item, dict):
add_live(merged["lives"], live_keys, item)
for key in ("parses", "rules", "doh"):
if key not in merged:
merged[key] = []
for item in data.get(key, []):
if isinstance(item, dict):
add_named(merged[key], named_keys[key], item, source)
for key in SET_KEYS:
if key not in merged:
merged[key] = []
for item in data.get(key, []):
if isinstance(item, str) and item not in set_values[key]:
set_values[key].add(item)
merged[key].append(item)
manifest["sources"].append(source_status)
manifest["generated"] = {"site_name_overrides": SITE_NAME_OVERRIDES}
for key in ["sites", "lives", "parses", "hosts", "flags", "doh", "rules", "ads"]:
if key in merged:
manifest["counts"][key] = len(merged[key])
return merged, manifest
def write_json(path: Path, data: dict[str, Any]) -> None:
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def main() -> int:
merged, manifest = build()
write_json(OUTPUT_FILE, merged)
write_json(MANIFEST_FILE, manifest)
print(f"wrote {OUTPUT_FILE.relative_to(ROOT)}")
print(f"wrote {MANIFEST_FILE.relative_to(ROOT)}")
print(json.dumps(manifest["counts"], ensure_ascii=False, sort_keys=True))
failed = [source for source in manifest["sources"] if not source.get("ok")]
if failed:
print(json.dumps({"failed_sources": failed}, ensure_ascii=False, indent=2), file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())