ao3fetch 0.1.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.
- ao3fetch-0.1.0/API_REFERENCE.md +838 -0
- ao3fetch-0.1.0/LICENSE.md +10 -0
- ao3fetch-0.1.0/MANIFEST.in +6 -0
- ao3fetch-0.1.0/PKG-INFO +123 -0
- ao3fetch-0.1.0/README.md +100 -0
- ao3fetch-0.1.0/ao3fetch/__init__.py +211 -0
- ao3fetch-0.1.0/ao3fetch/downloads.py +455 -0
- ao3fetch-0.1.0/ao3fetch/entities.py +481 -0
- ao3fetch-0.1.0/ao3fetch/fetcher.py +256 -0
- ao3fetch-0.1.0/ao3fetch/languages.py +176 -0
- ao3fetch-0.1.0/ao3fetch/markdown.py +200 -0
- ao3fetch-0.1.0/ao3fetch/pageLoaders.py +109 -0
- ao3fetch-0.1.0/ao3fetch/pagination.py +28 -0
- ao3fetch-0.1.0/ao3fetch/py.typed +0 -0
- ao3fetch-0.1.0/ao3fetch/series.py +82 -0
- ao3fetch-0.1.0/ao3fetch/t.py +36 -0
- ao3fetch-0.1.0/ao3fetch/tags.py +231 -0
- ao3fetch-0.1.0/ao3fetch/urls.py +282 -0
- ao3fetch-0.1.0/ao3fetch/users.py +135 -0
- ao3fetch-0.1.0/ao3fetch/utils.py +139 -0
- ao3fetch-0.1.0/ao3fetch/works.py +609 -0
- ao3fetch-0.1.0/ao3fetch.egg-info/PKG-INFO +123 -0
- ao3fetch-0.1.0/ao3fetch.egg-info/SOURCES.txt +29 -0
- ao3fetch-0.1.0/ao3fetch.egg-info/dependency_links.txt +1 -0
- ao3fetch-0.1.0/ao3fetch.egg-info/requires.txt +1 -0
- ao3fetch-0.1.0/ao3fetch.egg-info/top_level.txt +1 -0
- ao3fetch-0.1.0/pyproject.toml +143 -0
- ao3fetch-0.1.0/semver.txt +1 -0
- ao3fetch-0.1.0/setup.cfg +4 -0
- ao3fetch-0.1.0/tests/test_ao3fetch.py +417 -0
- ao3fetch-0.1.0/tests/test_markdown.py +38 -0
|
@@ -0,0 +1,838 @@
|
|
|
1
|
+
# `ao3fetch` API Reference
|
|
2
|
+
|
|
3
|
+
Complete reference for all public functions, classes, enums, type aliases, exceptions, and constants exported by `ao3fetch`.
|
|
4
|
+
|
|
5
|
+
All symbols documented below are re-exported directly from the top-level `ao3fetch` package (e.g. `import ao3fetch; ao3fetch.getWorkContent(...)`).
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Works & Chapters](#works--chapters)
|
|
10
|
+
- [Functions](#works-functions)
|
|
11
|
+
- [Data Classes](#works-data-classes)
|
|
12
|
+
- [Enums](#works-enums)
|
|
13
|
+
- [EPUB Downloads](#epub-downloads)
|
|
14
|
+
- [Functions](#epub-functions)
|
|
15
|
+
- [Data Classes](#epub-data-classes)
|
|
16
|
+
- [Series](#series)
|
|
17
|
+
- [Functions](#series-functions)
|
|
18
|
+
- [Data Classes](#series-data-classes)
|
|
19
|
+
- [Users](#users)
|
|
20
|
+
- [Functions](#users-functions)
|
|
21
|
+
- [Data Classes](#users-data-classes)
|
|
22
|
+
- [Type Aliases](#users-type-aliases)
|
|
23
|
+
- [Tags & Tag Search](#tags--tag-search)
|
|
24
|
+
- [Functions](#tags-functions)
|
|
25
|
+
- [Data Classes](#tags-data-classes)
|
|
26
|
+
- [Type Aliases](#tags-type-aliases)
|
|
27
|
+
- [Pagination](#pagination)
|
|
28
|
+
- [Functions](#pagination-functions)
|
|
29
|
+
- [Protocol & Data Class](#pagination-protocol--data-class)
|
|
30
|
+
- [HTML Conversion](#html-conversion)
|
|
31
|
+
- [URLs, Archive Base URL & IDs](#urls-archive-base-url--ids)
|
|
32
|
+
- [Constants & Submodule](#urls-constants--submodule)
|
|
33
|
+
- [Archive Base URL Configuration](#archive-base-url-configuration)
|
|
34
|
+
- [URL Builders](#url-builders)
|
|
35
|
+
- [URL Parsers & Data Classes](#url-parsers--data-classes)
|
|
36
|
+
- [Archive ID Helpers & Types](#archive-id-helpers--types)
|
|
37
|
+
- [HTTP Fetcher, Retry & Caching](#http-fetcher-retry--caching)
|
|
38
|
+
- [Fetcher Configuration](#fetcher-configuration)
|
|
39
|
+
- [Middleware & Helpers](#middleware--helpers)
|
|
40
|
+
- [Classes & Type Aliases](#fetcher-classes--type-aliases)
|
|
41
|
+
- [Exceptions, Constants & Shared Types](#exceptions-constants--shared-types)
|
|
42
|
+
- [Exceptions](#exceptions)
|
|
43
|
+
- [Constants](#constants)
|
|
44
|
+
- [Shared Type Aliases](#shared-type-aliases)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Works & Chapters
|
|
49
|
+
|
|
50
|
+
<a id="works-functions"></a>
|
|
51
|
+
### Functions
|
|
52
|
+
|
|
53
|
+
#### `getWork(workId: str | int) -> WorkSummary | LockedWorkSummary`
|
|
54
|
+
Fetch metadata for an archive work by ID.
|
|
55
|
+
|
|
56
|
+
- Automatically appends `view_adult=true` and sends `Cookie: view_adult=true;` to bypass adult content warning gates.
|
|
57
|
+
- Returns `LockedWorkSummary` (`locked == True`) if the work is restricted to logged-in users rather than raising an error. To scrape restricted works, install a custom fetcher via [`setFetcher`](#setfetcher) that attaches a session `Cookie`.
|
|
58
|
+
- `WorkSummary.summary` is only populated when AO3 renders the work summary on the loaded page (one-shots or chapter 1).
|
|
59
|
+
- Raises [`InvalidIDError`](#invalididerror) if `workId` is not a valid archive ID, [`ArchivePageRequestError`](#archivepagerequesterror) on non-2xx HTTP responses, or `ValueError` if an unrecognized rating/category/warning is encountered.
|
|
60
|
+
|
|
61
|
+
#### `getWorkContent(workId: str | int, *, chapterId: str | int | None = None, fullWork: bool = False) -> WorkWithText | LockedWorkSummary`
|
|
62
|
+
Fetch work metadata together with one chapter or full-work chapter HTML and author's notes.
|
|
63
|
+
|
|
64
|
+
- Set `fullWork=True` to load every chapter of a multi-chapter work in a single HTTP request (`?view_full_work=true`).
|
|
65
|
+
- When `fullWork=False`, fetches either the work root (chapter 1 / one-shot) or the specific `chapterId`. If a requested `chapterId` does not match the chapter AO3 actually served, raises `ValueError`.
|
|
66
|
+
- Removes screen-reader `.landmark` headings (e.g. `"Work Text"`, `"Chapter Text"`) from returned chapter HTML.
|
|
67
|
+
- Returns `LockedWorkSummary` if the work is restricted.
|
|
68
|
+
- Raises [`InvalidIDError`](#invalididerror) for invalid `workId` or `chapterId`, [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors, or `ValueError` if no content body is present on the page.
|
|
69
|
+
|
|
70
|
+
#### `getWorkWithChapters(workId: str | int) -> WorkWithChapters`
|
|
71
|
+
Fetch work title, authors, and chapter list (including per-chapter publication dates) from `/works/<id>/navigate`.
|
|
72
|
+
|
|
73
|
+
- Chapters using AO3's default `"Chapter N"` placeholder have `Chapter.title` normalized to `None`.
|
|
74
|
+
- Raises [`InvalidIDError`](#invalididerror) if `workId` is invalid or [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors.
|
|
75
|
+
|
|
76
|
+
#### `parseWorkBlurb(blurbHtml: str) -> WorkBlurbSummary`
|
|
77
|
+
Parse one work blurb HTML fragment (`<li class="work blurb">`) from a series or user works listing page.
|
|
78
|
+
|
|
79
|
+
- Converts AO3 blurb dates like `"01 Jan 2026"` into ISO `"2026-01-01"`.
|
|
80
|
+
- `WorkBlurbSummary.adult` is always `False` because listing blurbs do not include the adult-gate indicator.
|
|
81
|
+
- Raises `ValueError` if the blurb is missing its title, URL, author, or has an unrecognized date format.
|
|
82
|
+
|
|
83
|
+
<a id="works-data-classes"></a>
|
|
84
|
+
### Data Classes
|
|
85
|
+
|
|
86
|
+
All data classes use `@dataclass(slots=True)`.
|
|
87
|
+
|
|
88
|
+
#### `WorkSummary`
|
|
89
|
+
Full metadata for a publicly accessible work.
|
|
90
|
+
|
|
91
|
+
- `id: ArchiveId`
|
|
92
|
+
- `source: Literal["website"]`
|
|
93
|
+
- `host: str`
|
|
94
|
+
- `url: str`
|
|
95
|
+
- `shortUrl: str | None`
|
|
96
|
+
- `title: str`
|
|
97
|
+
- `category: tuple[WorkCategory, ...] | None`
|
|
98
|
+
- `publishedAt: str`
|
|
99
|
+
- `updatedAt: str | None`
|
|
100
|
+
- `summary: str | None` — inner HTML fragment, or `None` if absent
|
|
101
|
+
- `rating: WorkRatings`
|
|
102
|
+
- `adult: bool` — `True` for `Mature`, `Explicit`, or `Not Rated`
|
|
103
|
+
- `fandoms: tuple[str, ...]`
|
|
104
|
+
- `tags: WorkTags`
|
|
105
|
+
- `authors: tuple[Author, ...]`
|
|
106
|
+
- `language: str`
|
|
107
|
+
- `words: int`
|
|
108
|
+
- `chapters: WorkChaptersCount`
|
|
109
|
+
- `series: tuple[BasicSeries, ...]`
|
|
110
|
+
- `complete: bool` — `True` when `chapters.total is not None` and `chapters.total == chapters.published`
|
|
111
|
+
- `stats: WorkStats`
|
|
112
|
+
- `locked: Literal[False] = False`
|
|
113
|
+
|
|
114
|
+
#### `WorkWithText`
|
|
115
|
+
A work's full metadata together with its HTML text and notes. Has all the same fields as [`WorkSummary`](#worksummary) plus:
|
|
116
|
+
|
|
117
|
+
- `content: WorkText`
|
|
118
|
+
- `locked: Literal[False] = False`
|
|
119
|
+
|
|
120
|
+
#### `LockedWorkSummary`
|
|
121
|
+
Summary returned by `getWork` / `getWorkContent` when a work is restricted to logged-in users.
|
|
122
|
+
|
|
123
|
+
- `id: ArchiveId`
|
|
124
|
+
- `source: Literal["website"]`
|
|
125
|
+
- `host: str`
|
|
126
|
+
- `url: str`
|
|
127
|
+
- `shortUrl: str | None`
|
|
128
|
+
- `locked: Literal[True] = True`
|
|
129
|
+
|
|
130
|
+
#### `WorkWithChapters`
|
|
131
|
+
Work title, authors, and chapter list returned by [`getWorkWithChapters`](#getworkwithchaptersworkid-str--int---workwithchapters).
|
|
132
|
+
|
|
133
|
+
- `title: str`
|
|
134
|
+
- `authors: tuple[Author, ...]`
|
|
135
|
+
- `workId: ArchiveId`
|
|
136
|
+
- `chapters: tuple[Chapter, ...]`
|
|
137
|
+
|
|
138
|
+
#### `WorkBlurbSummary`
|
|
139
|
+
A work as rendered in AO3 listing pages (series, user works).
|
|
140
|
+
|
|
141
|
+
- `id: ArchiveId`
|
|
142
|
+
- `source: Literal["website"]`
|
|
143
|
+
- `host: str`
|
|
144
|
+
- `url: str`
|
|
145
|
+
- `shortUrl: str | None`
|
|
146
|
+
- `title: str`
|
|
147
|
+
- `updatedAt: str | None`
|
|
148
|
+
- `summary: str | None` — inner HTML fragment
|
|
149
|
+
- `adult: bool`
|
|
150
|
+
- `fandoms: tuple[str, ...]`
|
|
151
|
+
- `tags: WorkBlurbTags`
|
|
152
|
+
- `authors: tuple[Author, ...]`
|
|
153
|
+
- `language: str`
|
|
154
|
+
- `words: int`
|
|
155
|
+
- `chapters: WorkChaptersCount`
|
|
156
|
+
- `complete: bool`
|
|
157
|
+
- `stats: WorkBlurbStats`
|
|
158
|
+
|
|
159
|
+
#### `WorkText`
|
|
160
|
+
Work-level author's notes and chapter contents.
|
|
161
|
+
|
|
162
|
+
- `notes: ContentNotes`
|
|
163
|
+
- `chapters: tuple[ChapterText, ...]`
|
|
164
|
+
|
|
165
|
+
#### `ChapterText`
|
|
166
|
+
A single chapter's HTML content, summary, and notes.
|
|
167
|
+
|
|
168
|
+
- `id: ArchiveId | None` — `None` for one-shots or EPUB-parsed chapters
|
|
169
|
+
- `url: str | None` — `None` for EPUB-parsed chapters
|
|
170
|
+
- `shortUrl: str | None`
|
|
171
|
+
- `index: int` — 1-based chapter number
|
|
172
|
+
- `title: str | None` — custom chapter title, or `None` if untitled / default `"Chapter N"`
|
|
173
|
+
- `summary: str | None` — inner HTML fragment
|
|
174
|
+
- `notes: ContentNotes`
|
|
175
|
+
- `html: str` — chapter body HTML
|
|
176
|
+
|
|
177
|
+
#### `Chapter`
|
|
178
|
+
Chapter index entry from `/works/<id>/navigate`.
|
|
179
|
+
|
|
180
|
+
- `id: ArchiveId`
|
|
181
|
+
- `workId: ArchiveId`
|
|
182
|
+
- `index: int` — 1-based chapter number
|
|
183
|
+
- `title: str | None`
|
|
184
|
+
- `publishedAt: str` — ISO `"YYYY-MM-DD"` date string (or `""` if missing)
|
|
185
|
+
- `url: str`
|
|
186
|
+
- `shortUrl: str | None`
|
|
187
|
+
|
|
188
|
+
#### `ContentNotes`
|
|
189
|
+
Start and end author's notes HTML fragments.
|
|
190
|
+
|
|
191
|
+
- `start: str | None = None`
|
|
192
|
+
- `end: str | None = None`
|
|
193
|
+
|
|
194
|
+
#### `WorkTags`
|
|
195
|
+
Structured tag lists on a work page or EPUB download.
|
|
196
|
+
|
|
197
|
+
- `warnings: tuple[WorkWarnings, ...]`
|
|
198
|
+
- `characters: tuple[str, ...]`
|
|
199
|
+
- `relationships: tuple[str, ...]`
|
|
200
|
+
- `additional: tuple[str, ...]`
|
|
201
|
+
|
|
202
|
+
#### `WorkBlurbTags`
|
|
203
|
+
Tag lists visible on a work blurb card.
|
|
204
|
+
|
|
205
|
+
- `characters: tuple[str, ...]`
|
|
206
|
+
- `relationships: tuple[str, ...]`
|
|
207
|
+
- `additional: tuple[str, ...]`
|
|
208
|
+
|
|
209
|
+
#### `WorkChaptersCount`
|
|
210
|
+
Published and planned total chapter counts (`total` is `None` when displayed as `?`).
|
|
211
|
+
|
|
212
|
+
- `published: int`
|
|
213
|
+
- `total: int | None`
|
|
214
|
+
|
|
215
|
+
#### `WorkStats`
|
|
216
|
+
Engagement statistics on a full work page.
|
|
217
|
+
|
|
218
|
+
- `bookmarks: int`
|
|
219
|
+
- `comments: int`
|
|
220
|
+
- `kudos: int`
|
|
221
|
+
- `hits: int`
|
|
222
|
+
|
|
223
|
+
#### `WorkBlurbStats`
|
|
224
|
+
Engagement statistics visible on a listing blurb.
|
|
225
|
+
|
|
226
|
+
- `bookmarks: int`
|
|
227
|
+
- `kudos: int`
|
|
228
|
+
- `hits: int`
|
|
229
|
+
|
|
230
|
+
#### `Author`
|
|
231
|
+
An author or pseud credit on a work or series.
|
|
232
|
+
|
|
233
|
+
- `username: str`
|
|
234
|
+
- `pseud: str`
|
|
235
|
+
- `anonymous: bool = False`
|
|
236
|
+
|
|
237
|
+
#### `BasicSeries`
|
|
238
|
+
Series membership reference shown on a work page.
|
|
239
|
+
|
|
240
|
+
- `id: ArchiveId`
|
|
241
|
+
- `url: str`
|
|
242
|
+
- `shortUrl: str | None`
|
|
243
|
+
- `index: int` — 1-based position in the series (or `-1` if not parsed)
|
|
244
|
+
- `name: str`
|
|
245
|
+
|
|
246
|
+
<a id="works-enums"></a>
|
|
247
|
+
### Enums
|
|
248
|
+
|
|
249
|
+
All enums inherit from `enum.StrEnum`.
|
|
250
|
+
|
|
251
|
+
#### `WorkRatings`
|
|
252
|
+
Canonical AO3 rating labels:
|
|
253
|
+
|
|
254
|
+
- `NOT_RATED = "Not Rated"`
|
|
255
|
+
- `GENERAL_AUDIENCES = "General Audiences"`
|
|
256
|
+
- `TEEN_AND_UP_AUDIENCES = "Teen And Up Audiences"`
|
|
257
|
+
- `MATURE = "Mature"`
|
|
258
|
+
- `EXPLICIT = "Explicit"`
|
|
259
|
+
|
|
260
|
+
#### `WorkCategory`
|
|
261
|
+
Canonical AO3 relationship category labels:
|
|
262
|
+
|
|
263
|
+
- `FF = "F/F"`
|
|
264
|
+
- `FM = "F/M"`
|
|
265
|
+
- `GEN = "Gen"`
|
|
266
|
+
- `MM = "M/M"`
|
|
267
|
+
- `MULTI = "Multi"`
|
|
268
|
+
- `OTHER = "Other"`
|
|
269
|
+
|
|
270
|
+
#### `WorkWarnings`
|
|
271
|
+
Canonical AO3 archive warning labels:
|
|
272
|
+
|
|
273
|
+
- `GRAPHIC_VIOLENCE = "Graphic Depictions Of Violence"`
|
|
274
|
+
- `MAJOR_CHARACTER_DEATH = "Major Character Death"`
|
|
275
|
+
- `NO_WARNINGS_APPLY = "No Archive Warnings Apply"`
|
|
276
|
+
- `NONCON = "Rape/Non-Con"`
|
|
277
|
+
- `UNDERAGE = "Underage Sex"`
|
|
278
|
+
- `CHOOSE_NOT_TO_WARN = "Creator Chose Not To Use Archive Warnings"`
|
|
279
|
+
|
|
280
|
+
#### `WorkWarningStatus`
|
|
281
|
+
High-level archive warning status enum:
|
|
282
|
+
|
|
283
|
+
- `NO_WARNINGS_APPLY = "Author indicated no warnings apply"`
|
|
284
|
+
- `CHOOSE_NOT_TO_WARN = "Author chose not to warn"`
|
|
285
|
+
- `EXTERNAL = "External work"`
|
|
286
|
+
- `HAS_WARNING = "Work has one or more warning"`
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## EPUB Downloads
|
|
291
|
+
|
|
292
|
+
<a id="epub-functions"></a>
|
|
293
|
+
### Functions
|
|
294
|
+
|
|
295
|
+
#### `parseEpubWork(epub: bytes, *, filePath: str | None = None) -> DownloadedWork`
|
|
296
|
+
Parse a downloaded AO3 `.epub` file (raw bytes) into a [`DownloadedWork`](#downloadedwork) completely offline.
|
|
297
|
+
|
|
298
|
+
- Embeds relative `<img>` assets from inside the EPUB archive as base64 `data:` URIs.
|
|
299
|
+
- Sanitizes chapter and note HTML: strips attributes/classes/styles, removes `.endnote-link` anchors, unwraps links whose `href` is not `http:`, `https:`, or `mailto:`, and retains only `https:` `<iframe>` sources.
|
|
300
|
+
- Maps the EPUB preface warning `"Choose Not To Use Archive Warnings"` to `WorkWarnings.CHOOSE_NOT_TO_WARN`.
|
|
301
|
+
- Resolves language from the preface `Language:` metadata or falls back to `<dc:language>` looked up in [`AO3_LANGUAGES`](#ao3_languages).
|
|
302
|
+
- Uses the currently configured archive base URL (see [`setArchiveBaseUrl`](#setarchivebaseurlurl-str---none)) when building the canonical `url`/`host`/`shortUrl`.
|
|
303
|
+
- Raises `ValueError` if `epub` is not a valid ZIP/EPUB, does not look like an AO3 download, or is missing required metadata/chapters.
|
|
304
|
+
|
|
305
|
+
<a id="epub-data-classes"></a>
|
|
306
|
+
### Data Classes
|
|
307
|
+
|
|
308
|
+
#### `DownloadedWork`
|
|
309
|
+
Metadata and full text parsed from a downloaded AO3 EPUB file.
|
|
310
|
+
|
|
311
|
+
- `id: ArchiveId`
|
|
312
|
+
- `source: Literal["file"]`
|
|
313
|
+
- `file: DownloadedFileInfo`
|
|
314
|
+
- `host: str`
|
|
315
|
+
- `url: str`
|
|
316
|
+
- `shortUrl: str | None`
|
|
317
|
+
- `title: str`
|
|
318
|
+
- `authors: tuple[Author, ...]`
|
|
319
|
+
- `rating: WorkRatings`
|
|
320
|
+
- `adult: bool`
|
|
321
|
+
- `category: tuple[WorkCategory, ...] | None`
|
|
322
|
+
- `fandoms: tuple[str, ...]`
|
|
323
|
+
- `tags: WorkTags`
|
|
324
|
+
- `language: str`
|
|
325
|
+
- `summary: str | None`
|
|
326
|
+
- `publishedAt: str`
|
|
327
|
+
- `updatedAt: str | None`
|
|
328
|
+
- `words: int`
|
|
329
|
+
- `chapters: WorkChaptersCount`
|
|
330
|
+
- `complete: bool`
|
|
331
|
+
- `stats: None` — EPUB downloads do not contain engagement stats
|
|
332
|
+
- `content: WorkText`
|
|
333
|
+
|
|
334
|
+
#### `DownloadedFileInfo`
|
|
335
|
+
Source format and optional file path for a downloaded work.
|
|
336
|
+
|
|
337
|
+
- `format: Literal["epub"]`
|
|
338
|
+
- `filePath: str | None`
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## Series
|
|
343
|
+
|
|
344
|
+
<a id="series-functions"></a>
|
|
345
|
+
### Functions
|
|
346
|
+
|
|
347
|
+
#### `getSeries(seriesId: str | int, *, page: int | None = None) -> Series`
|
|
348
|
+
Fetch metadata and one page of work blurbs for an archive series.
|
|
349
|
+
|
|
350
|
+
- `page` is 1-based (`None` fetches page 1). Compatible with [`getAllPages`](#getallpagesr-pagedresultgetter-callable-r-kwargs-any---iterabler) and [`collectAllPages`](#collectallpagesr-pagedresultgetter-callable-r-kwargs-any---listr).
|
|
351
|
+
- `Series.pages.total` is `0` when the series contains no visible works, otherwise `>= 1`.
|
|
352
|
+
- Raises [`InvalidIDError`](#invalididerror) if `seriesId` is invalid or [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors.
|
|
353
|
+
|
|
354
|
+
<a id="series-data-classes"></a>
|
|
355
|
+
### Data Classes
|
|
356
|
+
|
|
357
|
+
#### `Series`
|
|
358
|
+
Metadata and one page of works for an archive series.
|
|
359
|
+
|
|
360
|
+
- `id: ArchiveId`
|
|
361
|
+
- `url: str`
|
|
362
|
+
- `shortUrl: str | None`
|
|
363
|
+
- `name: str`
|
|
364
|
+
- `startedAt: str`
|
|
365
|
+
- `updatedAt: str`
|
|
366
|
+
- `authors: tuple[Author, ...]`
|
|
367
|
+
- `description: str | None` — inner HTML fragment
|
|
368
|
+
- `notes: str | None` — inner HTML fragment
|
|
369
|
+
- `words: int`
|
|
370
|
+
- `bookmarks: int`
|
|
371
|
+
- `complete: bool`
|
|
372
|
+
- `workCount: int`
|
|
373
|
+
- `pages: PageInfo`
|
|
374
|
+
- `works: tuple[WorkBlurbSummary, ...]`
|
|
375
|
+
|
|
376
|
+
---
|
|
377
|
+
|
|
378
|
+
## Users
|
|
379
|
+
|
|
380
|
+
<a id="users-functions"></a>
|
|
381
|
+
### Functions
|
|
382
|
+
|
|
383
|
+
#### `getUser(username: str) -> User`
|
|
384
|
+
Fetch public profile details for an archive username (`/users/<username>/profile`).
|
|
385
|
+
|
|
386
|
+
- `User.pseuds` is a comma-separated string of all pseuds listed on the profile.
|
|
387
|
+
- Raises [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors or `ValueError` if expected profile markup (such as the profile icon) is missing.
|
|
388
|
+
|
|
389
|
+
#### `getUserWorks(username: str, *, pseud: str | None = None, page: int | None = None, sortColumn: UserWorksSortColumn | None = None, sortDirection: SortDirection | None = None) -> UserWorks`
|
|
390
|
+
Fetch one page of a user's public works (optionally scoped to a specific `pseud`).
|
|
391
|
+
|
|
392
|
+
- Defaults `sortColumn` to `"updated_at"`. When `sortDirection` is `None`, `filters.sortDirection` resolves to `"asc"` for `"authors"` and `"title"`, and `"desc"` for all other columns (matching AO3's default sort order).
|
|
393
|
+
- Compatible with [`getAllPages`](#getallpagesr-pagedresultgetter-callable-r-kwargs-any---iterabler) and [`collectAllPages`](#collectallpagesr-pagedresultgetter-callable-r-kwargs-any---listr).
|
|
394
|
+
- Raises [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors or `ValueError` if the public works index is missing.
|
|
395
|
+
|
|
396
|
+
<a id="users-data-classes"></a>
|
|
397
|
+
### Data Classes
|
|
398
|
+
|
|
399
|
+
#### `User`
|
|
400
|
+
Public profile information for an archive user.
|
|
401
|
+
|
|
402
|
+
- `id: ArchiveId`
|
|
403
|
+
- `username: str`
|
|
404
|
+
- `pseuds: str`
|
|
405
|
+
- `url: str`
|
|
406
|
+
- `shortUrl: str | None`
|
|
407
|
+
- `icon: str` — profile icon image URL
|
|
408
|
+
- `header: str | None`
|
|
409
|
+
- `joined: str` — ISO `"YYYY-MM-DD"` join date
|
|
410
|
+
- `location: str | None`
|
|
411
|
+
- `birthday: str | None`
|
|
412
|
+
- `works: int`
|
|
413
|
+
- `series: int`
|
|
414
|
+
- `bookmarks: int`
|
|
415
|
+
- `collections: int`
|
|
416
|
+
- `gifts: int`
|
|
417
|
+
- `bioHtml: str | None` — inner HTML fragment
|
|
418
|
+
|
|
419
|
+
#### `UserWorks`
|
|
420
|
+
One page of a user's public works.
|
|
421
|
+
|
|
422
|
+
- `filters: UserWorksFilters`
|
|
423
|
+
- `totalResults: int`
|
|
424
|
+
- `pages: PageInfo`
|
|
425
|
+
- `works: tuple[WorkBlurbSummary, ...]`
|
|
426
|
+
|
|
427
|
+
#### `UserWorksFilters`
|
|
428
|
+
Normalized filter parameters for a user's public works listing.
|
|
429
|
+
|
|
430
|
+
- `sortColumn: UserWorksSortColumn = "updated_at"`
|
|
431
|
+
- `sortDirection: SortDirection = "desc"`
|
|
432
|
+
- `page: int = 1`
|
|
433
|
+
|
|
434
|
+
<a id="users-type-aliases"></a>
|
|
435
|
+
### Type Aliases
|
|
436
|
+
|
|
437
|
+
#### `UserWorksSortColumn`
|
|
438
|
+
```python
|
|
439
|
+
Literal[
|
|
440
|
+
"authors",
|
|
441
|
+
"title",
|
|
442
|
+
"created_at",
|
|
443
|
+
"updated_at",
|
|
444
|
+
"word_count",
|
|
445
|
+
"hits",
|
|
446
|
+
"kudos_count",
|
|
447
|
+
"comments_count",
|
|
448
|
+
"bookmarks_count",
|
|
449
|
+
]
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
## Tags & Tag Search
|
|
455
|
+
|
|
456
|
+
<a id="tags-functions"></a>
|
|
457
|
+
### Functions
|
|
458
|
+
|
|
459
|
+
#### `getTag(tagName: str) -> Tag`
|
|
460
|
+
Fetch full wrangling status, parent/child/subtag hierarchy, and synonyms for an AO3 tag.
|
|
461
|
+
|
|
462
|
+
- Makes **two** HTTP requests: the tag profile page (`/tags/<tag>/`) and its works feed page (`/tags/<tag>/works`) to read the numeric tag ID from the RSS/Atom link (`Tag.id` is `None` if no feed link exists).
|
|
463
|
+
- Raises [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors or `ValueError` if the tag category or ID cannot be parsed.
|
|
464
|
+
|
|
465
|
+
#### `getTagNameById(tagId: str | int) -> str | None`
|
|
466
|
+
Resolve a canonical tag name from its numeric tag ID via `/tags/<tagId>/feed.atom`.
|
|
467
|
+
|
|
468
|
+
- Returns `None` if the Atom feed does not contain a matching `"AO3 works tagged '...'"` title.
|
|
469
|
+
- Raises [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors.
|
|
470
|
+
|
|
471
|
+
#### `searchTags(*, tagName: str | None = None, fandoms: Sequence[str] = (), tagType: TagSearchType = "any", wranglingStatus: TagWranglingStatus = "any", sortColumn: TagSearchSortColumn = "name", sortDirection: SortDirection = "asc", page: int = 1) -> TagSearchResultSummary`
|
|
472
|
+
Search AO3 tags with filters and pagination (`/tags/search`).
|
|
473
|
+
|
|
474
|
+
- Maps AO3's `"UnsortedTag"` result label to `"unsorted"`.
|
|
475
|
+
- Compatible with [`getAllPages`](#getallpagesr-pagedresultgetter-callable-r-kwargs-any---iterabler) and [`collectAllPages`](#collectallpagesr-pagedresultgetter-callable-r-kwargs-any---listr).
|
|
476
|
+
- Raises [`ArchivePageRequestError`](#archivepagerequesterror) on HTTP errors or `ValueError` on unexpected tag type markup.
|
|
477
|
+
|
|
478
|
+
<a id="tags-data-classes"></a>
|
|
479
|
+
### Data Classes
|
|
480
|
+
|
|
481
|
+
#### `Tag`
|
|
482
|
+
Detailed AO3 tag wrangling and hierarchy information.
|
|
483
|
+
|
|
484
|
+
- `name: str`
|
|
485
|
+
- `id: ArchiveId | None`
|
|
486
|
+
- `category: TagCategory`
|
|
487
|
+
- `canonical: bool`
|
|
488
|
+
- `common: bool`
|
|
489
|
+
- `canonicalName: str | None` — set to the displayed name if canonical, the merger target if synonymous, or `None` otherwise
|
|
490
|
+
- `parentTags: tuple[str, ...]`
|
|
491
|
+
- `childTags: tuple[ChildTag, ...]`
|
|
492
|
+
- `subTags: tuple[SubTag, ...]`
|
|
493
|
+
- `tagsWithSameMeaning: tuple[str, ...]`
|
|
494
|
+
|
|
495
|
+
#### `ChildTag`
|
|
496
|
+
A child tag listed under a parent tag page.
|
|
497
|
+
|
|
498
|
+
- `tagName: str`
|
|
499
|
+
- `category: TagCategory`
|
|
500
|
+
|
|
501
|
+
#### `SubTag`
|
|
502
|
+
A subtag in a tag's subtag hierarchy.
|
|
503
|
+
|
|
504
|
+
- `tagName: str`
|
|
505
|
+
- `parentSubTag: str | None` — `None` for top-level subtags, or the immediate parent subtag name for nested subtags
|
|
506
|
+
|
|
507
|
+
#### `TagSearchFilters`
|
|
508
|
+
Normalized filter parameters for AO3 tag search.
|
|
509
|
+
|
|
510
|
+
- `tagName: str | None = None`
|
|
511
|
+
- `fandoms: tuple[str, ...] = ()`
|
|
512
|
+
- `type: TagSearchType = "any"`
|
|
513
|
+
- `wranglingStatus: TagWranglingStatus = "any"`
|
|
514
|
+
- `sortColumn: TagSearchSortColumn = "name"`
|
|
515
|
+
- `sortDirection: SortDirection = "asc"`
|
|
516
|
+
- `page: int = 1`
|
|
517
|
+
|
|
518
|
+
#### `TagSearchResult`
|
|
519
|
+
A single tag row returned from AO3 tag search.
|
|
520
|
+
|
|
521
|
+
- `name: str`
|
|
522
|
+
- `type: TagSearchResultType`
|
|
523
|
+
- `canonical: bool`
|
|
524
|
+
- `worksCount: int`
|
|
525
|
+
|
|
526
|
+
#### `TagSearchResultSummary`
|
|
527
|
+
One page of AO3 tag search results.
|
|
528
|
+
|
|
529
|
+
- `filters: TagSearchFilters`
|
|
530
|
+
- `totalResults: int`
|
|
531
|
+
- `pages: PageInfo`
|
|
532
|
+
- `tags: tuple[TagSearchResult, ...]`
|
|
533
|
+
|
|
534
|
+
<a id="tags-type-aliases"></a>
|
|
535
|
+
### Type Aliases
|
|
536
|
+
|
|
537
|
+
#### `TagCategory`
|
|
538
|
+
```python
|
|
539
|
+
Literal["fandom", "character", "relationship", "archive warning", "additional tags"]
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### `TagSearchType`
|
|
543
|
+
```python
|
|
544
|
+
Literal["fandom", "character", "relationship", "freeform", "any"]
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
#### `TagSearchResultType`
|
|
548
|
+
```python
|
|
549
|
+
Literal["fandom", "character", "relationship", "freeform", "unsorted"]
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
#### `TagWranglingStatus`
|
|
553
|
+
```python
|
|
554
|
+
Literal[
|
|
555
|
+
"canonical",
|
|
556
|
+
"noncanonical",
|
|
557
|
+
"synonymous",
|
|
558
|
+
"canonical_or_synonymous",
|
|
559
|
+
"noncanonical_and_nonsynonymous",
|
|
560
|
+
"any",
|
|
561
|
+
]
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
#### `TagSearchSortColumn`
|
|
565
|
+
```python
|
|
566
|
+
Literal["name", "created_at", "works_count"]
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
---
|
|
570
|
+
|
|
571
|
+
## Pagination
|
|
572
|
+
|
|
573
|
+
<a id="pagination-functions"></a>
|
|
574
|
+
### Functions
|
|
575
|
+
|
|
576
|
+
#### `getAllPages[R: PagedResult](getter: Callable[..., R], **kwargs: Any) -> Iterable[R]`
|
|
577
|
+
Lazily yield every page of a paginated getter (`getSeries`, `getUserWorks`, `searchTags`), fetching one page per iteration.
|
|
578
|
+
|
|
579
|
+
- Always calls `getter(page=1, **kwargs)` first, then continues while `page <= result.pages.total` (so empty listings with `pages.total == 0` yield the single empty page 1 and stop).
|
|
580
|
+
- Forward any required getter parameters as keyword arguments (e.g. `getAllPages(ao3fetch.getSeries, seriesId=77)`).
|
|
581
|
+
|
|
582
|
+
#### `collectAllPages[R: PagedResult](getter: Callable[..., R], **kwargs: Any) -> list[R]`
|
|
583
|
+
Eagerly fetch every page from [`getAllPages`](#getallpagesr-pagedresultgetter-callable-r-kwargs-any---iterabler) and return them as a `list`.
|
|
584
|
+
|
|
585
|
+
<a id="pagination-protocol--data-class"></a>
|
|
586
|
+
### Protocol & Data Class
|
|
587
|
+
|
|
588
|
+
#### `PagedResult`
|
|
589
|
+
`Protocol` implemented by paginated response types (`Series`, `UserWorks`, `TagSearchResultSummary`):
|
|
590
|
+
|
|
591
|
+
- `pages: PageInfo`
|
|
592
|
+
|
|
593
|
+
#### `PageInfo`
|
|
594
|
+
Pagination metadata for multi-page archive responses.
|
|
595
|
+
|
|
596
|
+
- `total: int`
|
|
597
|
+
- `current: int`
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
601
|
+
## HTML Conversion
|
|
602
|
+
|
|
603
|
+
#### `toPlainText(html: str) -> str`
|
|
604
|
+
Convert AO3 work/note HTML into plain text matching AO3.js formatting.
|
|
605
|
+
|
|
606
|
+
- Drops `<img>`, `<script>`, and `<style>` elements.
|
|
607
|
+
- Separates block elements with blank lines, turns `<br>` into `\n`, `<hr>` into `---`, and prefixes `<ul><li>` items with `- `.
|
|
608
|
+
- Collapses runs of whitespace within lines and normalizes consecutive blank lines.
|
|
609
|
+
|
|
610
|
+
#### `toMarkdown(html: str) -> str`
|
|
611
|
+
Convert AO3 HTML content (prose, summaries, or author's notes) into clean CommonMark Markdown.
|
|
612
|
+
|
|
613
|
+
- Preserves `_italics_`, `**bold**` (keeping outer spaces/breaks outside emphasis markers), links, images, inline `` `code` ``, fenced `<pre>` blocks (with indentation and internal spaces intact), ATX headings (`<h1>`–`<h6>`), blockquotes (including multi-paragraph quotes), ordered/unordered lists (including continuation indentation), `<hr>` scene breaks (`---`), and safe inline HTML tags (`<u>`, `<s>`, `<del>`, `<sup>`, `<sub>`).
|
|
614
|
+
- Collapses HTML source whitespace (`[ \t\r\n]+`) in text nodes outside `<pre>`/`<code>` so source-wrapped HTML does not introduce spurious line breaks.
|
|
615
|
+
- Preserves `<br>` (including `<br />\n`) as a single Markdown hard line break (` \n`), turns consecutive `<br>` tags into clean paragraph breaks without dangling trailing spaces, and omits trailing two-space breaks immediately before setext heading underlines (`---` / `===`) so underlined Markdown headings round-trip cleanly.
|
|
616
|
+
|
|
617
|
+
---
|
|
618
|
+
|
|
619
|
+
## URLs, Archive Base URL & IDs
|
|
620
|
+
|
|
621
|
+
<a id="urls-constants--submodule"></a>
|
|
622
|
+
### Constants & Submodule
|
|
623
|
+
|
|
624
|
+
<a id="default_base_url"></a>
|
|
625
|
+
#### `DEFAULT_BASE_URL: Final[str]`
|
|
626
|
+
Default archive base URL read from the `ARCHIVE_BASE_URL` environment variable at import time, defaulting to `"https://archiveofourown.org"`.
|
|
627
|
+
|
|
628
|
+
#### `urls`
|
|
629
|
+
The `ao3fetch.urls` submodule itself (also re-exported at top level for compatibility with code referencing `ao3fetch.urls.*`).
|
|
630
|
+
|
|
631
|
+
<a id="archive-base-url-configuration"></a>
|
|
632
|
+
### Archive Base URL Configuration
|
|
633
|
+
|
|
634
|
+
Base URL state is process-global (not thread-local). If temporarily targeting another OTW/AO3-compatible archive, restore the previous URL in a `try ... finally` block.
|
|
635
|
+
|
|
636
|
+
#### `setArchiveBaseUrl(url: str) -> None`
|
|
637
|
+
Set the active archive base URL used by all URL builders and scrapers (e.g. `"https://superlove.sayitditto.net"`).
|
|
638
|
+
|
|
639
|
+
#### `getArchiveBaseUrl() -> str`
|
|
640
|
+
Return the currently active archive base URL.
|
|
641
|
+
|
|
642
|
+
#### `resetArchiveBaseUrl() -> None`
|
|
643
|
+
Reset the active archive base URL back to [`DEFAULT_BASE_URL`](#default_base_url).
|
|
644
|
+
|
|
645
|
+
<a id="url-builders"></a>
|
|
646
|
+
### URL Builders
|
|
647
|
+
|
|
648
|
+
#### `getWorkUrl(workId: str | int, *, chapterId: str | int | None = None, collectionName: str | None = None, fullWork: bool = False) -> str`
|
|
649
|
+
Construct the canonical URL for a work or specific chapter (optionally under a collection or with `?view_full_work=true`). Raises [`InvalidIDError`](#invalididerror) for invalid IDs.
|
|
650
|
+
|
|
651
|
+
#### `getWorkIndexUrl(workId: str | int) -> str`
|
|
652
|
+
Construct the `/works/<workId>/navigate` chapter index URL. Raises [`InvalidIDError`](#invalididerror) if `workId` is invalid.
|
|
653
|
+
|
|
654
|
+
#### `getSeriesUrl(seriesId: str | int, *, page: int | None = None) -> str`
|
|
655
|
+
Construct a series URL (with optional `?page=<n>`). Raises [`InvalidIDError`](#invalididerror) if `seriesId` is invalid.
|
|
656
|
+
|
|
657
|
+
#### `getAsShortUrl(url: str) -> str | None`
|
|
658
|
+
Convert an `archiveofourown.org` URL to `ao3.org`, preserving path/query/port. Returns `None` for any other host.
|
|
659
|
+
|
|
660
|
+
#### `getDownloadUrls(workId: ArchiveId, title: str, *, publishedAt: str, updatedAt: str | None = None) -> dict[str, str]`
|
|
661
|
+
Build AO3 download URLs keyed by format (`"azw3"`, `"epub"`, `"mobi"`, `"html"`, `"pdf"`).
|
|
662
|
+
|
|
663
|
+
- Uses `updatedAt` (falling back to `publishedAt`) as an ISO-8601 date/datetime to compute the `updated_at` millisecond timestamp query parameter; naive timestamps are treated as UTC.
|
|
664
|
+
|
|
665
|
+
#### `getUserProfileUrl(username: str) -> str`
|
|
666
|
+
Construct a user profile URL (`/users/<username>/profile`).
|
|
667
|
+
|
|
668
|
+
#### `getUserWorksUrl(username: str, *, pseud: str | None = None, page: int | None = None, sortColumn: UserWorksSortColumn | None = None, sortDirection: SortDirection | None = None) -> str`
|
|
669
|
+
Construct a user's public works listing URL (optionally scoped to `pseud` with pagination and sort query parameters).
|
|
670
|
+
|
|
671
|
+
#### `getTagUrl(tagName: str) -> str`
|
|
672
|
+
Construct a tag URL using AO3's special character token encoding (`/`, `&`, `.`, `#`, `?` → `*s*`, `*a*`, `*d*`, `*h*`, `*q*`) followed by percent-encoding.
|
|
673
|
+
|
|
674
|
+
#### `getTagWorksFeedUrl(tagName: str) -> str`
|
|
675
|
+
Construct the recent works listing URL for a tag (`/tags/<encoded>/works`).
|
|
676
|
+
|
|
677
|
+
#### `getTagWorksFeedAtomUrl(tagId: str | int) -> str`
|
|
678
|
+
Construct the Atom feed URL for a numeric tag ID (`/tags/<tagId>/feed.atom`).
|
|
679
|
+
|
|
680
|
+
#### `getSearchUrlFromTagFilters(filters: TagSearchFilters) -> str`
|
|
681
|
+
Construct an AO3 `/tags/search` URL with query parameters derived from [`TagSearchFilters`](#tagsearchfilters).
|
|
682
|
+
|
|
683
|
+
<a id="url-parsers--data-classes"></a>
|
|
684
|
+
### URL Parsers & Data Classes
|
|
685
|
+
|
|
686
|
+
#### `getWorkDetailsFromUrl(url: str) -> ParsedWorkUrl`
|
|
687
|
+
Extract work ID, optional chapter ID, optional collection name, and origin `baseUrl` from an archive work URL (relative or absolute).
|
|
688
|
+
|
|
689
|
+
- Normalizes `ao3.org` short URLs so `ParsedWorkUrl.baseUrl` is `"https://archiveofourown.org"`.
|
|
690
|
+
- Raises `ValueError` if no `/works/<id>` segment is found, or [`InvalidIDError`](#invalididerror) if matched IDs are invalid.
|
|
691
|
+
|
|
692
|
+
#### `getUserDetailsFromUrl(url: str) -> ParsedUserUrl`
|
|
693
|
+
Extract `username` and optional percent-decoded `pseud` from a `/users/<username>` or `/users/<username>/pseuds/<pseud>` URL. Raises `ValueError` on invalid user URLs.
|
|
694
|
+
|
|
695
|
+
#### `getAuthorFromUrl(url: str) -> Author`
|
|
696
|
+
Parse an AO3 author link (`/users/<username>/pseuds/<pseud>`) into an [`Author`](#author). Raises `ValueError` if the URL does not include a `/pseuds/...` segment.
|
|
697
|
+
|
|
698
|
+
#### `ParsedWorkUrl`
|
|
699
|
+
Work URL components returned by [`getWorkDetailsFromUrl`](#getworkdetailsfromurlurl-str---parsedworkurl).
|
|
700
|
+
|
|
701
|
+
- `workId: ArchiveId`
|
|
702
|
+
- `chapterId: ArchiveId | None = None`
|
|
703
|
+
- `collectionName: str | None = None`
|
|
704
|
+
- `baseUrl: str | None = None`
|
|
705
|
+
|
|
706
|
+
#### `ParsedUserUrl`
|
|
707
|
+
User URL components returned by [`getUserDetailsFromUrl`](#getuserdetailsfromurlurl-str---parseduserurl).
|
|
708
|
+
|
|
709
|
+
- `username: str`
|
|
710
|
+
- `pseud: str | None = None`
|
|
711
|
+
|
|
712
|
+
<a id="archive-id-helpers--types"></a>
|
|
713
|
+
### Archive ID Helpers & Types
|
|
714
|
+
|
|
715
|
+
#### `ArchiveId`
|
|
716
|
+
```python
|
|
717
|
+
type ArchiveId = int
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
#### `isValidArchiveId(rawId: str | int | None) -> bool`
|
|
721
|
+
Return `True` if `rawId` is a non-negative `int` (excluding `bool`) or an ASCII base-10 digit string (`^\d+$`).
|
|
722
|
+
|
|
723
|
+
#### `parseArchiveId(rawId: str | int) -> ArchiveId`
|
|
724
|
+
Convert a validated archive ID (`int` or digit `str`) into an `ArchiveId` (`int`).
|
|
725
|
+
|
|
726
|
+
---
|
|
727
|
+
|
|
728
|
+
## HTTP Fetcher, Retry & Caching
|
|
729
|
+
|
|
730
|
+
<a id="fetcher-configuration"></a>
|
|
731
|
+
### Fetcher Configuration
|
|
732
|
+
|
|
733
|
+
Fetcher state is process-global (not thread-local). Restore the default or previous fetcher when overriding temporarily (e.g. in tests or CLI wrappers).
|
|
734
|
+
|
|
735
|
+
#### `defaultFetch(url: str, headers: Mapping[str, str] | None = None) -> FetchResponse`
|
|
736
|
+
Default HTTP `GET` fetcher built on Python's stdlib `urllib.request`.
|
|
737
|
+
|
|
738
|
+
- Accepts only `https://` and `http://` URLs (raises `ValueError` for other schemes).
|
|
739
|
+
- Sends a default `ao3fetch` `User-Agent` header (overridable via `headers`) and uses a 30-second timeout.
|
|
740
|
+
- Returns `4xx`/`5xx` HTTP errors as `FetchResponse` objects (with `response.ok == False`) rather than raising `urllib.error.HTTPError`. Network/transport failures raise `OSError`.
|
|
741
|
+
|
|
742
|
+
#### `getFetcher() -> Fetcher`
|
|
743
|
+
Return the active fetcher, defaulting to [`defaultFetch`](#defaultfetchurl-str-headers-mappingstr-str--none--none---fetchresponse).
|
|
744
|
+
|
|
745
|
+
<a id="setfetcher"></a>
|
|
746
|
+
#### `setFetcher(newFetcher: Fetcher) -> None`
|
|
747
|
+
Override the HTTP fetcher used by all archive page loaders.
|
|
748
|
+
|
|
749
|
+
#### `resetFetcher() -> None`
|
|
750
|
+
Restore [`defaultFetch`](#defaultfetchurl-str-headers-mappingstr-str--none--none---fetchresponse) as the active fetcher.
|
|
751
|
+
|
|
752
|
+
<a id="middleware--helpers"></a>
|
|
753
|
+
### Middleware & Helpers
|
|
754
|
+
|
|
755
|
+
#### `withRetry(baseFetcher: Fetcher, *, maxRetries: int = 3, maxRateLimitRetries: int = 10, baseDelaySeconds: float = 1.0, maxDelaySeconds: float = 180.0, maxRetryAfterSeconds: float | None = None, onRetry: OnRetryCallback | None = None, sleepFunc: Callable[[float], None] = time.sleep) -> Fetcher`
|
|
756
|
+
Wrap a fetcher to automatically retry HTTP `429` rate limits, `5xx` server errors, and `OSError` network failures.
|
|
757
|
+
|
|
758
|
+
- Tracks rate-limit retries (`maxRateLimitRetries`) and server/network error retries (`maxRetries`) independently.
|
|
759
|
+
- Uses the server's `Retry-After` header (via [`getRetryAfterSeconds`](#getretryaftersecondsheader-str--none--now-float--none--none---float--none)) when present, aborting retries immediately if `maxRetryAfterSeconds` is set and exceeded. Otherwise uses exponential backoff with 50%–100% random jitter capped at `maxDelaySeconds`.
|
|
760
|
+
- Calls `onRetry(RetryEvent(...))` before sleeping on each retry attempt.
|
|
761
|
+
- When retries are exhausted, re-raises the final `OSError` (for network errors) or returns the final non-2xx `FetchResponse` (for HTTP errors).
|
|
762
|
+
|
|
763
|
+
#### `withCache(baseFetcher: Fetcher, *, ttlSeconds: float = 300.0, maxEntries: int = 500, clock: Callable[[], float] = time.monotonic) -> Fetcher`
|
|
764
|
+
Wrap a fetcher with an in-memory LRU+TTL cache.
|
|
765
|
+
|
|
766
|
+
- Caches only successful (`response.ok`, i.e. `2xx`) responses, keyed by `(url, sorted_lowercase_nonempty_headers)`.
|
|
767
|
+
- Compose as `withCache(withRetry(defaultFetch))` so cached responses skip retry overhead and transient failures are retried before caching.
|
|
768
|
+
|
|
769
|
+
#### `getRetryAfterSeconds(header: str | None, *, now: float | None = None) -> float | None`
|
|
770
|
+
Parse an HTTP `Retry-After` header value (either non-negative numeric seconds or an RFC 7231 HTTP-date) into delay seconds (clamped to `>= 0.0`). Returns `None` if empty or unparseable.
|
|
771
|
+
|
|
772
|
+
<a id="fetcher-classes--type-aliases"></a>
|
|
773
|
+
### Classes & Type Aliases
|
|
774
|
+
|
|
775
|
+
#### `FetchResponse`
|
|
776
|
+
Lightweight HTTP response returned by fetchers.
|
|
777
|
+
|
|
778
|
+
- `status: int`
|
|
779
|
+
- `headers: dict[str, str]`
|
|
780
|
+
- `body: bytes`
|
|
781
|
+
- `url: str`
|
|
782
|
+
- `ok -> bool` *(property)*: `True` for `200 <= status < 300`.
|
|
783
|
+
- `text -> str` *(property)*: `body` decoded as UTF-8 (`errors="replace"`).
|
|
784
|
+
- `header(name: str) -> str | None`: Case-insensitive response header lookup.
|
|
785
|
+
|
|
786
|
+
#### `RetryEvent`
|
|
787
|
+
Details passed to `onRetry` callbacks before sleeping on a retry attempt.
|
|
788
|
+
|
|
789
|
+
- `attempt: int` — cumulative retry count (`rateLimitCount + errorCount`)
|
|
790
|
+
- `delaySeconds: float`
|
|
791
|
+
- `response: FetchResponse | None = None`
|
|
792
|
+
- `error: Exception | None = None`
|
|
793
|
+
|
|
794
|
+
#### `Fetcher`
|
|
795
|
+
```python
|
|
796
|
+
type Fetcher = Callable[[str, Mapping[str, str] | None], FetchResponse]
|
|
797
|
+
```
|
|
798
|
+
|
|
799
|
+
#### `OnRetryCallback`
|
|
800
|
+
```python
|
|
801
|
+
type OnRetryCallback = Callable[[RetryEvent], None]
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
---
|
|
805
|
+
|
|
806
|
+
## Exceptions, Constants & Shared Types
|
|
807
|
+
|
|
808
|
+
<a id="exceptions"></a>
|
|
809
|
+
### Exceptions
|
|
810
|
+
|
|
811
|
+
<a id="invalididerror"></a>
|
|
812
|
+
#### `InvalidIDError(rawId: str | int, kind: Literal["work", "chapter", "series"])`
|
|
813
|
+
Subclass of `ValueError` raised when an invalid work, chapter, or series ID is supplied.
|
|
814
|
+
|
|
815
|
+
- `rawId: str | int` — the invalid input value
|
|
816
|
+
- `kind: Literal["work", "chapter", "series"]` — which entity ID failed validation
|
|
817
|
+
|
|
818
|
+
<a id="archivepagerequesterror"></a>
|
|
819
|
+
#### `ArchivePageRequestError(url: str, status: int)`
|
|
820
|
+
Subclass of `RuntimeError` raised when an archive HTTP request returns a non-2xx status code.
|
|
821
|
+
|
|
822
|
+
- `url: str` — requested URL
|
|
823
|
+
- `status: int` — HTTP status code
|
|
824
|
+
|
|
825
|
+
<a id="constants"></a>
|
|
826
|
+
### Constants
|
|
827
|
+
|
|
828
|
+
<a id="ao3_languages"></a>
|
|
829
|
+
#### `AO3_LANGUAGES: Final[dict[str, str]]`
|
|
830
|
+
Mapping from AO3 language codes (e.g. `"en"`, `"ja"`, `"ptBR"`) to AO3 display language names (e.g. `"English"`, `"日本語"`, `"Português brasileiro"`). Used as a fallback when parsing EPUB `<dc:language>` tags.
|
|
831
|
+
|
|
832
|
+
<a id="shared-type-aliases"></a>
|
|
833
|
+
### Shared Type Aliases
|
|
834
|
+
|
|
835
|
+
#### `SortDirection`
|
|
836
|
+
```python
|
|
837
|
+
Literal["asc", "desc"]
|
|
838
|
+
```
|