stremio-addon-core 0.1.4

Reusable Rust core for authenticated Stremio addon servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
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
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
223
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
253
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
280
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
307
308
309
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# stremio-addon-core

Reusable Rust base for Stremio addon HTTP servers.

This crate owns the protocol-level work common to provider addons:

- Stremio manifest, stream, catalog, and meta response models.
- GET and POST stream routes.
- Encoded Stremio config path decoding.
- Private addon auth from config, path, query, bearer header, or custom header.
- CORS.
- Health routes.
- Optional playback redirect routes.
- Optional HMAC-signed playback tokens.
- Router options for common route surfaces such as stream-only addons, key-prefixed private addons, and manifest aliases.

Provider crates should keep provider-specific login, search API execution, and playback resolution outside this crate. The core includes shared title lookup, search query generation, normalized result ranking, filename/media parsing, JSON cache storage, stream selection, and stream-card formatting helpers that multiple providers can reuse.

## Install Shape

Add the crate to a provider app:

```toml
[dependencies]
stremio-addon-core = "0.1.4"
async-trait = "0.1"
axum = "0.7"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
```

## CI/CD

The crate includes GitHub Actions workflows:

- `.github/workflows/ci.yml`: runs on pushes, pull requests, and manual dispatch. It checks formatting, `cargo check`, clippy, tests, and `cargo package`.
- `.github/workflows/release.yml`: runs on `v*.*.*` tags or manual dispatch. It repeats validation, runs `cargo publish --dry-run`, then publishes to crates.io when triggered by a matching tag or manual `publish=true`.

CI uses Rust 1.88.0, matching the crate `rust-version`. This is required because the current locked dependency graph includes crates whose declared MSRV is 1.88.

Crates.io publishing requires a repository secret:

```text
CARGO_REGISTRY_TOKEN
```

Release flow:

1. Update `version` in `Cargo.toml`.
2. Run `cargo test --locked --all-targets`.
3. Create and push a matching tag, for example `v0.1.4`.
4. The release workflow verifies the tag matches `Cargo.toml` and publishes the crate.

## Minimal Adapter

```rust
use async_trait::async_trait;
use std::sync::Arc;
use stremio_addon_core::{
    build_router, AddonAdapter, AddonContext, AddonError, AuthConfig, BehaviorHints,
    CatalogExtraArgs, CatalogResponse, Manifest, MetaResponse, PlaybackResponse, ResourceSpec,
    Stream, StreamResponse,
};

struct MyAddon;

#[async_trait]
impl AddonAdapter for MyAddon {
    async fn manifest(&self, _ctx: AddonContext) -> Result<Manifest, AddonError> {
        Ok(Manifest {
            id: "example.my-addon".to_string(),
            version: "0.1.0".to_string(),
            name: "My Addon".to_string(),
            description: Some("Example addon".to_string()),
            resources: vec![ResourceSpec::from("stream")],
            types: vec!["movie".to_string(), "series".to_string()],
            catalogs: vec![],
            id_prefixes: vec!["tt".to_string()],
            behavior_hints: Some(BehaviorHints {
                configurable: Some(false),
                configuration_required: Some(false),
                p2p: Some(false),
                adult: Some(false),
            }),
            config: vec![],
            logo: None,
            background: None,
            contact_email: None,
            extra: Default::default(),
        })
    }

    async fn stream(
        &self,
        _ctx: AddonContext,
        content_type: String,
        id: String,
    ) -> Result<StreamResponse, AddonError> {
        Ok(StreamResponse {
            streams: vec![Stream {
                name: Some(format!("Example {content_type}")),
                title: Some(id),
                url: Some("https://cdn.example/video.mp4".to_string()),
                ..Stream::default()
            }],
        })
    }

    async fn catalog(
        &self,
        _ctx: AddonContext,
        _content_type: String,
        _id: String,
        _extra: CatalogExtraArgs,
    ) -> Result<CatalogResponse, AddonError> {
        Ok(CatalogResponse::default())
    }

    async fn meta(
        &self,
        _ctx: AddonContext,
        _content_type: String,
        _id: String,
    ) -> Result<MetaResponse, AddonError> {
        Ok(MetaResponse::default())
    }

    async fn playback(
        &self,
        _ctx: AddonContext,
        _ident: String,
    ) -> Result<PlaybackResponse, AddonError> {
        Err(AddonError::Playback("playback unsupported".to_string()))
    }
}

#[tokio::main]
async fn main() {
    let auth = AuthConfig::required(std::env::var("ADDON_AUTH_KEY").unwrap());
    let app = build_router(Arc::new(MyAddon), auth);
    let listener = tokio::net::TcpListener::bind("0.0.0.0:61613").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

## Routes

Core Stremio routes:

| Route | Method | Handler |
| --- | --- | --- |
| `/manifest.json` | GET | `AddonAdapter::manifest` |
| `/{config}/manifest.json` | GET | `AddonAdapter::manifest` with decoded config |
| `/stream/{type}/{id}` | GET | `AddonAdapter::stream` |
| `/{config}/stream/{type}/{id}` | GET | `AddonAdapter::stream` with decoded config |
| `/stream` | POST | `AddonAdapter::stream_request` |
| `/api/streams` | POST | `AddonAdapter::stream_request` |
| `/api/streams/{type}/{id}` | GET | `AddonAdapter::stream` |
| `/catalog/{type}/{id}/{extra}` | GET | `AddonAdapter::catalog` |
| `/{config}/catalog/{type}/{id}/{extra}` | GET | `AddonAdapter::catalog` with decoded config |
| `/meta/{type}/{id}` | GET | `AddonAdapter::meta` |
| `/{config}/meta/{type}/{id}` | GET | `AddonAdapter::meta` with decoded config |
| `/play/{ident}` | GET | `AddonAdapter::playback` |
| `/{config}/play/{ident}` | GET | `AddonAdapter::playback` with decoded config |

Alias routes enabled by default:

| Route | Purpose |
| --- | --- |
| `/` | manifest alias |
| `/stremio/manifest.json` | manifest alias |
| `/api/manifest` | manifest alias |
| `/health` | unauthenticated health |
| `/healthz` | unauthenticated health |
| `/u/{authKey}/manifest.json` | key-prefixed private manifest |
| `/u/{authKey}/stream/{type}/{id}` | key-prefixed private stream |

The route parser strips a trailing `.json` from Stremio `id` path segments.

## Router Options

Use `build_router_with_options` when the provider needs a narrower route surface:

```rust
use stremio_addon_core::{build_router_with_options, RouterOptions};

let router = build_router_with_options(
    adapter,
    auth,
    RouterOptions {
        catalog_routes: false,
        meta_routes: false,
        playback_routes: false,
        alias_routes: true,
        path_key_routes: false,
        health_routes: true,
        playback_signing_key: None,
    },
);
```

Options:

| Option | Default | Meaning |
| --- | --- | --- |
| `catalog_routes` | `true` | Mount catalog routes. Disable for stream-only addons. |
| `meta_routes` | `true` | Mount meta routes. Disable for stream-only addons. |
| `playback_routes` | `true` | Mount `/play` redirect routes. Disable for direct-URL providers. |
| `alias_routes` | `true` | Mount root/index/API manifest aliases. |
| `path_key_routes` | `true` | Mount `/u/{authKey}/...` routes. |
| `health_routes` | `true` | Mount `/health` and `/healthz`. |
| `playback_signing_key` | `None` | If set, `/play/{ident}` requires `?sig=` signed by this key. |

## Auth

`AuthConfig::required(key)` applies to Stremio routes. `AuthConfig::disabled()` skips addon auth, useful for tests or public addons.

Accepted auth locations, in precedence order:

1. Encoded config JSON: `/{%7B%22authKey%22%3A%22secret%22%7D}/manifest.json`
2. Path key: `/u/secret/manifest.json`
3. Query string: `?authKey=secret`
4. Query string alias: `?key=secret`
5. Bearer header: `Authorization: Bearer secret`
6. Header: `X-Addon-Auth: secret`

Key comparisons use constant-time equality. `AuthConfig` redacts the configured key from `Debug`.

Health routes are intentionally unauthenticated.

## Encoded Config

Stremio supports a configuration object encoded as a path segment:

```json
{"authKey":"secret","enableSearch":true}
```

The core decodes this into:

```rust
pub struct UserConfig {
    pub auth_key: Option<String>,
    pub enable_search: Option<bool>,
    pub extra: serde_json::Map<String, serde_json::Value>,
}
```

Unknown fields are preserved in `extra`, so provider adapters can read custom fields without changing the core model.

## Stream Requests

GET stream routes call:

```rust
async fn stream(ctx, content_type, id) -> Result<StreamResponse, AddonError>
```

POST stream routes call:

```rust
async fn stream_request(ctx, request) -> Result<StreamResponse, AddonError>
```

The default `stream_request` implementation forwards to `stream` when `type` and `id` are present. Override it for provider-specific POST request bodies that use alternate fields such as `name`, `episode`, `year`, or custom metadata.

POST body shape:

```json
{
  "type": "movie",
  "id": "tt0111161",
  "name": "The Shawshank Redemption",
  "year": "1994",
  "episode": null
}
```

## Stream Responses

`Stream` supports the common Stremio transport shapes:

- `url`: direct playable URL or addon playback URL.
- `externalUrl`: external page.
- `infoHash` plus `sources`: torrent/P2P style.

It also has typed provider fields used by local addons:

- `quality`
- `behaviorHints.countryWhitelist`
- `behaviorHints.bingeGroup`
- `behaviorHints.videoSize`
- `behaviorHints.filename`

Provider-specific fields can be added through `extra` maps.

## Metadata Lookups

`metadata` provides a small TMDB/Cinemeta resolver:

```rust
use stremio_addon_core::{MetadataClient, MetadataConfig};

let metadata = MetadataClient::new(MetadataConfig {
    tmdb_api_key: std::env::var("TMDB_API_KEY").ok(),
    timeout_seconds: 10,
    ..MetadataConfig::default()
});

let info = metadata.lookup_imdb("movie", "tt0111161").await?;
let episode = metadata.lookup_imdb("series", "tt0903747:1:2").await?;
let tmdb = metadata.lookup_tmdb_id("movie", "278").await?;
```

Resolution behavior:

- Uses TMDB first when `tmdb_api_key` is set.
- Falls back to Cinemeta for IMDb IDs when TMDB returns no usable result.
- Uses Cinemeta only when no TMDB key is configured.
- Preserves `season` and `episode` from Stremio IDs like `tt123:1:2`.
- Returns `TitleInfo` with Czech/primary title, Slovak title, English title, original title, year, type, season, and episode.

The core performs metadata HTTP requests, but it does not cache metadata. Provider apps should add caching around `MetadataClient` if needed.

## Search Query Generation

`search` builds provider-friendly query variants from `TitleInfo`:

```rust
use stremio_addon_core::{build_search_queries, QueryInput, QueryProfile};

let input = QueryInput::from_title_info(&info);
let queries = build_search_queries(QueryProfile::Balanced, &input);
```

Profiles:

- Title/year profile: movie queries include each title and title-with-year variants; series queries include `S01E02` and `01x02` variants.
- Ordered fallback profile: query order starts specific and gradually broadens, useful for providers where query order is an important ranking signal.

Providers still execute provider searches and normalize raw provider rows into core `SearchResult` values.

## Search Result Ranking

`ranking` scores normalized provider results against the metadata returned by TMDB/Cinemeta before cards are built:

```rust
use stremio_addon_core::{rank_results, RankingOptions, RankingProfile, SearchResult};

let ranked = rank_results(
    &info,
    vec![SearchResult {
        id: "provider-file-id".to_string(),
        title: "Movie.2024.1080p.mkv".to_string(),
        parsed_title: Some("Movie".to_string()),
        filename: Some("Movie.2024.1080p.mkv".to_string()),
        year: Some(2024),
        size_bytes: Some(2_000_000_000),
        positive_votes: Some(12),
        negative_votes: Some(1),
        quality_rank: Some(3),
        query_index: Some(0),
        ..SearchResult::default()
    }],
    &RankingOptions::for_profile(RankingProfile::Balanced),
);
```

Profiles:

- Provider-tuned profiles for existing query styles.
- `Balanced`: generic title and filename blend for new adapters.

Ranking behavior:

- Filters protected rows by default.
- Filters results with a year outside the configured tolerance.
- Requires exact season/episode matches for series when metadata has episode information.
- Filters episode-like movie false positives for profiles that enable that guard.
- Preserves `strong_match` and `weak_match` flags for card formatting.
- Uses optional vote ratio and quality rank as small tie-breakers.

The recommended provider pipeline is:

1. Resolve `TitleInfo` with `MetadataClient`.
2. Build queries with `build_search_queries`.
3. Execute provider searches.
4. Normalize raw provider rows into `SearchResult`.
5. Rank and filter with `rank_results`.
6. Convert ranked rows to `StreamCardInput` and build `Stream` cards.

## Stream Card Formatting

`cards` converts common provider presentation data into `Stream` values:

```rust
use stremio_addon_core::{stream_card, CardProfile, StreamCardInput};

let stream = stream_card(CardProfile::Compact, StreamCardInput {
    provider: "Example".to_string(),
    provider_url: Some("https://addon/play/file-id?sig=...".to_string()),
    filename: Some("Movie.2024.1080p.mkv".to_string()),
    quality: Some("1080p".to_string()),
    size_bytes: Some(2_000_000_000),
    positive_votes: Some(10),
    negative_votes: Some(0),
    strong_match: true,
    ..StreamCardInput::default()
});
```

Profiles:

- Provider-tuned profiles for richer legacy card layouts.
- `Compact`: generic one-line card useful for simple providers.

The helpers are intentionally presentation-only. They should run after metadata lookup, query generation, provider search, and ranking. They do not resolve playback URLs.

## Playback Redirects

Use playback routes when stream URLs should point back to the addon instead of exposing provider tokens or expensive direct URLs.

Typical private provider flow:

1. Provider search returns stream URL `/play/{ident}?authKey=...&sig=...`.
2. Core validates addon auth.
3. If `RouterOptions.playback_signing_key` is set, core verifies `sig`.
4. Core calls `AddonAdapter::playback(ctx, ident)`.
5. Adapter resolves the real provider URL.
6. Core redirects with `Cache-Control: max-age={seconds}, must-revalidate, proxy-revalidate`.

Create signatures with `SignedPlayback`:

```rust
use stremio_addon_core::SignedPlayback;

let payload = SignedPlayback::new("provider-file-id", 18_000);
let sig = payload.sign(b"redirect-signing-key")?;
```

If signing is enabled, the signed payload `ident` must match the `/play/{ident}` path.

For providers that already return safe direct playable URLs, disable playback routes:

```rust
RouterOptions {
    playback_routes: false,
    ..RouterOptions::default()
}
```

## Manifest Compatibility

`Manifest.resources` accepts both official Stremio shapes:

```json
"resources": ["stream"]
```

and:

```json
"resources": [
  { "name": "stream", "types": ["movie", "series"], "idPrefixes": ["tt"] }
]
```

`Manifest.extra` can carry fields that are not modeled explicitly, such as `stremioAddonsConfig`.

## Error Behavior

Errors are JSON:

```json
{"error":"..."}
```

Status mapping:

| Error | Status |
| --- | --- |
| auth failure | `401` |
| invalid config or bad request | `400` |
| provider/playback failure | `500` |

Provider adapters should return empty Stremio responses for normal no-result cases, not errors:

```rust
Ok(StreamResponse::default())
Ok(CatalogResponse::default())
Ok(MetaResponse::default())
```

## Provider Design Guidance

Keep these in provider crates:

- Provider HTTP clients.
- Login/session/token caching.
- Provider-specific filename parsing that does not fit the shared parser.
- Provider-specific cache policy. The shared `FileJsonCache` only provides storage semantics.
- Provider-specific ranking overrides that do not fit the built-in profiles.
- Provider-specific stream-card formatting that does not fit the built-in card profiles.

This keeps the core stable enough to reuse across provider adapters.