ranobelib API reference
Entry point
ranobelib.RanobeLib
RanobeLib(
url: str,
*,
cache_dir: str | Path | None = None,
cache_ttl: float | None = None,
verbosity: Verbosity = False,
headers: Mapping[str, str] | None = None,
)
Bases: TitleClient[Title, Chapter, Volume]
Entry point for interacting with a single ranobelib.me title.
Example
async with RanobeLib("https://ranobelib.me/ru/book/6712--high-school-dxd-novel") as lib:
info = await lib.get_info()
Initialize the SDK for a title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
A ranobelib.me title URL, or a bare |
required |
cache_dir
|
str | Path | None
|
Where to cache raw API responses (title metadata, chapter list,
chapter content) on disk, so a repeated export or downloading newly added
chapters doesn't re-fetch data already on hand. Defaults to
|
None
|
cache_ttl
|
float | None
|
Seconds after which a cached response is treated as stale and
re-fetched. |
None
|
verbosity
|
Verbosity
|
Console output level. |
False
|
headers
|
Mapping[str, str] | None
|
Extra HTTP headers merged over the SDK's defaults (browser-like
|
None
|
Source code in packages/ranobelib/src/ranobelib/sdk.py
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 | |
slug_url
property
slug_url: str
The title's {id}--{slug} identifier.
aclose
async
aclose() -> None
Close the underlying HTTP client.
Source code in packages/core/src/libsocial/title.py
173 174 175 | |
download_title
async
download_title(
*,
branch_id: int | None = None,
translation_index: int | None = None,
chapter_delay: float = 0.0,
on_chapter: Callable[[int, int], None] | None = None,
max_rate_limit_retries: int = DEFAULT_RATE_LIMIT_RETRIES,
) -> list[VolumeT]
Download every chapter of the title, across all its volumes.
Fetches the chapter list once, then every chapter's content, sequentially, in the
API's own order — same pacing/retry/backoff as any other chapter fetch (see
ApiClient), plus chapter_delay on top if given.
Resolving translations is checked for the whole title up front, not chapter by
chapter: if any chapters have more than one translation, this fetches nothing and
raises MultipleTitleTranslationsError listing all of them at once (unless
branch_id or translation_index resolves every one of them), rather than
failing partway through a long download on the first ambiguous chapter.
A long title's sequential download can outlast ApiClient's own retry budget for
429s (tuned for a single one-off request, not hundreds/thousands of sequential
fetches — see docs/api-notes.md). On top of that, each chapter fetch that still comes
back rate-limited is retried again here, up to max_rate_limit_retries times, with
its own (coarser) backoff. If a chapter fetch still fails after that — from rate
limiting or anything else — the chapters already downloaded are not discarded: they're
attached to a raised DownloadTitleInterruptedError instead of being lost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branch_id
|
int | None
|
Translation to use for every chapter that has more than one — same
meaning as |
None
|
translation_index
|
int | None
|
Alternative to |
None
|
chapter_delay
|
float
|
Extra delay, in seconds, after fetching each chapter, on top of
|
0.0
|
on_chapter
|
Callable[[int, int], None] | None
|
Called with |
None
|
max_rate_limit_retries
|
int
|
How many extra times to retry a single chapter fetch that
comes back rate-limited, on top of |
DEFAULT_RATE_LIMIT_RETRIES
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
MultipleTitleTranslationsError
|
If the title has one or more chapters with more
than one translation that |
DownloadTitleInterruptedError
|
If fetching a chapter fails unrecoverably partway through the download — carries every chapter already fetched. |
Source code in packages/core/src/libsocial/title.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
estimate_title_size
async
estimate_title_size(
*,
sample_size: int = DEFAULT_SAMPLE_SIZE,
average_image_size: int = DEFAULT_AVERAGE_IMAGE_SIZE,
branch_id: int | None = None,
translation_index: int | None = None,
) -> int
Estimate the whole title's download size in bytes, from a sample of its chapters.
Fetches the chapter list (cheap, cached), then the content of up to sample_size
chapters spread evenly across the title — not just the first ones, since early and
late chapters can differ in length — and extrapolates the sample's average
sizing.chapter_size() across the title's total chapter count. This trades
exactness for cost: an exact total requires fetching every chapter's content, the
same cost as download_title() itself, which defeats the purpose of checking
"is this worth downloading" before paying that cost.
Sampled chapters go through the same disk cache as any other chapter fetch, so a
later download_title() call doesn't refetch them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_size
|
int
|
How many chapters to sample. Clamped to the number of chapters the title actually has (or that resolve to a usable translation, see below) if that's fewer. |
DEFAULT_SAMPLE_SIZE
|
average_image_size
|
int
|
Forwarded to |
DEFAULT_AVERAGE_IMAGE_SIZE
|
branch_id
|
int | None
|
Same meaning as |
None
|
translation_index
|
int | None
|
Same meaning as |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The estimated size in bytes, or |
int
|
its chapters have a translation |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Note
Unlike download_title(), a chapter with more than one translation that
branch_id/translation_index doesn't resolve is skipped when picking the
sample rather than raising MultipleTitleTranslationsError — an estimate
doesn't need a specific translation to be correct, only a chapter's worth of
this title's content to measure.
Source code in packages/ranobelib/src/ranobelib/sdk.py
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 | |
export
async
export(
chapters: list[Chapter], *, fmt: str, path: str | Path
) -> Path
Export chapters to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapters
|
list[Chapter]
|
The chapters to include, in the order they should appear. |
required |
path
|
str | Path
|
Where to write the exported file. |
required |
fmt
|
str
|
Export format — a key of |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/sdk.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
get_chapter
async
get_chapter(
volume: int,
number: str,
*,
branch_id: int | None = None,
refresh: bool = False,
) -> ChapterT
Fetch a single chapter, including its content.
When a chapter has more than one team's translation and branch_id isn't given,
this raises MultipleTranslationsError rather than guessing — see
get_translations() and docs/api-notes.md for why the API's own default (when
branch_id is omitted) isn't relied on. Doing this check costs an extra request
to fetch the chapter list, only when branch_id isn't already given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
int
|
The chapter's volume number. |
required |
number
|
str
|
The chapter number, as returned by the API — may contain a decimal
(e.g. |
required |
branch_id
|
int | None
|
Which translation to fetch, as returned by |
None
|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if a cached response is on hand. |
False
|
Raises:
| Type | Description |
|---|---|
MultipleTranslationsError
|
If the chapter has more than one translation and
|
Source code in packages/core/src/libsocial/title.py
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 | |
get_chapters
async
get_chapters(
chapters: list[tuple[int, str]],
) -> list[ChapterT]
Fetch several chapters, including their content.
Fetches the chapter list once, shared across all requested chapters (also used to
detect chapters with more than one translation — see get_chapter), then each
chapter individually and sequentially, in the order given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapters
|
list[tuple[int, str]]
|
A list of |
required |
Raises:
| Type | Description |
|---|---|
ChapterNotFoundError
|
If any requested chapter doesn't exist. |
MultipleTranslationsError
|
If any requested chapter has more than one translation. |
Source code in packages/core/src/libsocial/title.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
get_info
async
get_info(*, refresh: bool = False) -> TitleT
Fetch title metadata: names, cover, summary, genres, tags, authors, teams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if a cached response is on hand. |
False
|
Source code in packages/core/src/libsocial/title.py
177 178 179 180 181 182 183 184 185 | |
get_table_of_contents
async
get_table_of_contents(
*, refresh: bool = False
) -> list[VolumeT]
Fetch the title's volumes and chapter names/numbers, without chapter content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if a cached response is on hand — needed to pick up newly published chapters, since otherwise the cached chapter list would keep being reused. |
False
|
Source code in packages/core/src/libsocial/title.py
187 188 189 190 191 192 193 194 195 196 197 | |
get_translations
async
get_translations(
volume: int, number: str
) -> list[ChapterBranch]
Fetch the available translations (branches) for a chapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
int
|
The chapter's volume number. |
required |
number
|
str
|
The chapter number. |
required |
Raises:
| Type | Description |
|---|---|
ChapterNotFoundError
|
If no chapter exists for this volume/number. |
Source code in packages/core/src/libsocial/title.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_volume
async
get_volume(volume: int) -> VolumeT
Fetch a whole volume: all its chapters, each including content.
The API has no bulk "volume content" endpoint (see docs/api-notes.md), so this fetches the chapter list once to find which numbers belong to the volume, then fetches each of those chapters individually and sequentially.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
int
|
The volume number. |
required |
Raises:
| Type | Description |
|---|---|
VolumeNotFoundError
|
If the title has no chapters for this volume. |
MultipleTranslationsError
|
If any of the volume's chapters has more than one translation. |
Source code in packages/core/src/libsocial/title.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
get_volumes
async
get_volumes(volumes: list[int]) -> list[VolumeT]
Fetch several whole volumes, each including chapter content.
Fetches the chapter list once, shared across all requested volumes, then each
chapter individually and sequentially — same approach as get_volume, applied
to more than one volume without re-fetching the chapter list per volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volumes
|
list[int]
|
The volume numbers to fetch. |
required |
Raises:
| Type | Description |
|---|---|
VolumeNotFoundError
|
If the title has no chapters for one of the volumes. |
MultipleTranslationsError
|
If any of the volumes' chapters has more than one translation. |
Source code in packages/core/src/libsocial/title.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
ranobelib.Catalog
Catalog(
*,
cache_dir: str | Path | None = None,
cache_ttl: float | None = None,
headers: Mapping[str, str] | None = None,
)
Entry point for listing/searching the ranobelib.me catalog.
Lists titles (list_titles()) and the filter options for them (list_genres(),
list_countries(), list_statuses()), each filtered to ranobelib.me.
Example
async with Catalog() as catalog:
page = await catalog.list_titles(query="dxd", genres=[34], statuses=[1])
for title in page.items:
print(title.name)
Initialize the catalog client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | Path | None
|
Where to cache raw API responses on disk, one entry per distinct set
of listing parameters (page/filters/sort) — same disk cache as |
None
|
cache_ttl
|
float | None
|
Seconds after which a cached page is treated as stale and re-fetched.
|
None
|
headers
|
Mapping[str, str] | None
|
Extra HTTP headers merged over the SDK's defaults, same as
|
None
|
Source code in packages/ranobelib/src/ranobelib/catalog.py
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 | |
aclose
async
aclose() -> None
Close the underlying HTTP client.
Source code in packages/core/src/libsocial/catalog.py
103 104 105 | |
list_countries
async
list_countries(*, refresh: bool = False) -> list[Country]
Fetch the full list of catalog countries (id → name), for use as filter options
with list_titles(countries=...).
Mirrors list_genres() exactly, down to the network-wide, not-site-scoped shape
of the underlying endpoint (GET /api/constants?fields[]=types — the API's own
name for what this SDK calls "country" is "type", see docs/api-notes.md and
Country's docstring). This filters that down to entries tagged for site before
returning, same as list_genres() does for genres.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if the country list was already cached. |
False
|
Returns:
| Type | Description |
|---|---|
list[Country]
|
Every |
Source code in packages/core/src/libsocial/catalog.py
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 | |
list_genres
async
list_genres(*, refresh: bool = False) -> list[Genre]
Fetch the full list of catalog genres (id → name), for use as filter options
with list_titles(genres=[...]).
The underlying endpoint (GET /api/constants?fields[]=genres) has no site-scoping
parameter — it returns the full genre list shared across the whole lib.social
network in one shot, each tagged with the site ids it applies to (see
docs/api-notes.md). This filters that down to genres tagged for site before
returning, so callers don't get filter options that could never match any of the
site's titles (e.g. "Детское", tagged only for site 5, on ranobelib.me).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if the genre list was already cached. |
False
|
Returns:
| Type | Description |
|---|---|
list[Genre]
|
Every |
Source code in packages/core/src/libsocial/catalog.py
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 | |
list_statuses
async
list_statuses(*, refresh: bool = False) -> list[Label]
Fetch the full list of title statuses (id → label), for use as filter options
with list_titles(statuses=...).
Mirrors list_genres()/list_countries(): the underlying endpoint
(GET /api/constants?fields[]=status) isn't site-scoped, so entries not tagged for
site are dropped before returning (see docs/api-notes.md). Returns the same
Label model as Title.status, so a title's status can be compared against
these directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if the status list was already cached. |
False
|
Returns:
| Type | Description |
|---|---|
list[Label]
|
Every status ( |
Source code in packages/core/src/libsocial/catalog.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
list_titles
async
list_titles(
*,
page: int = 1,
per_page: int = 30,
query: str | None = None,
genres: list[int] | None = None,
tags: list[int] | None = None,
statuses: list[int] | None = None,
countries: list[int] | None = None,
min_chapters: int | None = None,
max_chapters: int | None = None,
sort: str = DEFAULT_SORT,
refresh: bool = False,
) -> CatalogPage[TitleT]
Fetch one page of catalog listing/search results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
int
|
1-based page number. |
1
|
per_page
|
int
|
How many titles per page. Must be between |
30
|
query
|
str | None
|
Free-text search term, matched against name/rus_name/eng_name. |
None
|
genres
|
list[int] | None
|
Genre ids to filter by. A title must have all of them, not just one
(AND, not OR — see docs/api-notes.md). Not validated against |
None
|
tags
|
list[int] | None
|
|
None
|
statuses
|
list[int] | None
|
|
None
|
countries
|
list[int] | None
|
|
None
|
min_chapters
|
int | None
|
Only titles with at least this many chapters (inclusive). Counts
the same number as |
None
|
max_chapters
|
int | None
|
Only titles with at most this many chapters (inclusive). |
None
|
sort
|
str
|
Sort order. Despite the name, this is sent as the API's |
DEFAULT_SORT
|
refresh
|
bool
|
Bypass the disk cache and re-fetch from the API even if this exact combination of parameters was already cached. |
False
|
Returns:
| Type | Description |
|---|---|
CatalogPage[TitleT]
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/core/src/libsocial/catalog.py
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 | |
Models
ranobelib.CatalogPage
Bases: CatalogPage[Title]
One page of catalog listing/search results, as returned by Catalog.list_titles().
items reuses Title as-is — a catalog list item has every field Title requires,
the ones it doesn't send (genres, summary, chapter_count, last_chapter_at,
...) just come back at their defaults, same as any other partially-populated Title
(see docs/api-notes.md).
ranobelib.Title
Bases: Title
Metadata for a single title (novel).
base_names
property
base_names: list[str]
The title's names without the format suffix ranobelib.me appends to them.
rus_name, eng_name, name and other_names, in that order, each with
trailing publication-format markers removed — "Старшая школа D×D (Новелла)" →
"Старшая школа D×D", likewise (Novel), (LN), (WN), (Light Novel),
(Ранобэ), [квест], ... (stacked ones too, (LN) (Novel)). Other bracketed
tails — a year, (18+), (Фанфик), [LitRPG], a franchise name — distinguish
one title from another and are kept. Whitespace is trimmed; empty names and
duplicates (keeping the first) are dropped. Nothing else is normalized (case,
punctuation, ×): that's up to whatever compares the names.
The original fields are left as they are. On Catalog.list_titles() items
other_names is empty (the listing doesn't return it), so it contributes nothing.
chapter_count
class-attribute
instance-attribute
chapter_count: int | None = None
Number of uploaded chapters. Only TitleClient.get_info() fills it in: the catalog
listing endpoint doesn't return it (see docs/api-notes.md), so it's None on
Catalog.list_titles() items.
last_chapter_at
class-attribute
instance-attribute
last_chapter_at: datetime | None = Field(
default=None, alias="last_item_at"
)
When the most recent chapter was published — the value the catalog's
sort="last_chapter_at" orders by. Only TitleClient.get_info() fills it in: the
catalog listing endpoint doesn't return it (see docs/api-notes.md), so it's None on
Catalog.list_titles() items, and on a title with no chapters yet.
ranobelib.Chapter
Bases: Chapter
A chapter: volume, number, name, available translations, and optionally its content.
index/item_number/branches_count/branches come from the chapter-list
endpoint (see RanobeLib.get_table_of_contents()) and are None/empty when a
Chapter instead comes from fetching a single chapter's content, which has a
different response shape (see docs/api-notes.md). content is the reverse: only
populated by the single-chapter endpoint.
footnotes holds the chapter's translator footnotes (see Footnote), which are
removed from content rather than kept in both places — empty when the chapter has
none, or when content isn't fetched.
ranobelib.Footnote
Bases: BaseModel
A translator footnote (term explanation, translation note) taken out of a chapter.
The API has no structured footnotes (see docs/api-notes.md): they're ordinary
paragraphs/list items whose text starts with ↑, so that arrow is what the SDK detects
them by — and strips from content. There's no marker linking a footnote back to a
point in the text: translators mark the reference with an unnumbered * at best, which
the text also uses for scene breaks and censored words, so Chapter.footnotes's order
(the order they appear in the source) is the only link there is.
content
instance-attribute
content: str
Sanitized inline HTML (same tag vocabulary as Chapter.content), without the arrow
and without a wrapping <p>.
ranobelib.Volume
ranobelib.ChapterBranch
Bases: BaseModel
A single team's (or solo uploader's) translation of a chapter.
A chapter has more than one branch when several teams have translated it
independently; see Chapter.branches_count.
ranobelib.Genre
Bases: BaseModel
A genre tag (e.g. Fantasy, Romance).
ranobelib.Country
Bases: BaseModel
A title's country/region of origin.
Maps onto what the raw API itself calls "type" (Title's raw type field; the
catalog filter's types[] parameter; GET /api/constants?fields[]=types) — not a
country/countries[]/fields[]=countries concept, which turned out to be
something unrelated (see docs/api-notes.md). The values aren't all literal countries: on
ranobelib.me, Japan, Korea and China sit next to original English-language work, original
web novel and fanfiction, all surfaced as-is.
ranobelib.Label
Bases: BaseModel
A small {id, label} enum value used throughout the API (status, age rating, ...).
Exceptions
ranobelib.RanobeLibError
module-attribute
RanobeLibError = LibSocialError
Base class for all errors raised by the SDK (libsocial.LibSocialError).
ranobelib.TitleNotFoundError
TitleNotFoundError(slug_url: str)
Bases: LibSocialError
Raised when a title cannot be found on the site.
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
Source code in packages/core/src/libsocial/exceptions.py
23 24 25 | |
ranobelib.ChapterNotFoundError
ChapterNotFoundError(
slug_url: str, *, volume: str, number: str
)
Bases: LibSocialError
Raised when a chapter cannot be found for a given volume/number.
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
|
volume |
The volume number that was requested. |
|
number |
The chapter number that was requested. |
Source code in packages/core/src/libsocial/exceptions.py
37 38 39 40 41 | |
ranobelib.VolumeNotFoundError
VolumeNotFoundError(slug_url: str, *, volume: str)
Bases: LibSocialError
Raised when a title has no chapters for a given volume number.
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
|
volume |
The volume number that was requested. |
Source code in packages/core/src/libsocial/exceptions.py
52 53 54 55 | |
ranobelib.MultipleTranslationsError
MultipleTranslationsError(
slug_url: str,
*,
volume: str,
number: str,
branches: list[ChapterBranch],
)
Bases: LibSocialError
Raised when a chapter has more than one team's translation and none was selected.
The SDK does not guess a default: the API's own default when branch_id is omitted
is not simply the first branch listed, and the actual rule it uses isn't documented
(see docs/api-notes.md) — returning it silently would be unpredictable. Call
TitleClient.get_translations() to list the available branches, then pass one's
branch_id explicitly.
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
|
volume |
The chapter's volume number. |
|
number |
The chapter number. |
|
branches |
The chapter's available translations, as returned by
|
Source code in packages/core/src/libsocial/exceptions.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
ranobelib.MultipleTitleTranslationsError
MultipleTitleTranslationsError(
slug_url: str, *, chapters: list[AmbiguousChapter]
)
Bases: LibSocialError
Raised by download_title() when one or more chapters have an unresolved translation.
Unlike MultipleTranslationsError (one chapter), this can list several: a title-wide
bulk download checks every chapter up front rather than stopping at the first ambiguous
one, so a caller doesn't have to fix one chapter, rerun, hit the next, and rerun again.
Pass branch_id or translation_index to download_title() to resolve them, or
fetch the listed chapters individually with get_chapter(..., branch_id=...).
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
|
chapters |
The ambiguous chapters and their available translations. |
Source code in packages/core/src/libsocial/exceptions.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
ranobelib.AuthRequiredError
AuthRequiredError(url: str)
Bases: LibSocialError
Raised when the requested content requires authorization (paid or early access).
Attributes:
| Name | Type | Description |
|---|---|---|
url |
The request URL that returned 403. |
Source code in packages/core/src/libsocial/exceptions.py
152 153 154 | |
ranobelib.AccessBlockedError
AccessBlockedError(
url: str, *, headers_hint: str = DEFAULT_HEADERS_HINT
)
Bases: LibSocialError
Raised when the site's edge protection rejects a request before it reaches the API.
Not an authorization problem, unlike AuthRequiredError: the DDoS-Guard edge in front
of api.cdnlibs.org answers with an HTML 403 page (the API's own 403 is JSON) when it
doesn't like the request — so far, a missing or empty Referer (see
docs/api-notes.md, section "WAF 403"). The SDK already sends browser-like headers, so
seeing this means either the edge changed its rules — check for an SDK update, or send
what it now expects via headers= on the title client/Catalog (or ApiClient,
if using it directly) — or the edge is blocking the network/IP itself, which no header
fixes. Not retried: resending the same request gets the same answer.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
The request URL that returned 403. |
Initialize the error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The request URL that returned 403. |
required |
headers_hint
|
str
|
Where to pass different headers, as the message suggests it — a
site package names its own entry points (see |
DEFAULT_HEADERS_HINT
|
Source code in packages/core/src/libsocial/exceptions.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
ranobelib.RateLimitError
RateLimitError(retry_after: float | None = None)
Bases: LibSocialError
Raised when the API responds with 429 Too Many Requests.
Attributes:
| Name | Type | Description |
|---|---|---|
retry_after |
The response's |
Source code in packages/core/src/libsocial/exceptions.py
202 203 204 205 | |
ranobelib.DownloadTitleInterruptedError
DownloadTitleInterruptedError(
slug_url: str,
*,
volumes: Sequence[Volume[Any]],
completed: int,
total: int,
)
Bases: LibSocialError
Raised by download_title() when a chapter fetch fails unrecoverably partway through.
download_title() fetches hundreds-to-thousands of chapters sequentially for a long
title, which can outlast even the extra, bulk-specific rate-limit retry budget described
in download_title()'s max_rate_limit_retries — or hit some other unrecoverable
error (e.g. AuthRequiredError on a chapter that turns out to need a paywall/auth
token). Rather than letting that error propagate on its own and silently discard every
chapter already fetched, it's wrapped in this exception instead, carrying what was
already downloaded — so a caller can keep that partial result, or simply call
download_title() again: the SDK's disk cache means already-fetched chapters aren't
re-requested, so a retry resumes close to where this one stopped rather than restarting
the whole title. See docs/api-notes.md's "Rate limiting и retry" section and issue #41.
Attributes:
| Name | Type | Description |
|---|---|---|
slug_url |
The title's |
|
volumes |
list[Volume[Any]]
|
Chapters fetched before the failure, grouped into volumes the same way a
successful |
completed |
How many chapters were fetched before the failure. |
|
total |
How many chapters |
Source code in packages/core/src/libsocial/exceptions.py
230 231 232 233 234 235 236 237 238 239 240 | |
Cache maintenance
ranobelib.cache_stats
cache_stats(
cache_dir: str | Path = DEFAULT_CACHE_DIR,
) -> CacheStats
Count the entries in an SDK disk cache and their total size on disk.
Only files the cache itself writes are counted, not anything else that happens to be in the directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | Path
|
The cache directory — the same |
DEFAULT_CACHE_DIR
|
Returns:
| Type | Description |
|---|---|
CacheStats
|
The number of entries and their total size in bytes. A missing directory is an empty |
CacheStats
|
cache, |
Source code in packages/ranobelib/src/ranobelib/cache.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
ranobelib.clear_cache
clear_cache(
cache_dir: str | Path = DEFAULT_CACHE_DIR,
*,
older_than: float | None = None,
) -> CacheStats
Remove entries from an SDK disk cache.
Only files the cache itself writes are removed — anything else in the directory, and the directory itself, is left alone. Safe to call while clients are using the same cache: an entry being read as it's removed is just a cache miss, and one written after it's removed is simply recreated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | Path
|
The cache directory — see |
DEFAULT_CACHE_DIR
|
older_than
|
float | None
|
If given, only remove entries written more than this many seconds ago
(the same age check |
None
|
Returns:
| Type | Description |
|---|---|
CacheStats
|
The number of entries removed and the bytes they took. A missing directory removes |
CacheStats
|
nothing, |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/cache.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
ranobelib.CacheStats
dataclass
CacheStats(entries: int, bytes: int)
How many cache entries there are (or were removed), and their total size on disk.
Sizing
ranobelib.chapter_size
chapter_size(
chapter: Chapter,
*,
average_image_size: int = DEFAULT_AVERAGE_IMAGE_SIZE,
) -> int
Estimate a fetched chapter's size in bytes: exact text plus assumed-average images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapter
|
Chapter
|
A chapter with content already fetched (e.g. via |
required |
average_image_size
|
int
|
Assumed bytes per |
DEFAULT_AVERAGE_IMAGE_SIZE
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/sizing.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
ranobelib.volume_size
volume_size(
volume: Volume,
*,
average_image_size: int = DEFAULT_AVERAGE_IMAGE_SIZE,
) -> int
Estimate a fetched volume's size in bytes: the sum of its chapters' chapter_size().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
Volume
|
A volume whose chapters have content already fetched (e.g. via
|
required |
average_image_size
|
int
|
Forwarded to |
DEFAULT_AVERAGE_IMAGE_SIZE
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any chapter in |
Source code in packages/ranobelib/src/ranobelib/sizing.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Chapter content
ranobelib.chapter_image_urls
chapter_image_urls(chapter: Chapter) -> list[str]
List the image URLs a fetched chapter references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapter
|
Chapter
|
A chapter with content already fetched (e.g. via |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Every |
list[str]
|
document order, each URL listed once (its first occurrence). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/images.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
ranobelib.rewrite_image_urls
rewrite_image_urls(
chapter: Chapter, replace: Callable[[str], str]
) -> Chapter
Return a copy of a fetched chapter with every <img src> passed through replace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapter
|
Chapter
|
A chapter with content already fetched (e.g. via |
required |
replace
|
Callable[[str], str]
|
Called with each image URL (once per |
required |
Returns:
| Type | Description |
|---|---|
Chapter
|
A new |
Chapter
|
rewritten; every other field is unchanged, and |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/images.py
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 | |
ranobelib.chapter_paragraphs
chapter_paragraphs(chapter: Chapter) -> list[str]
A fetched chapter's top-level blocks as plain text, in document order.
Entry i is the text of the i-th top-level element of chapter.content — the
same position a browser gives it among the rendered content's element children — so a
paragraph index counted in the DOM can be used to look it up directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chapter
|
Chapter
|
A chapter with content already fetched (e.g. via |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
One string per top-level element: its text with entities decoded and whitespace |
list[str]
|
collapsed to single spaces ( |
list[str]
|
an image-only paragraph). |
list[str]
|
of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/ranobelib/src/ranobelib/paragraphs.py
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 | |
Exporters
Not re-exported from the top-level ranobelib package — import from ranobelib.exporters.
Adding a new export format is a new module in ranobelib/exporters/ implementing
Exporter and decorated with @register; no changes needed elsewhere.
ranobelib.exporters.Exporter
Bases: Protocol
Renders a title's chapters to a single file in some format.
format
class-attribute
format: str
The registry key this exporter is selected by, e.g. "txt".
export
async
export(
title: Title,
chapters: list[Chapter],
output_path: Path,
*,
on_chapter: Callable[[], None] | None = None,
headers: Mapping[str, str] | None = None,
) -> Path
Write chapters (in the given order) to output_path.
async since embedding illustrations (epub, pdf) requires downloading them —
the SDK is async-only throughout (see CLAUDE.md), so this can't drop to a sync
HTTP call. txt/fb2 do no I/O and just don't await anything in their bodies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
Title
|
The chapters' parent title, for metadata (name, authors, ...). |
required |
chapters
|
list[Chapter]
|
The chapters to include, in the order they should appear. |
required |
output_path
|
Path
|
Where to write the exported file. |
required |
on_chapter
|
Callable[[], None] | None
|
Called once per chapter processed, if given — drives
|
None
|
headers
|
Mapping[str, str] | None
|
Extra HTTP headers for any downloads the exporter makes (epub/pdf
illustrations), merged over the SDK's browser-like defaults —
|
None
|
Returns:
| Type | Description |
|---|---|
Path
|
|
Source code in packages/ranobelib/src/ranobelib/exporters/__init__.py
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 | |
ranobelib.exporters.register
register(exporter: ExporterT) -> ExporterT
Class decorator: register exporter under its format key.
Source code in packages/ranobelib/src/ranobelib/exporters/__init__.py
66 67 68 69 | |
Registered exporters, keyed by Exporter.format.