Metadata-Version: 2.4
Name: ao3fetch
Version: 0.1.0
Summary: Python library for scraping AO3 and AO3-compatible archives, ported from FujoWebDev/AO3.js
Author: Tab Atkins-Bittner
License-Expression: MIT
Project-URL: Homepage, https://github.com/tabatkins/ao3fetch
Project-URL: Repository, https://github.com/tabatkins/ao3fetch
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Typing :: Typed
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: beautifulsoup4>=4.12
Dynamic: license-file

# ao3fetch

A typed Python 3.12+ library for scraping [Archive of Our Own (AO3)](https://archiveofourown.org) and AO3-compatible OTW archives (such as Superlove or SquidgeWorld), ported from [FujoWebDev/AO3.js](https://github.com/FujoWebDev/AO3.js). (Currently up-to-date with [Commit 62e3ab8e](https://github.com/FujoWebDev/AO3.js/commit/62e3ab8ed6f0562e131b6f5aeb215c392a571678).)

## Features

- **Works & Chapters:** Fetch work metadata (`getWork`), full work or single-chapter HTML/notes (`getWorkContent`), and `/navigate` chapter indices with publication dates (`getWorkWithChapters`).
- **Series, Users & Tags:** Scrape series metadata & work blurbs (`getSeries`), user profiles & public works (`getUser`, `getUserWorks`), tag wrangling/hierarchies (`getTag`, `getTagNameById`), and tag search (`searchTags`).
- **Pagination:** Iterate or collect multi-page results with `getAllPages` and `collectAllPages`.
- **EPUB Downloads:** Parse downloaded AO3 `.epub` files offline with `parseEpubWork` (extracting metadata, chapters, notes, and embedded images).
- **Text & Markdown Conversion:** Convert AO3 HTML prose/notes to plain text (`toPlainText`) or clean CommonMark Markdown (`toMarkdown`).
- **Archive-Agnostic:** Target any OTW/AO3-compatible archive via `setArchiveBaseUrl(...)` or the `ARCHIVE_BASE_URL` environment variable.
- **Pluggable Fetcher:** Swap the HTTP layer (`setFetcher`), wrap with `429 Retry-After` + `5xx` retry backoff (`withRetry`), and add in-memory LRU+TTL caching (`withCache`).
- **Fully Typed:** Ships with `py.typed` (PEP 561) and slotted dataclasses.

## Installation

```sh
pip install ao3fetch
```

Requires Python 3.12+ and `beautifulsoup4>=4.12`.

## Quick Start

```python
import ao3fetch

# Optional: enable retry (respecting 429 Retry-After) and in-memory caching.
ao3fetch.setFetcher(ao3fetch.withCache(ao3fetch.withRetry(ao3fetch.defaultFetch)))

# Fetch a full work from AO3 (or another AO3-compatible archive).
work = ao3fetch.getWorkContent(123456, fullWork=True)
if not isinstance(work, ao3fetch.LockedWorkSummary):
    print(work.title, "by", ", ".join(a.pseud for a in work.authors))
    for chapter in work.content.chapters:
        markdown = ao3fetch.toMarkdown(chapter.html)
        print(markdown)
```

### Targeting an AO3-Compatible Archive

```python
import ao3fetch

ao3fetch.setArchiveBaseUrl("https://superlove.sayitditto.net")
work = ao3fetch.getWork(1269)
ao3fetch.resetArchiveBaseUrl()
```

### Parsing a Local AO3 EPUB Download

```python
from pathlib import Path
import ao3fetch

work = ao3fetch.parseEpubWork(Path("story.epub").read_bytes(), filePath="story.epub")
print(work.title, len(work.content.chapters))
```

### Iterating Paginated Listings

```python
import ao3fetch

for page in ao3fetch.getAllPages(ao3fetch.getSeries, seriesId=77):
    for blurb in page.works:
        print(blurb.title)
```

## API Overview

See the full [API Reference](API_REFERENCE.md) for complete type signatures, data class fields, enums, and behavioral notes.

| Category | Exports |
| --- | --- |
| Works & Chapters | `getWork`, `getWorkContent`, `getWorkWithChapters`, `parseWorkBlurb` |
| Series | `getSeries` |
| Users | `getUser`, `getUserWorks` |
| Tags | `getTag`, `getTagNameById`, `searchTags` |
| Downloads | `parseEpubWork` |
| Pagination | `getAllPages`, `collectAllPages`, `PagedResult` |
| HTML Conversion | `toPlainText`, `toMarkdown` |
| URLs & Base URL | `DEFAULT_BASE_URL`, `setArchiveBaseUrl`, `getArchiveBaseUrl`, `resetArchiveBaseUrl`, `getWorkUrl`, `getWorkIndexUrl`, `getSeriesUrl`, `getAsShortUrl`, `getDownloadUrls`, `getUserProfileUrl`, `getUserWorksUrl`, `getTagUrl`, `getTagWorksFeedUrl`, `getTagWorksFeedAtomUrl`, `getUserDetailsFromUrl`, `getAuthorFromUrl`, `getWorkDetailsFromUrl`, `getSearchUrlFromTagFilters`, `isValidArchiveId`, `parseArchiveId` |
| Fetcher & Middleware | `defaultFetch`, `getFetcher`, `setFetcher`, `resetFetcher`, `withRetry`, `withCache`, `getRetryAfterSeconds`, `FetchResponse`, `Fetcher`, `RetryEvent`, `OnRetryCallback` |
| Errors | `InvalidIDError`, `ArchivePageRequestError` |
| Entities & Enums | `WorkSummary`, `WorkWithText`, `LockedWorkSummary`, `WorkWithChapters`, `DownloadedWork`, `Series`, `User`, `UserWorks`, `Tag`, `TagSearchResultSummary`, `Author`, `Chapter`, `ChapterText`, `WorkRatings`, `WorkCategory`, `WorkWarnings`, `WorkWarningStatus`, ... |

## Development

```sh
pip install -r requirements-dev.txt
./lint      # runs ruff, black, mypy, pylint (10/10), check-manifest, and pytest
```

See [CONTRIBUTING.md](CONTRIBUTING.md) for coding conventions.

## License

MIT License — see [LICENSE.md](LICENSE.md). Ported from [FujoWebDev/AO3.js](https://github.com/FujoWebDev/AO3.js) (MIT).
