Metadata-Version: 2.4
Name: rapp
Version: 0.1.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Rust
Classifier: Operating System :: OS Independent
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup :: XML
Classifier: Typing :: Typed
License-File: LICENSE
Summary: Fast, forgiving RSS, Atom, RDF and JSON Feed parser for Python, written in Rust
Keywords: rss,atom,rdf,json-feed,feed,parser,podcast,rust
Author: Jan K. Griffioen-Järfalk
License: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/u840903/rapp/blob/main/CHANGELOG.md
Project-URL: Homepage, https://github.com/u840903/rapp
Project-URL: Issues, https://github.com/u840903/rapp/issues
Project-URL: Repository, https://github.com/u840903/rapp

# rapp

[![CI](https://github.com/u840903/rapp/actions/workflows/ci.yml/badge.svg)](https://github.com/u840903/rapp/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![PyPI](https://img.shields.io/pypi/v/rapp.svg)](https://pypi.org/project/rapp/)
[![Python](https://img.shields.io/pypi/pyversions/rapp.svg)](https://pypi.org/project/rapp/)

A fast, forgiving RSS, Atom, RDF and JSON Feed parser for Python, written in Rust.

```python
import rapp

feed = rapp.parse(data)  # bytes in, Feed out
print(feed.title)
for entry in feed.entries:
    print(entry.published, entry.title, entry.link)
```

- **Fast.** About 4,300 feeds a second on one core and 18,000 with
  `parse_many`, 135 to 570 times faster than `feedparser`
  ([benchmark](#benchmark)).
- **Forgiving.** Real feeds are broken in all sorts of ways. rapp reads
  truncated downloads, wrong encodings, HTML entities, unquoted attributes and
  unclosed tags, and returns what it can instead of giving up
  ([details](#what-it-copes-with)).
- **Podcast-aware.** Enclosures, Media RSS and `itunes:image` /
  `itunes:duration` come out as structured fields.
- **Typed.** Ships type stubs, including `TypedDict`s for `to_dict()`.

## Install

```bash
pip install rapp
```

Wheels cover Python 3.10 and later on Linux (x86_64 and ARM, glibc and musl),
macOS (Intel and Apple Silicon) and Windows. Anywhere else pip builds from
source, which needs a [Rust toolchain](https://rustup.rs).

## Usage

rapp parses; it doesn't fetch. Download feeds with the HTTP client you
already use and pass the body as bytes:

```python
import httpx
import rapp

data = httpx.get("https://example.com/feed.xml").content

try:
    feed = rapp.parse(data)
except rapp.FeedParseError as e:  # an HTML page, a sitemap, empty input...
    print("not a feed:", e)
```

Common fields are attributes. Everything, including authors, categories,
enclosures and media, is in `to_dict()`:

```python
feed.title, feed.link, feed.language, feed.updated

entry = feed.entries[0]
entry.title, entry.link, entry.published, entry.content

d = feed.to_dict()  # {"feed": {...}, "entries": [{...}, ...]}
d["entries"][0]["enclosures"]  # [{"url": ..., "length": ..., "type": ...}]
```

### Many feeds at once

`parse_many` parses a batch across all cores, outside the GIL, and returns the
feeds in input order:

```python
feeds = rapp.parse_many(list_of_bytes)
```

If any feed in the batch fails, `parse_many` raises `FeedParseError` and you
get no results for the rest. When a batch may contain non-feeds, call
`parse` per item instead, or split the batch.

## API

```python
rapp.parse(data: bytes) -> rapp.Feed
rapp.parse_many(blobs: list[bytes]) -> list[rapp.Feed]
rapp.FeedParseError  # the same class as ValueError
```

| `Feed` | `Entry` |
|---|---|
| `title` | `title` |
| `link` | `link` |
| `description` | `description` |
| `language` | `content` |
| `generator` | `published` |
| `updated` | `updated` |
| `id` | `id` |
| `icon`, `logo` | |
| `entries` | |
| `to_dict()` | `to_dict()` |

Every attribute except `entries` is `str | None`. `Feed` and `Entry` are
read-only and safe to share between threads.

`to_dict()` adds the fields that have no attribute:

- **feed:** `links`, `image` (`url`, `title`, `link`, `width`, `height`),
  `itunes_image`, `authors`, `categories`
- **entry:** `links`, `itunes_image`, `itunes_duration` (as written in the
  feed), `authors`, `categories`, `enclosures`, `media`

Every key is always present: a missing value is `None` or an empty list. See
[`src/rapp/_rapp.pyi`](src/rapp/_rapp.pyi) for the full schema.

### Formats

- RSS 2.0, with the content, Dublin Core, Media RSS, iTunes and `atom:link`
  extensions
- Atom 1.0, including `type="xhtml"` content
- RDF / RSS 1.0, with Dublin Core
- JSON Feed 1.0 and 1.1

The format is detected from the content, not from headers or file names.

### Output conventions

- Dates are UTC ISO 8601: `2006-01-02T13:04:05+00:00`. A date rapp can't
  parse is passed through as written.
- HTML in descriptions and content is returned as it is, not sanitized.
  Sanitize it before you render it.

## What it copes with

Each of these comes from a feed that broke a parser in production:

| The feed has | rapp |
|---|---|
| A download cut off mid-tag or mid-CDATA | Returns every entry that was complete |
| A UTF-16 BOM, a wrong encoding declaration, or invalid UTF-8 | Detects the encoding; invalid bytes become `�` |
| HTML entities such as `&nbsp;` or `&rsquo;`, which XML doesn't define | Decodes them |
| Unescaped HTML (`<b>`, `<br>`) inside a description | Keeps it as markup |
| Unquoted attributes (`<link href=x>`) | Quotes them without touching text such as `E=mc2` |
| An unclosed tag inside Atom XHTML content | Keeps the entries that follow |
| A doubled `<?xml ?>` declaration | Repairs it |
| `24:00` as a time | Rolls over to `00:00` the next day |
| RDF's `<image>` beside `<channel>` | Reads it as the feed image |

## Benchmark

Parsing the bundled corpus of 200 real-world feeds (45 MB, 6,600 entries) on
an Apple M4 Pro (14 cores), Python 3.14.7, fastest of five runs:

| Parser | Time | Feeds/s | vs feedparser |
|---|---:|---:|---:|
| `rapp.parse` | 46 ms | 4,351 | 135× |
| `rapp.parse_many` | 11 ms | 18,304 | 568× |
| fastfeedparser 0.6.5 | 65 ms | 3,070 | 95× |
| feedparser 6.0.14 | 6,204 ms | 32 | 1× |

To run it yourself:

```bash
pip install . feedparser fastfeedparser
python benchmark/benchmark.py
```

## Development

```bash
cargo test                          # Rust tests
pip install -e . pytest             # build the extension, then
pytest                              # Python tests
cargo fmt && cargo clippy           # Rust style and lints
ruff check . && ruff format .       # Python style and lints
```

The Rust parser lives in `crates/rapp-core` and has no Python dependency.
`crates/rapp-py` holds the pyo3 bindings and `src/rapp` the Python package.
Snapshot tests compare every feed in `tests/feeds/` with the expected JSON
next to it; see [`tests/test_snapshots.py`](tests/test_snapshots.py) for how
to add one.

To release: bump `version` in `Cargo.toml` (the only place it lives), move
the changelog's "Unreleased" notes under the new version, merge, then publish
a GitHub release tagged `vX.Y.Z`. The release workflow builds the wheels, tests
them on every platform and uploads them to PyPI.

Bugs and ideas are welcome as [issues](https://github.com/u840903/rapp/issues).
A feed that rapp gets wrong is the most useful report there is. To report a
security issue, see [SECURITY.md](SECURITY.md).

## Background

rapp started as a fork of Kagi's
[fastfeedparser](https://github.com/kagisearch/fastfeedparser), took
inspiration from it, and became its own parser, written from scratch in Rust.
The test and benchmark feeds come from fastfeedparser.

## License

MIT. See [LICENSE](LICENSE).

