feat: 接入黄豆短剧独立清单与下载

This commit is contained in:
Nixevol
2026-10-21 22:54:56 +08:00
parent e31e80384c
commit 5b4a948e5f
25 changed files with 2093 additions and 132 deletions
+28 -8
View File
@@ -88,15 +88,26 @@ class HttpClient:
self._client = httpx.Client(**self._client_kwargs())
return self._client
def get_bytes(self, url: str) -> tuple[bytes, str]:
response = self._client_locked().get(url, headers={"User-Agent": "RaptDrama"})
def send(self, method: str, url: str, *, headers: dict | None = None, content: bytes | None = None, timeout=None) -> httpx.Response:
options: dict = {"headers": headers or {}}
if content is not None:
options["content"] = content
if timeout is not None:
options["timeout"] = timeout
return self._client_locked().request(method, url, **options)
def get_bytes(self, url: str, headers: dict | None = None) -> tuple[bytes, str]:
hdrs = {"User-Agent": "RaptDrama"}
if headers:
hdrs.update(headers)
response = self._client_locked().get(url, headers=hdrs)
response.raise_for_status()
media = response.headers.get("content-type", "image/jpeg").split(";", 1)[0].strip() or "image/jpeg"
if not media.startswith("image/"):
raise RuntimeError("不是图片")
return response.content, media
def download_file(self, url: str, dest, on_chunk=None, stop=None) -> int:
def download_file(self, url: str, dest, on_chunk=None, stop=None, headers: dict | None = None) -> int:
from pathlib import Path
target = Path(dest)
@@ -107,7 +118,10 @@ class HttpClient:
if stop and stop():
raise RuntimeError("已取消")
try:
with self._client_locked().stream("GET", url, headers={"User-Agent": "RaptDrama"}) as response:
hdrs = {"User-Agent": "RaptDrama"}
if headers:
hdrs.update(headers)
with self._client_locked().stream("GET", url, headers=hdrs) as response:
response.raise_for_status()
size = 0
with temporary.open("wb") as handle:
@@ -131,11 +145,14 @@ class HttpClient:
time.sleep(0.4)
raise RuntimeError(f"下载失败: {last_error}")
def read_text(self, url: str) -> str:
def read_text(self, url: str, headers: dict | None = None) -> str:
last_error = None
hdrs = {"User-Agent": "RaptDrama"}
if headers:
hdrs.update(headers)
for _attempt in range(3):
try:
response = self._client_locked().get(url, headers={"User-Agent": "RaptDrama"})
response = self._client_locked().get(url, headers=hdrs)
response.raise_for_status()
return response.text
except (httpx.HTTPError, TimeoutError, OSError) as exc:
@@ -143,9 +160,12 @@ class HttpClient:
time.sleep(0.4)
raise RuntimeError(f"下载失败: {last_error}")
def open_stream(self, url: str) -> httpx.Response:
def open_stream(self, url: str, headers: dict | None = None) -> httpx.Response:
client = self._client_locked()
request = client.build_request("GET", url, headers={"User-Agent": "RaptDrama"})
hdrs = {"User-Agent": "RaptDrama"}
if headers:
hdrs.update(headers)
request = client.build_request("GET", url, headers=hdrs)
response = client.send(request, stream=True)
try:
response.raise_for_status()