Metadata-Version: 2.4
Name: sermonindex
Version: 0.1.0
Summary: Client for the SermonIndex.net API: sermons, speakers, topics and scripture. Standard library only.
Author: SermonIndex.net
License-Expression: MIT
Project-URL: Homepage, https://www.sermonindex.net/api/
Keywords: sermonindex,sermons,bible,api,audio
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# sermonindex

A small Python client for the [SermonIndex.net API](https://www.sermonindex.net/api/): sermons, speakers, topics and scripture, over 70,000 sermons that are free to listen to. It uses only the standard library and needs Python 3.9 or later.

The API is a set of pre-generated static files. This library only ever makes GET requests, and never adds a query string.

## Install

    pip install sermonindex

## Examples

Sermons on a verse (the file for a famous verse such as John 3:16 is several megabytes, so ask for a smaller one when you can):

```python
from sermonindex import SermonIndex

si = SermonIndex()
verse = si.scripture("Romans", 8, 28)   # names, abbreviations and codes all work
print(verse["reference"], verse["sermonCount"])
for s in verse["sermons"][:5]:
    print(s["title"], "-", s["speaker"])
```

A small summary of what is preached on a whole chapter, light enough for a hover card:

```python
cards = si.cards("John", 3)   # short keys: v, b, n, c, k, u, w, h, s, t, e, m
```

Walk a preacher's sermons, one page of fifty at a time:

```python
for sermon in si.speaker_sermons("zac-poonen"):
    print(sermon["title"])
    if "Prayer" in sermon["title"]:
        break   # no further pages are fetched
```

Play audio, with a fallback:

```python
import urllib.request

sermon = si.sermon("OTh0VCDnIG3i6wOj")
for url in si.audio_sources(sermon):   # the archive.org copy, then the CDN copy; empty if hasAudio is false
    try:
        urllib.request.urlretrieve(url, "sermon.mp3")
        break
    except OSError:
        continue
```

One random sermon that is certain to have audio:

```python
r = si.random()
print(r["title"], r["speaker"], r["mp3Url"])
```

Speakers and topics:

```python
speakers = si.speakers()["speakers"]   # about 1.8 MB
prayer = si.topic("prayer")            # several megabytes for the large topics
stats = si.stats()
```

Search the whole library by title, speaker and topic:

```python
for hit in si.search("revival", speaker="ravenhill", limit=10):
    print(hit["title"], hit["url"], hit["hasAudio"])
```

## Things that surprise people

- There are no query parameters. A CDN cannot read them, so every page size is a set of real files. Pages hold 500 sermons for the whole library and 50 for a speaker.
- Follow `next`. `sermons()` and `speaker_sermons()` do this for you. A guessed page path is a bare 404.
- Only speakers with more than 50 sermons have pages. For the others `speaker_sermons()` falls back to the full record, which for them is small. The full record of a large speaker can be 8 MB, which is why pages are preferred.
- Audio is Archive first, CDN second. `mp3Url` points at the Archive.org mirror when there is one; `cdnMp3Url` is our own copy. `audio_sources()` returns them in that order.
- Check `hasAudio`. Text-only sermons carry empty strings in every media field.
- The book codes are OSIS-style, with `SNG` for Song of Songs and `NAM` for Nahum. `scripture()` and `cards()` accept names such as "Song of Songs", abbreviations such as "SOS" or "Nah", and the codes themselves.
- A 404 comes back with an HTML page labelled as JSON. The library checks the status first and raises `NotFoundError`.
- Some speaker slugs contain accents or `&`. Pass the slug as the API gives it; the library percent-encodes it, and if the file is stored under the other Unicode form (composed or decomposed) it tries that too.
- `topics()` is about 38 MB, and some chapter and verse files are very large. Prefer `topic(slug)`, `sermons()` and `cards()`.

## Search

`search(query, speaker=None, limit=20)` reads a compact file, `/v2/search-index.json`, once per client and matches in memory. All words must match somewhere in the title, the speaker name or the topic names; a title match ranks above the others. The file is about 3 MB to download and is fetched only when you first search. If it cannot be found, `search()` raises `SearchUnavailableError`; everything else works without it. You can point at another copy with `SermonIndex(search_index_url=...)`.

## Options

```python
SermonIndex(
    base_url="https://api.sermonindex.net/v2",
    timeout=30.0,                 # seconds per request
    user_agent="my-app/1.0",
)
```

A network error is retried once. Errors are `SermonIndexError`, with `NotFoundError` and `SearchUnavailableError` as subclasses. The package carries type hints. Records are returned as plain dictionaries with the API's own field names.

## Tests

    python3 -m unittest

The live tests make light read-only requests to the real API; a few pull a file of one to two megabytes.

## Terms

The sermons are free for personal and ministry use, and not for commercial sale; see [the copying permissions](https://www.sermonindex.net/md/copying-permissions/). The code is MIT licensed.
