nab 0.7.1

Token-optimized HTTP client for LLMs — fetches any URL as clean markdown
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
//! NRK (Norwegian) streaming provider

use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use reqwest::Client;
use serde::Deserialize;

use super::common::{last_path_segment_without_query, segment_after, strip_query};
use crate::stream::provider::{EpisodeInfo, SeriesInfo, StreamInfo, StreamProvider};

const NRK_PSAPI_BASE: &str = "https://psapi.nrk.no";
const NRK_PLAYBACK_BASE: &str = "https://psapi.nrk.no/playback";

pub struct NrkProvider {
    client: Client,
}

impl NrkProvider {
    pub fn new() -> Result<Self> {
        let client = Client::builder().user_agent("nab/1.0").build()?;
        Ok(Self { client })
    }

    /// Extract program ID from URL or return as-is if already an ID
    /// URLs: <https://tv.nrk.no/program/KMTE50001219>
    /// URLs: <https://tv.nrk.no/serie/nytt-paa-nytt/sesong/59/episode/7>
    fn extract_program_id(url_or_id: &str) -> String {
        if url_or_id.starts_with("http") {
            // Check for /program/ID pattern
            if let Some(program_id) = segment_after(url_or_id, "program") {
                return program_id.to_string();
            }

            // For series URLs with episode, return the full path for later handling
            if url_or_id.contains("/serie/") && url_or_id.contains("/episode/") {
                // Return series-id/season/episode format
                let parts: Vec<&str> = url_or_id.split('/').collect();
                let serie_idx = parts.iter().position(|&p| p == "serie");
                if let Some(idx) = serie_idx
                    && idx + 5 < parts.len()
                {
                    return format!(
                        "{}/s{}/e{}",
                        parts[idx + 1],
                        parts[idx + 3],              // sesong number
                        strip_query(parts[idx + 5])  // episode number
                    );
                }
            }

            // Fallback: last path segment
            last_path_segment_without_query(url_or_id)
                .unwrap_or(url_or_id)
                .to_string()
        } else {
            url_or_id.to_string()
        }
    }

    /// Extract series ID from URL
    fn extract_series_id(url_or_id: &str) -> String {
        if url_or_id.starts_with("http") {
            segment_after(url_or_id, "serie")
                .or_else(|| last_path_segment_without_query(url_or_id))
                .unwrap_or(url_or_id)
                .to_string()
        } else {
            url_or_id.to_string()
        }
    }

    async fn fetch_playback_manifest(&self, program_id: &str) -> Result<NrkPlaybackResponse> {
        let url = format!("{NRK_PLAYBACK_BASE}/manifest/program/{program_id}");

        let resp = self
            .client
            .get(&url)
            .header("Accept", "application/json")
            .send()
            .await
            .with_context(|| format!("NRK playback API request failed for {program_id}"))?;

        if !resp.status().is_success() {
            return Err(anyhow!(
                "NRK Playback API error: {} for program {}",
                resp.status(),
                program_id
            ));
        }

        resp.json()
            .await
            .context("Failed to parse NRK playback API response")
    }

    async fn fetch_program_metadata(&self, program_id: &str) -> Result<NrkProgramMetadata> {
        let url = format!("{NRK_PSAPI_BASE}/tv/catalog/programs/{program_id}");

        let resp = self
            .client
            .get(&url)
            .header("Accept", "application/json")
            .send()
            .await
            .with_context(|| format!("NRK PSAPI request failed for {program_id}"))?;

        if !resp.status().is_success() {
            return Err(anyhow!(
                "NRK PSAPI error: {} for program {}",
                resp.status(),
                program_id
            ));
        }

        resp.json()
            .await
            .context("Failed to parse NRK program metadata response")
    }

    async fn fetch_series(&self, series_id: &str) -> Result<NrkSeriesResponse> {
        let url = format!("{NRK_PSAPI_BASE}/tv/catalog/series/{series_id}");

        let resp = self
            .client
            .get(&url)
            .header("Accept", "application/json")
            .send()
            .await
            .with_context(|| format!("NRK series API request failed for {series_id}"))?;

        if !resp.status().is_success() {
            return Err(anyhow!(
                "NRK Series API error: {} for series {}",
                resp.status(),
                series_id
            ));
        }

        resp.json()
            .await
            .context("Failed to parse NRK series API response")
    }
}

impl Default for NrkProvider {
    fn default() -> Self {
        Self::new().expect("Failed to create NrkProvider")
    }
}

#[async_trait]
impl StreamProvider for NrkProvider {
    fn name(&self) -> &'static str {
        "nrk"
    }

    fn matches(&self, url: &str) -> bool {
        url.contains("tv.nrk.no") || url.contains("nrk.no/tv") || url.contains("radio.nrk.no")
    }

    async fn get_stream_info(&self, id: &str) -> Result<StreamInfo> {
        let program_id = Self::extract_program_id(id);

        // Fetch playback manifest
        let playback = self.fetch_playback_manifest(&program_id).await?;

        // Find the best playable asset
        let playable = playback
            .playable
            .ok_or_else(|| anyhow!("No playable content found"))?;

        // Get HLS manifest URL
        let manifest_url = playable
            .assets
            .iter()
            .find(|a| a.format == "HLS")
            .map(|a| a.url.clone())
            .ok_or_else(|| anyhow!("No HLS manifest found"))?;

        // Try to get additional metadata
        let metadata = self.fetch_program_metadata(&program_id).await.ok();

        let title = metadata
            .as_ref()
            .map_or_else(|| program_id.clone(), |m| m.titles.title.clone());

        let description = metadata.as_ref().and_then(|m| m.titles.subtitle.clone());

        let duration = playable.duration.map(|d| {
            // Duration is in ISO 8601 format like "PT45M" or "PT1H30M"
            parse_iso8601_duration(&d).unwrap_or(0)
        });

        let thumbnail_url = metadata.as_ref().and_then(|m| {
            m.image.as_ref().and_then(|img| {
                img.web_images
                    .iter()
                    .find(|w| w.pixel_width >= 960)
                    .or(img.web_images.first())
                    .map(|w| w.image_url.clone())
            })
        });

        let is_live = playable.live.unwrap_or(false);

        Ok(StreamInfo {
            id: program_id,
            title,
            description,
            duration_seconds: duration,
            manifest_url,
            is_live,
            qualities: vec![],
            thumbnail_url,
        })
    }

    async fn list_series(&self, series_id: &str) -> Result<SeriesInfo> {
        let id = Self::extract_series_id(series_id);
        let series = self.fetch_series(&id).await?;

        let mut episodes = Vec::new();

        // NRK organizes by seasons
        for season in series.seasons.unwrap_or_default() {
            for episode in season.episodes.unwrap_or_default() {
                episodes.push(EpisodeInfo {
                    id: episode.id,
                    title: episode.titles.title,
                    // Sign loss acceptable: episode/season numbers are non-negative
                    #[allow(clippy::cast_sign_loss)]
                    episode_number: episode.episode_number.map(|n| n as u32),
                    #[allow(clippy::cast_sign_loss)]
                    season_number: Some(season.season_number as u32),
                    duration_seconds: episode.duration.and_then(|d| parse_iso8601_duration(&d)),
                    publish_date: episode.availability.and_then(|a| a.published),
                });
            }
        }

        Ok(SeriesInfo {
            id,
            title: series.titles.title,
            episodes,
        })
    }
}

/// Parse ISO 8601 duration format (PT1H30M45S) to seconds
fn parse_iso8601_duration(duration: &str) -> Option<u64> {
    let duration = duration.trim_start_matches("PT");
    let mut seconds: u64 = 0;
    let mut current_num = String::new();

    for c in duration.chars() {
        if c.is_ascii_digit() {
            current_num.push(c);
        } else {
            let num: u64 = current_num.parse().unwrap_or(0);
            current_num.clear();
            match c {
                'H' => seconds += num * 3600,
                'M' => seconds += num * 60,
                'S' => seconds += num,
                _ => {}
            }
        }
    }

    if seconds > 0 { Some(seconds) } else { None }
}

// Serde structures for NRK API responses

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkPlaybackResponse {
    playable: Option<NrkPlayable>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkPlayable {
    assets: Vec<NrkAsset>,
    duration: Option<String>,
    live: Option<bool>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkAsset {
    url: String,
    format: String,
    #[allow(dead_code)]
    mime_type: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkProgramMetadata {
    titles: NrkTitles,
    image: Option<NrkImage>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkTitles {
    title: String,
    subtitle: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkImage {
    web_images: Vec<NrkWebImage>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkWebImage {
    image_url: String,
    pixel_width: u32,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkSeriesResponse {
    titles: NrkTitles,
    seasons: Option<Vec<NrkSeason>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkSeason {
    season_number: i32,
    episodes: Option<Vec<NrkEpisode>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkEpisode {
    id: String,
    titles: NrkTitles,
    episode_number: Option<i32>,
    duration: Option<String>,
    availability: Option<NrkAvailability>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct NrkAvailability {
    published: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_program_id() {
        assert_eq!(
            NrkProvider::extract_program_id("KMTE50001219"),
            "KMTE50001219"
        );
        assert_eq!(
            NrkProvider::extract_program_id("https://tv.nrk.no/program/KMTE50001219"),
            "KMTE50001219"
        );
        assert_eq!(
            NrkProvider::extract_program_id(
                "https://tv.nrk.no/serie/nytt-paa-nytt/sesong/59/episode/7?autoplay=false"
            ),
            "nytt-paa-nytt/s59/e7"
        );
        assert_eq!(
            NrkProvider::extract_program_id(
                "https://tv.nrk.no/serie/nytt-paa-nytt/sesong/59/episode"
            ),
            "episode"
        );
    }

    #[test]
    fn test_extract_series_id() {
        assert_eq!(
            NrkProvider::extract_series_id("https://tv.nrk.no/serie/nytt-paa-nytt"),
            "nytt-paa-nytt"
        );
        assert_eq!(
            NrkProvider::extract_series_id("https://tv.nrk.no/serie/nytt-paa-nytt?autoplay=false"),
            "nytt-paa-nytt"
        );
        assert_eq!(
            NrkProvider::extract_series_id("https://radio.nrk.no/nytt-paa-nytt?autoplay=false"),
            "nytt-paa-nytt"
        );
    }

    #[test]
    fn test_parse_iso8601_duration() {
        assert_eq!(parse_iso8601_duration("PT1H"), Some(3600));
        assert_eq!(parse_iso8601_duration("PT30M"), Some(1800));
        assert_eq!(parse_iso8601_duration("PT1H30M"), Some(5400));
        assert_eq!(parse_iso8601_duration("PT45M30S"), Some(2730));
    }

    #[test]
    fn test_parse_iso8601_duration_seconds_only() {
        assert_eq!(parse_iso8601_duration("PT90S"), Some(90));
    }

    #[test]
    fn test_parse_iso8601_duration_full() {
        assert_eq!(parse_iso8601_duration("PT2H15M30S"), Some(8130));
    }

    #[test]
    fn test_parse_iso8601_duration_empty() {
        assert_eq!(parse_iso8601_duration("PT"), None);
        assert_eq!(parse_iso8601_duration("PT0S"), None);
    }

    #[test]
    fn test_matches() {
        let provider = NrkProvider::default();
        assert!(provider.matches("https://tv.nrk.no/program/KMTE50001219"));
        assert!(provider.matches("https://nrk.no/tv/program/KMTE50001219"));
        assert!(provider.matches("https://radio.nrk.no/program/ABC123"));
        assert!(!provider.matches("https://example.com"));
    }
}