ranobelib-python-sdk 0.10.0__tar.gz → 0.11.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/PKG-INFO +1 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/pyproject.toml +1 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/pyproject.toml.orig +1 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/__init__.py +2 -0
- ranobelib_python_sdk-0.11.0/src/ranobelib/_http.py +45 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/catalog.py +5 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/client.py +25 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exceptions.py +28 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/__init__.py +6 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/epub.py +11 -2
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/fb2.py +4 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/pdf.py +10 -2
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/txt.py +4 -1
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/sdk.py +17 -4
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/README.md +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/cache.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/console.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/_illustrations.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/_shared.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/models.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/numbering.py +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/py.typed +0 -0
- {ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/sizing.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ranobelib-python-sdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: Async Python SDK for ranobelib.me: title metadata, chapter/volume downloads, and export to epub/fb2/txt/pdf
|
|
5
5
|
Keywords: ranobelib,ranobe,novel,webnovel,epub,fb2,sdk
|
|
6
6
|
Author: LLIJIYAPNIK
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from ranobelib.catalog import Catalog
|
|
4
4
|
from ranobelib.exceptions import (
|
|
5
|
+
AccessBlockedError,
|
|
5
6
|
AmbiguousChapter,
|
|
6
7
|
AuthRequiredError,
|
|
7
8
|
ChapterNotFoundError,
|
|
@@ -27,6 +28,7 @@ from ranobelib.sdk import RanobeLib
|
|
|
27
28
|
from ranobelib.sizing import chapter_size, volume_size
|
|
28
29
|
|
|
29
30
|
__all__ = [
|
|
31
|
+
"AccessBlockedError",
|
|
30
32
|
"AmbiguousChapter",
|
|
31
33
|
"AuthRequiredError",
|
|
32
34
|
"Catalog",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""HTTP settings shared by every client the SDK opens (not public API).
|
|
2
|
+
|
|
3
|
+
Lives in its own module rather than in ``client.py`` because it isn't specific to the
|
|
4
|
+
api.cdnlibs.org JSON API: the exporters' image-downloading clients (cover.cdnlibs.org,
|
|
5
|
+
ranobelib.me uploads) need the same headers, and importing them from here keeps the
|
|
6
|
+
exporters from depending on ``ApiClient``'s module just for a constant.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Mapping
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
SITE_ORIGIN = "https://ranobelib.me"
|
|
16
|
+
"""Origin of the ranobelib.me website, as its own pages send it."""
|
|
17
|
+
|
|
18
|
+
BROWSER_HEADERS: dict[str, str] = {
|
|
19
|
+
"Origin": SITE_ORIGIN,
|
|
20
|
+
"Referer": f"{SITE_ORIGIN}/",
|
|
21
|
+
"User-Agent": (
|
|
22
|
+
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 "
|
|
23
|
+
"(KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1"
|
|
24
|
+
),
|
|
25
|
+
}
|
|
26
|
+
"""Headers a browser on ranobelib.me sends, so requests look like they come from the site.
|
|
27
|
+
|
|
28
|
+
The DDoS-Guard edge in front of api.cdnlibs.org and cover.cdnlibs.org rejects requests
|
|
29
|
+
without a non-empty ``Referer`` with an HTML 403 page (see docs/api-notes.md, section
|
|
30
|
+
"WAF 403"). Any ``Referer`` value was enough when checked; the site's own one is sent,
|
|
31
|
+
together with ``Origin`` and a real browser ``User-Agent``, so the request as a whole matches
|
|
32
|
+
what the site itself sends rather than advertising ``python-httpx``.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def browser_headers(overrides: Mapping[str, str] | None = None) -> httpx.Headers:
|
|
37
|
+
"""``BROWSER_HEADERS`` with ``overrides`` merged over them.
|
|
38
|
+
|
|
39
|
+
A key in ``overrides`` replaces the default one, matched case-insensitively (``httpx``
|
|
40
|
+
headers semantics) — the same merge ``ApiClient(headers=...)`` does for API requests.
|
|
41
|
+
"""
|
|
42
|
+
headers = httpx.Headers(BROWSER_HEADERS)
|
|
43
|
+
if overrides:
|
|
44
|
+
headers.update(overrides)
|
|
45
|
+
return headers
|
|
@@ -7,6 +7,7 @@ its own ``ApiClient``/``DiskCache`` rather than a method on ``RanobeLib``.
|
|
|
7
7
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
|
+
from collections.abc import Mapping
|
|
10
11
|
from pathlib import Path
|
|
11
12
|
from types import TracebackType
|
|
12
13
|
from typing import Any, Self
|
|
@@ -40,6 +41,7 @@ class Catalog:
|
|
|
40
41
|
*,
|
|
41
42
|
cache_dir: str | Path | None = None,
|
|
42
43
|
cache_ttl: float | None = None,
|
|
44
|
+
headers: Mapping[str, str] | None = None,
|
|
43
45
|
) -> None:
|
|
44
46
|
"""Initialize the catalog client.
|
|
45
47
|
|
|
@@ -51,8 +53,10 @@ class Catalog:
|
|
|
51
53
|
cache_ttl: Seconds after which a cached page is treated as stale and re-fetched.
|
|
52
54
|
``None`` (the default) means cached pages never expire on their own — see
|
|
53
55
|
``refresh=True`` on ``list_titles()`` to force one anyway.
|
|
56
|
+
headers: Extra HTTP headers merged over the SDK's defaults, same as
|
|
57
|
+
``RanobeLib(headers=...)`` — see there.
|
|
54
58
|
"""
|
|
55
|
-
self._client = ApiClient()
|
|
59
|
+
self._client = ApiClient(headers=headers)
|
|
56
60
|
self._cache = DiskCache(
|
|
57
61
|
cache_dir if cache_dir is not None else DEFAULT_CACHE_DIR, ttl=cache_ttl
|
|
58
62
|
)
|
|
@@ -4,13 +4,15 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import time
|
|
7
|
-
from collections.abc import Awaitable, Callable
|
|
7
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
8
8
|
from types import TracebackType
|
|
9
9
|
from typing import Any, Self
|
|
10
10
|
|
|
11
11
|
import httpx
|
|
12
12
|
|
|
13
|
+
from ranobelib._http import BROWSER_HEADERS
|
|
13
14
|
from ranobelib.exceptions import (
|
|
15
|
+
AccessBlockedError,
|
|
14
16
|
AuthRequiredError,
|
|
15
17
|
ChapterNotFoundError,
|
|
16
18
|
RanobeLibError,
|
|
@@ -36,6 +38,11 @@ class ApiClient:
|
|
|
36
38
|
Bounds concurrency with a semaphore, paces request starts with a small fixed delay, and
|
|
37
39
|
retries 429/5xx responses with exponential backoff — see docs/api-notes.md for why a
|
|
38
40
|
manual implementation was chosen over a dependency like ``tenacity`` for this.
|
|
41
|
+
|
|
42
|
+
Every request carries browser-like ``Origin``/``Referer``/``User-Agent`` headers on top
|
|
43
|
+
of the API-specific ``Site-Id``/``Accept``: without a non-empty ``Referer``, the
|
|
44
|
+
DDoS-Guard edge in front of the API answers every request with an HTML 403 page (see
|
|
45
|
+
docs/api-notes.md, section "WAF 403").
|
|
39
46
|
"""
|
|
40
47
|
|
|
41
48
|
def __init__(
|
|
@@ -44,6 +51,7 @@ class ApiClient:
|
|
|
44
51
|
base_url: str = API_BASE_URL,
|
|
45
52
|
timeout: float = 15.0,
|
|
46
53
|
transport: httpx.AsyncBaseTransport | None = None,
|
|
54
|
+
headers: Mapping[str, str] | None = None,
|
|
47
55
|
max_concurrency: int = 5,
|
|
48
56
|
request_delay: float | None = None,
|
|
49
57
|
max_retries: int = 3,
|
|
@@ -57,6 +65,13 @@ class ApiClient:
|
|
|
57
65
|
base_url: API base URL.
|
|
58
66
|
timeout: Per-request timeout, in seconds.
|
|
59
67
|
transport: Custom ``httpx`` transport, e.g. a mock transport for tests.
|
|
68
|
+
headers: Extra headers merged over the defaults (browser-like ``Origin``/
|
|
69
|
+
``Referer``/``User-Agent`` plus the API's ``Site-Id``/``Accept``); a key
|
|
70
|
+
given here replaces the default one, matched case-insensitively. Meant for
|
|
71
|
+
swapping in a different ``User-Agent`` if the site's edge protection changes
|
|
72
|
+
its rules, without waiting for an SDK release. Overriding ``Site-Id`` or
|
|
73
|
+
``Accept`` is possible but breaks the API's contract (see
|
|
74
|
+
docs/api-notes.md) — only do it knowingly.
|
|
60
75
|
max_concurrency: Maximum number of requests in flight at once.
|
|
61
76
|
request_delay: Minimum time, in seconds, between the start of one request and
|
|
62
77
|
the next, even under ``max_concurrency``. ``None`` (the default) uses
|
|
@@ -74,10 +89,13 @@ class ApiClient:
|
|
|
74
89
|
timeout=timeout,
|
|
75
90
|
transport=transport,
|
|
76
91
|
headers={
|
|
92
|
+
**BROWSER_HEADERS,
|
|
77
93
|
"Site-Id": RANOBELIB_SITE_ID,
|
|
78
94
|
"Accept": "application/json",
|
|
79
95
|
},
|
|
80
96
|
)
|
|
97
|
+
if headers is not None:
|
|
98
|
+
self._http.headers.update(headers)
|
|
81
99
|
self._semaphore = asyncio.Semaphore(max_concurrency)
|
|
82
100
|
self._request_delay = DEFAULT_REQUEST_DELAY if request_delay is None else request_delay
|
|
83
101
|
self._max_retries = max_retries
|
|
@@ -302,6 +320,12 @@ class ApiClient:
|
|
|
302
320
|
if response.status_code == 404:
|
|
303
321
|
raise not_found
|
|
304
322
|
if response.status_code == 403:
|
|
323
|
+
# The API's own 403 is JSON; the DDoS-Guard edge in front of it answers with an
|
|
324
|
+
# HTML page instead. Told apart by Content-Type, not by the page's wording, which
|
|
325
|
+
# can change (see docs/api-notes.md, section "WAF 403").
|
|
326
|
+
media_type = response.headers.get("Content-Type", "").split(";")[0].strip().lower()
|
|
327
|
+
if media_type != "application/json":
|
|
328
|
+
raise AccessBlockedError(str(response.request.url))
|
|
305
329
|
raise AuthRequiredError(str(response.request.url))
|
|
306
330
|
if response.status_code == 429:
|
|
307
331
|
retry_after = response.headers.get("Retry-After")
|
|
@@ -153,6 +153,34 @@ class AuthRequiredError(RanobeLibError):
|
|
|
153
153
|
super().__init__(f"Authorization required to access: {url}")
|
|
154
154
|
|
|
155
155
|
|
|
156
|
+
class AccessBlockedError(RanobeLibError):
|
|
157
|
+
"""Raised when the site's edge protection rejects a request before it reaches the API.
|
|
158
|
+
|
|
159
|
+
Not an authorization problem, unlike ``AuthRequiredError``: the DDoS-Guard edge in front
|
|
160
|
+
of api.cdnlibs.org answers with an HTML 403 page (the API's own 403 is JSON) when it
|
|
161
|
+
doesn't like the request — so far, a missing or empty ``Referer`` (see
|
|
162
|
+
docs/api-notes.md, section "WAF 403"). The SDK already sends browser-like headers, so
|
|
163
|
+
seeing this means either the edge changed its rules — check for an SDK update, or send
|
|
164
|
+
what it now expects via ``headers=`` on ``RanobeLib``/``Catalog`` (or ``ApiClient``, if
|
|
165
|
+
using it directly) — or the edge is blocking the network/IP itself, which no header
|
|
166
|
+
fixes. Not retried: resending the same request gets
|
|
167
|
+
the same answer.
|
|
168
|
+
|
|
169
|
+
Attributes:
|
|
170
|
+
url: The request URL that returned 403.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
def __init__(self, url: str) -> None:
|
|
174
|
+
self.url = url
|
|
175
|
+
super().__init__(
|
|
176
|
+
f"Request blocked by the site's protection (not an authorization issue): {url}. "
|
|
177
|
+
"The site may have changed which requests it accepts: check for an SDK update, "
|
|
178
|
+
"or send different headers via RanobeLib(..., headers=...) / "
|
|
179
|
+
"Catalog(headers=...). If the same URL doesn't open in a browser on this network "
|
|
180
|
+
"either, the IP itself is blocked and no header change will help."
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
|
|
156
184
|
class RateLimitError(RanobeLibError):
|
|
157
185
|
"""Raised when the API responds with 429 Too Many Requests.
|
|
158
186
|
|
{ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/__init__.py
RENAMED
|
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|
|
9
9
|
|
|
10
10
|
import importlib
|
|
11
11
|
import pkgutil
|
|
12
|
-
from collections.abc import Callable
|
|
12
|
+
from collections.abc import Callable, Mapping
|
|
13
13
|
from pathlib import Path
|
|
14
14
|
from typing import ClassVar, Protocol, TypeVar, runtime_checkable
|
|
15
15
|
|
|
@@ -30,6 +30,7 @@ class Exporter(Protocol):
|
|
|
30
30
|
output_path: Path,
|
|
31
31
|
*,
|
|
32
32
|
on_chapter: Callable[[], None] | None = None,
|
|
33
|
+
headers: Mapping[str, str] | None = None,
|
|
33
34
|
) -> Path:
|
|
34
35
|
"""Write ``chapters`` (in the given order) to ``output_path``.
|
|
35
36
|
|
|
@@ -45,6 +46,10 @@ class Exporter(Protocol):
|
|
|
45
46
|
``RanobeLib.export()``'s progress bar (see CLAUDE.md's roadmap step 23).
|
|
46
47
|
For epub/pdf this covers the per-chapter embedding step, not the earlier
|
|
47
48
|
illustration-download step, which isn't itself progress-reported.
|
|
49
|
+
headers: Extra HTTP headers for any downloads the exporter makes (epub/pdf
|
|
50
|
+
illustrations), merged over the SDK's browser-like defaults —
|
|
51
|
+
``RanobeLib.export()`` passes its own ``headers=`` through here. Exporters
|
|
52
|
+
that make no requests (txt, fb2) accept and ignore it.
|
|
48
53
|
|
|
49
54
|
Returns:
|
|
50
55
|
``output_path``, once the file has been written.
|
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import html
|
|
6
|
-
from collections.abc import Callable
|
|
6
|
+
from collections.abc import Callable, Mapping
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
from typing import ClassVar
|
|
9
9
|
|
|
10
10
|
import httpx
|
|
11
11
|
from ebooklib import epub
|
|
12
12
|
|
|
13
|
+
from ranobelib._http import browser_headers
|
|
13
14
|
from ranobelib.exporters import register
|
|
14
15
|
from ranobelib.exporters._illustrations import (
|
|
15
16
|
download_images,
|
|
@@ -47,6 +48,7 @@ class EpubExporter:
|
|
|
47
48
|
output_path: Path,
|
|
48
49
|
*,
|
|
49
50
|
on_chapter: Callable[[], None] | None = None,
|
|
51
|
+
headers: Mapping[str, str] | None = None,
|
|
50
52
|
) -> Path:
|
|
51
53
|
"""Write ``chapters`` to ``output_path`` as an EPUB file.
|
|
52
54
|
|
|
@@ -60,6 +62,8 @@ class EpubExporter:
|
|
|
60
62
|
output_path: Where to write the ``.epub`` file.
|
|
61
63
|
on_chapter: Called once per chapter embedded, if given — after illustrations
|
|
62
64
|
have already been downloaded, see the ``Exporter`` protocol's docstring.
|
|
65
|
+
headers: Extra HTTP headers for illustration downloads, merged over the
|
|
66
|
+
SDK's browser-like defaults (a given key replaces the default one).
|
|
63
67
|
|
|
64
68
|
Returns:
|
|
65
69
|
``output_path``.
|
|
@@ -71,8 +75,13 @@ class EpubExporter:
|
|
|
71
75
|
for author in title.authors:
|
|
72
76
|
book.add_author(author.name)
|
|
73
77
|
|
|
78
|
+
# Browser headers: cover.cdnlibs.org's edge answers requests without a (non-empty)
|
|
79
|
+
# Referer with a 403, same as the API (see docs/api-notes.md, section "WAF 403").
|
|
74
80
|
async with httpx.AsyncClient(
|
|
75
|
-
timeout=15.0,
|
|
81
|
+
timeout=15.0,
|
|
82
|
+
follow_redirects=True,
|
|
83
|
+
transport=self._transport,
|
|
84
|
+
headers=browser_headers(headers),
|
|
76
85
|
) as client:
|
|
77
86
|
cover_url = pick_cover_url(title.cover)
|
|
78
87
|
if cover_url is not None:
|
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import datetime
|
|
6
6
|
import xml.etree.ElementTree as ET
|
|
7
|
-
from collections.abc import Callable
|
|
7
|
+
from collections.abc import Callable, Mapping
|
|
8
8
|
from html.parser import HTMLParser
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from typing import ClassVar
|
|
@@ -153,6 +153,7 @@ class Fb2Exporter:
|
|
|
153
153
|
output_path: Path,
|
|
154
154
|
*,
|
|
155
155
|
on_chapter: Callable[[], None] | None = None,
|
|
156
|
+
headers: Mapping[str, str] | None = None,
|
|
156
157
|
) -> Path:
|
|
157
158
|
"""Write ``chapters`` to ``output_path`` as FB2 XML.
|
|
158
159
|
|
|
@@ -161,6 +162,8 @@ class Fb2Exporter:
|
|
|
161
162
|
chapters: The chapters to include, in the order they should appear.
|
|
162
163
|
output_path: Where to write the ``.fb2`` file.
|
|
163
164
|
on_chapter: Called once per chapter written, if given.
|
|
165
|
+
headers: Accepted for the ``Exporter`` protocol and ignored — this format
|
|
166
|
+
downloads nothing (no illustrations in fb2, see CLAUDE.md).
|
|
164
167
|
|
|
165
168
|
Returns:
|
|
166
169
|
``output_path``.
|
|
@@ -5,12 +5,13 @@ from __future__ import annotations
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import base64
|
|
7
7
|
import html
|
|
8
|
-
from collections.abc import Callable
|
|
8
|
+
from collections.abc import Callable, Mapping
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from typing import ClassVar
|
|
11
11
|
|
|
12
12
|
import httpx
|
|
13
13
|
|
|
14
|
+
from ranobelib._http import browser_headers
|
|
14
15
|
from ranobelib.exporters import register
|
|
15
16
|
from ranobelib.exporters._illustrations import (
|
|
16
17
|
download_images,
|
|
@@ -106,6 +107,7 @@ if weasyprint is not None:
|
|
|
106
107
|
output_path: Path,
|
|
107
108
|
*,
|
|
108
109
|
on_chapter: Callable[[], None] | None = None,
|
|
110
|
+
headers: Mapping[str, str] | None = None,
|
|
109
111
|
) -> Path:
|
|
110
112
|
"""Write ``chapters`` to ``output_path`` as a PDF file.
|
|
111
113
|
|
|
@@ -116,12 +118,18 @@ if weasyprint is not None:
|
|
|
116
118
|
on_chapter: Called once per chapter rendered to HTML, if given — after
|
|
117
119
|
illustrations have already been downloaded, see the ``Exporter``
|
|
118
120
|
protocol's docstring.
|
|
121
|
+
headers: Extra HTTP headers for illustration downloads, merged over the
|
|
122
|
+
SDK's browser-like defaults (a given key replaces the default one).
|
|
119
123
|
|
|
120
124
|
Returns:
|
|
121
125
|
``output_path``.
|
|
122
126
|
"""
|
|
127
|
+
# Browser headers for the cover CDN's edge — see the same comment in epub.py.
|
|
123
128
|
async with httpx.AsyncClient(
|
|
124
|
-
timeout=15.0,
|
|
129
|
+
timeout=15.0,
|
|
130
|
+
follow_redirects=True,
|
|
131
|
+
transport=self._transport,
|
|
132
|
+
headers=browser_headers(headers),
|
|
125
133
|
) as client:
|
|
126
134
|
cover_url = pick_cover_url(title.cover)
|
|
127
135
|
cover_data_uri = None
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from collections.abc import Callable
|
|
5
|
+
from collections.abc import Callable, Mapping
|
|
6
6
|
from html.parser import HTMLParser
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
from typing import ClassVar
|
|
@@ -77,6 +77,7 @@ class TxtExporter:
|
|
|
77
77
|
output_path: Path,
|
|
78
78
|
*,
|
|
79
79
|
on_chapter: Callable[[], None] | None = None,
|
|
80
|
+
headers: Mapping[str, str] | None = None,
|
|
80
81
|
) -> Path:
|
|
81
82
|
"""Write ``chapters`` to ``output_path`` as plain text.
|
|
82
83
|
|
|
@@ -85,6 +86,8 @@ class TxtExporter:
|
|
|
85
86
|
chapters: The chapters to include, in the order they should appear.
|
|
86
87
|
output_path: Where to write the ``.txt`` file.
|
|
87
88
|
on_chapter: Called once per chapter written, if given.
|
|
89
|
+
headers: Accepted for the ``Exporter`` protocol and ignored — this format
|
|
90
|
+
downloads nothing (no illustrations in txt, see CLAUDE.md).
|
|
88
91
|
|
|
89
92
|
Returns:
|
|
90
93
|
``output_path``.
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
|
-
from collections.abc import Callable
|
|
6
|
+
from collections.abc import Callable, Mapping
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
from types import TracebackType
|
|
9
9
|
from typing import Any, Self
|
|
@@ -68,6 +68,7 @@ class RanobeLib:
|
|
|
68
68
|
cache_dir: str | Path | None = None,
|
|
69
69
|
cache_ttl: float | None = None,
|
|
70
70
|
verbosity: Verbosity = False,
|
|
71
|
+
headers: Mapping[str, str] | None = None,
|
|
71
72
|
) -> None:
|
|
72
73
|
"""Initialize the SDK for a title.
|
|
73
74
|
|
|
@@ -85,9 +86,18 @@ class RanobeLib:
|
|
|
85
86
|
``"full"``: the same progress bars, plus a line logged for every title/
|
|
86
87
|
chapter-list/chapter-content fetch, noting whether it was served from the
|
|
87
88
|
disk cache or the API.
|
|
89
|
+
headers: Extra HTTP headers merged over the SDK's defaults (browser-like
|
|
90
|
+
``Origin``/``Referer``/``User-Agent``, see docs/api-notes.md, section "WAF
|
|
91
|
+
403") — a key given here replaces the default one, matched
|
|
92
|
+
case-insensitively. Applied to every API request and, through ``export()``,
|
|
93
|
+
to epub/pdf illustration downloads. Meant for when the site's edge protection
|
|
94
|
+
changes which requests it lets through (``AccessBlockedError``): send what it
|
|
95
|
+
now expects without waiting for an SDK release. ``None`` (the default) sends
|
|
96
|
+
the defaults as-is.
|
|
88
97
|
"""
|
|
89
98
|
self._slug_url = parse_slug_url(url)
|
|
90
|
-
self.
|
|
99
|
+
self._headers = dict(headers) if headers is not None else None
|
|
100
|
+
self._client = ApiClient(headers=self._headers)
|
|
91
101
|
self._cache = DiskCache(
|
|
92
102
|
cache_dir if cache_dir is not None else DEFAULT_CACHE_DIR, ttl=cache_ttl
|
|
93
103
|
)
|
|
@@ -457,7 +467,8 @@ class RanobeLib:
|
|
|
457
467
|
chapters: The chapters to include, in the order they should appear.
|
|
458
468
|
path: Where to write the exported file.
|
|
459
469
|
fmt: Export format — a key of ``ranobelib.exporters.EXPORTERS``
|
|
460
|
-
(currently: ``"txt"``, ``"fb2"``, ``"epub"``
|
|
470
|
+
(currently: ``"txt"``, ``"fb2"``, ``"epub"``, and ``"pdf"`` where
|
|
471
|
+
WeasyPrint's native libraries are available).
|
|
461
472
|
|
|
462
473
|
Raises:
|
|
463
474
|
ValueError: If ``fmt`` isn't a registered export format.
|
|
@@ -468,7 +479,9 @@ class RanobeLib:
|
|
|
468
479
|
raise ValueError(f"Unknown export format {fmt!r}. Available: {available}")
|
|
469
480
|
title = await self.get_info()
|
|
470
481
|
with self._reporter.progress(f"Exporting to {fmt}", len(chapters)) as advance:
|
|
471
|
-
return await exporter_cls().export(
|
|
482
|
+
return await exporter_cls().export(
|
|
483
|
+
title, chapters, Path(path), on_chapter=advance, headers=self._headers
|
|
484
|
+
)
|
|
472
485
|
|
|
473
486
|
async def _build_volume(self, volume: int, raw_chapters: list[dict[str, Any]]) -> Volume:
|
|
474
487
|
volume_str = str(volume)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{ranobelib_python_sdk-0.10.0 → ranobelib_python_sdk-0.11.0}/src/ranobelib/exporters/_shared.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|