fix: avoid API-only dependency and pin default categories
This commit is contained in:
+10
-1
@@ -9,7 +9,16 @@
|
||||
"source": {
|
||||
"root": "rule/Surge",
|
||||
"filename_pattern": "{name}.list",
|
||||
"include_categories": [],
|
||||
"include_categories": [
|
||||
"Apple",
|
||||
"China",
|
||||
"ChinaIPs",
|
||||
"Claude",
|
||||
"Gemini",
|
||||
"Lan",
|
||||
"OpenAI",
|
||||
"Proxy"
|
||||
],
|
||||
"exclude_categories": []
|
||||
},
|
||||
"output": {
|
||||
|
||||
@@ -12,6 +12,7 @@ from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.parse import quote
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
try:
|
||||
import tomllib
|
||||
@@ -67,6 +68,14 @@ class GiteaClient:
|
||||
with urlopen(req) as resp:
|
||||
return json.loads(resp.read().decode("utf-8"))
|
||||
|
||||
def _request_text_url(self, url: str) -> str:
|
||||
headers = {"Accept": "text/plain"}
|
||||
if self.token:
|
||||
headers["Authorization"] = f"token {self.token}"
|
||||
req = Request(url, headers=headers)
|
||||
with urlopen(req) as resp:
|
||||
return resp.read().decode("utf-8", errors="replace")
|
||||
|
||||
def list_dir(self, owner: str, repo: str, path: str, ref: str) -> list[dict[str, Any]]:
|
||||
encoded_path = quote(path.strip("/"), safe="/")
|
||||
endpoint = f"/api/v1/repos/{quote(owner)}/{quote(repo)}/contents/{encoded_path}"
|
||||
@@ -88,7 +97,18 @@ class GiteaClient:
|
||||
def read_file(self, owner: str, repo: str, path: str, ref: str) -> str:
|
||||
encoded_path = quote(path.strip("/"), safe="/")
|
||||
endpoint = f"/api/v1/repos/{quote(owner)}/{quote(repo)}/contents/{encoded_path}"
|
||||
data = self._request_json(endpoint, {"ref": ref})
|
||||
try:
|
||||
data = self._request_json(endpoint, {"ref": ref})
|
||||
except HTTPError as exc:
|
||||
if exc.code != 404:
|
||||
raise
|
||||
# Fallback for environments where Gitea API route is not exposed.
|
||||
raw_url = (
|
||||
f"{self.base_url}/{quote(owner)}/{quote(repo)}/raw/branch/"
|
||||
f"{quote(ref)}/{encoded_path}"
|
||||
)
|
||||
return self._request_text_url(raw_url)
|
||||
|
||||
if not isinstance(data, dict):
|
||||
raise RuntimeError(f"Path is not a file: {path}")
|
||||
|
||||
@@ -240,6 +260,9 @@ def should_include_category(name: str, cfg: Config, cli_names: set[str]) -> bool
|
||||
|
||||
|
||||
def find_categories(client: GiteaClient, cfg: Config, cli_names: set[str]) -> list[str]:
|
||||
if cfg.include_categories:
|
||||
return sorted([n for n in cfg.include_categories if should_include_category(n, cfg, cli_names)])
|
||||
|
||||
entries = client.list_dir(cfg.owner, cfg.repo, cfg.source_root, cfg.ref)
|
||||
categories: list[str] = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user