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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Streamlink bridge backend for streaming
//!
//! Uses streamlink subprocess for:
//! - `YouTube`, Twitch, and 1000+ other streaming sites
//! - HLS/DASH streams with site-specific extraction
//! - Live streams with real-time output

use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use std::path::Path;
use std::process::Stdio;
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::process::Command;
use tracing::{debug, info, warn};

use crate::stream::StreamQuality;
use crate::stream::backend::{
    BackendType, ProgressCallback, StreamBackend, StreamConfig, StreamProgress,
};

/// Streamlink-based streaming backend
pub struct StreamlinkBackend {
    /// Path to streamlink binary
    streamlink_path: String,
    /// Additional streamlink arguments
    extra_args: Vec<String>,
}

impl StreamlinkBackend {
    /// Create new streamlink backend, searching for binary in PATH
    pub fn new() -> Result<Self> {
        let streamlink_path = which::which("streamlink").map_or_else(
            |_| "streamlink".to_string(),
            |p| p.to_string_lossy().to_string(),
        );

        Ok(Self {
            streamlink_path,
            extra_args: Vec::new(),
        })
    }

    /// Specify custom streamlink binary path
    #[must_use]
    pub fn with_streamlink_path(mut self, path: &str) -> Self {
        self.streamlink_path = path.to_string();
        self
    }

    /// Add extra streamlink arguments
    #[must_use]
    pub fn with_extra_args(mut self, args: Vec<String>) -> Self {
        self.extra_args = args;
        self
    }

    /// Convert `StreamQuality` to streamlink quality string
    fn quality_to_string(quality: StreamQuality) -> String {
        match quality {
            StreamQuality::Best => "best".to_string(),
            StreamQuality::Worst => "worst".to_string(),
            StreamQuality::Specific(height) => format!("{height}p"),
        }
    }

    /// Build streamlink command arguments for stdout output
    fn build_args_stdout(&self, url: &str, config: &StreamConfig) -> Vec<String> {
        let mut args = Vec::new();

        // Output to stdout
        args.push("-O".to_string());

        // Headers
        for (key, value) in &config.headers {
            args.push("--http-header".to_string());
            args.push(format!("{key}={value}"));
        }

        // Cookies
        if let Some(ref cookies) = config.cookies {
            args.push("--http-cookies".to_string());
            args.push(cookies.clone());
        }

        // Extra args
        args.extend(self.extra_args.clone());

        // URL and quality
        args.push(url.to_string());
        args.push(Self::quality_to_string(config.quality));

        args
    }

    /// Build streamlink command arguments for file output
    fn build_args_file(
        &self,
        url: &str,
        config: &StreamConfig,
        output_path: &str,
        duration_secs: Option<u64>,
    ) -> Vec<String> {
        let mut args = Vec::new();

        // Output to file
        args.push("-o".to_string());
        args.push(output_path.to_string());

        // Force overwrite
        args.push("-f".to_string());

        // Duration limit for HLS/DASH streams
        if let Some(dur) = duration_secs {
            args.push("--stream-segmented-duration".to_string());
            args.push(format!("{dur}s"));
        }

        // Headers
        for (key, value) in &config.headers {
            args.push("--http-header".to_string());
            args.push(format!("{key}={value}"));
        }

        // Cookies
        if let Some(ref cookies) = config.cookies {
            args.push("--http-cookies".to_string());
            args.push(cookies.clone());
        }

        // Extra args
        args.extend(self.extra_args.clone());

        // URL and quality
        args.push(url.to_string());
        args.push(Self::quality_to_string(config.quality));

        args
    }

    /// Check if streamlink is available
    pub async fn check_available(&self) -> bool {
        Command::new(&self.streamlink_path)
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await
            .map(|s| s.success())
            .unwrap_or(false)
    }

    /// Check if streamlink supports a URL (via `streamlink --can-handle-url`)
    pub async fn can_handle_url(&self, url: &str) -> bool {
        Command::new(&self.streamlink_path)
            .arg("--can-handle-url")
            .arg(url)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await
            .map(|s| s.success())
            .unwrap_or(false)
    }

    /// Parse progress from streamlink stderr
    /// Streamlink outputs: "[download][stream] Downloaded X.XX MiB"
    fn parse_progress(line: &str) -> Option<StreamlinkProgress> {
        if !line.contains("Downloaded") {
            return None;
        }

        // Try to parse "Downloaded X.XX MiB" or "Downloaded X.XX KiB"
        let downloaded_part = line.split("Downloaded").nth(1)?;
        let parts: Vec<&str> = downloaded_part.split_whitespace().collect();

        if parts.len() >= 2 {
            let value: f64 = parts[0].parse().ok()?;
            let unit = parts[1];

            // Truncation/sign-loss acceptable: byte counts are non-negative finite values
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            let bytes = match unit {
                "KiB" => (value * 1024.0) as u64,
                "MiB" => (value * 1024.0 * 1024.0) as u64,
                "GiB" => (value * 1024.0 * 1024.0 * 1024.0) as u64,
                _ => return None,
            };

            return Some(StreamlinkProgress {
                bytes_downloaded: bytes,
            });
        }

        None
    }
}

#[async_trait]
impl StreamBackend for StreamlinkBackend {
    fn backend_type(&self) -> BackendType {
        BackendType::Streamlink
    }

    fn can_handle(&self, manifest_url: &str, _encrypted: bool) -> bool {
        // Streamlink handles many streaming sites by URL pattern
        // Common supported domains
        let supported_patterns = [
            "youtube.com",
            "youtu.be",
            "twitch.tv",
            "dailymotion.com",
            "vimeo.com",
            "facebook.com",
            "mixer.com",
            "crunchyroll.com",
            "mlg.com",
            "livestream.com",
            "ustream.tv",
            "afreeca.com",
            "bilibili.com",
            "huya.com",
            "douyu.com",
            "nimo.tv",
            "picarto.tv",
            "pluto.tv",
            "tv.se", // Swedish TV
            "svtplay.se",
            "tv4play.se",
            "ruv.is",
            "dr.dk",
            "nrk.no",
        ];

        // Check URL patterns
        for pattern in &supported_patterns {
            if manifest_url.contains(pattern) {
                return true;
            }
        }

        // Also handle generic HLS/DASH that streamlink might support
        // But prefer ffmpeg for raw manifest URLs
        false
    }

    async fn stream_to<W: AsyncWrite + Unpin + Send>(
        &self,
        manifest_url: &str,
        config: &StreamConfig,
        output: &mut W,
        progress: Option<ProgressCallback>,
    ) -> Result<()> {
        let args = self.build_args_stdout(manifest_url, config);
        debug!("streamlink args: {:?}", args);

        let mut child = Command::new(&self.streamlink_path)
            .args(&args)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn streamlink process")?;

        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| anyhow!("Failed to capture streamlink stdout"))?;
        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| anyhow!("Failed to capture streamlink stderr"))?;

        let start_time = std::time::Instant::now();

        // Spawn stderr reader for progress and error logging
        let stderr_handle = tokio::spawn(async move {
            let reader = BufReader::new(stderr);
            let mut lines = tokio::io::AsyncBufReadExt::lines(reader);

            while let Ok(Some(line)) = lines.next_line().await {
                debug!("streamlink: {}", line);
                // Log errors/warnings
                if line.contains("error") || line.contains("Error") {
                    warn!("streamlink: {}", line);
                }
            }
        });

        // Copy stdout to output
        let mut stdout_reader = BufReader::new(stdout);
        let mut buffer = vec![0u8; 64 * 1024]; // 64KB heap buffer
        let mut total_bytes = 0u64;

        loop {
            let n = stdout_reader.read(&mut buffer).await?;
            if n == 0 {
                break;
            }

            output.write_all(&buffer[..n]).await?;
            total_bytes += n as u64;

            if let Some(ref cb) = progress {
                cb(StreamProgress {
                    bytes_downloaded: total_bytes,
                    segments_completed: 0,
                    segments_total: None,
                    elapsed_seconds: start_time.elapsed().as_secs_f64(),
                });
            }
        }

        // Wait for process to complete
        let status = child
            .wait()
            .await
            .context("Failed to wait for streamlink process")?;
        stderr_handle.abort(); // Stop stderr reader

        if !status.success() {
            return Err(anyhow!("streamlink exited with status: {status}"));
        }

        output.flush().await?;
        info!("Streamed {} bytes via streamlink", total_bytes);

        Ok(())
    }

    async fn stream_to_file(
        &self,
        manifest_url: &str,
        config: &StreamConfig,
        path: &Path,
        progress: Option<ProgressCallback>,
        duration_secs: Option<u64>,
    ) -> Result<()> {
        let path_str = path.to_string_lossy();
        let args = self.build_args_file(manifest_url, config, &path_str, duration_secs);
        debug!("streamlink args: {:?}", args);

        let mut child = Command::new(&self.streamlink_path)
            .args(&args)
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn streamlink process")?;

        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| anyhow!("Failed to capture streamlink stderr"))?;

        let start_time = std::time::Instant::now();

        // Read stderr for progress
        let reader = BufReader::new(stderr);
        let mut lines = tokio::io::AsyncBufReadExt::lines(reader);

        while let Ok(Some(line)) = lines.next_line().await {
            if let Some(prog) = Self::parse_progress(&line)
                && let Some(ref cb) = progress
            {
                cb(StreamProgress {
                    bytes_downloaded: prog.bytes_downloaded,
                    segments_completed: 0,
                    segments_total: None,
                    elapsed_seconds: start_time.elapsed().as_secs_f64(),
                });
            }

            if line.contains("error") || line.contains("Error") {
                warn!("streamlink: {}", line);
            }
        }

        let status = child
            .wait()
            .await
            .context("Failed to wait for streamlink process")?;

        if !status.success() {
            return Err(anyhow!("streamlink exited with status: {status}"));
        }

        info!("Saved stream to {:?} via streamlink", path);
        Ok(())
    }
}

#[derive(Debug, Clone)]
struct StreamlinkProgress {
    bytes_downloaded: u64,
}

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

    #[test]
    fn test_quality_to_string() {
        assert_eq!(
            StreamlinkBackend::quality_to_string(StreamQuality::Best),
            "best"
        );
        assert_eq!(
            StreamlinkBackend::quality_to_string(StreamQuality::Worst),
            "worst"
        );
        assert_eq!(
            StreamlinkBackend::quality_to_string(StreamQuality::Specific(720)),
            "720p"
        );
    }

    #[test]
    fn test_build_args_stdout() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec![],
        };

        let config = StreamConfig {
            quality: StreamQuality::Best,
            headers: HashMap::new(),
            cookies: None,
        };

        let args = backend.build_args_stdout("https://www.twitch.tv/example", &config);

        assert!(args.contains(&"-O".to_string()));
        assert!(args.contains(&"https://www.twitch.tv/example".to_string()));
        assert!(args.contains(&"best".to_string()));
    }

    #[test]
    fn test_build_args_file() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec![],
        };

        let config = StreamConfig {
            quality: StreamQuality::Specific(720),
            headers: HashMap::new(),
            cookies: None,
        };

        let args = backend.build_args_file(
            "https://www.twitch.tv/example",
            &config,
            "/tmp/output.ts",
            None,
        );

        assert!(args.contains(&"-o".to_string()));
        assert!(args.contains(&"/tmp/output.ts".to_string()));
        assert!(args.contains(&"-f".to_string()));
        assert!(args.contains(&"https://www.twitch.tv/example".to_string()));
        assert!(args.contains(&"720p".to_string()));
    }

    #[test]
    fn test_build_args_with_headers() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec![],
        };

        let mut headers = HashMap::new();
        headers.insert("Referer".to_string(), "https://example.com".to_string());
        headers.insert("User-Agent".to_string(), "Custom/1.0".to_string());

        let config = StreamConfig {
            quality: StreamQuality::Best,
            headers,
            cookies: None,
        };

        let args = backend.build_args_stdout("https://www.twitch.tv/example", &config);

        // Check that --http-header appears
        let header_count = args.iter().filter(|a| *a == "--http-header").count();
        assert_eq!(header_count, 2);
    }

    #[test]
    fn test_build_args_with_cookies() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec![],
        };

        let config = StreamConfig {
            quality: StreamQuality::Best,
            headers: HashMap::new(),
            cookies: Some("session=abc123".to_string()),
        };

        let args = backend.build_args_stdout("https://www.twitch.tv/example", &config);

        assert!(args.contains(&"--http-cookies".to_string()));
        assert!(args.contains(&"session=abc123".to_string()));
    }

    #[test]
    fn test_can_handle() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec![],
        };

        // Supported sites
        assert!(backend.can_handle("https://www.twitch.tv/example", false));
        assert!(backend.can_handle("https://www.youtube.com/watch?v=abc123", false));
        assert!(backend.can_handle("https://youtu.be/abc123", false));
        assert!(backend.can_handle("https://www.dailymotion.com/video/abc", false));
        assert!(backend.can_handle("https://svtplay.se/video/abc", false));

        // Not directly supported (raw manifests go to ffmpeg)
        assert!(!backend.can_handle("https://example.com/master.m3u8", false));
        assert!(!backend.can_handle("https://example.com/stream.mpd", false));
    }

    #[test]
    fn test_parse_progress() {
        // Test MiB format
        let line = "[download][stream] Downloaded 10.50 MiB";
        let prog = StreamlinkBackend::parse_progress(line).unwrap();
        assert_eq!(prog.bytes_downloaded, 11_010_048); // 10.5 * 1024 * 1024

        // Test KiB format
        let line = "[download][stream] Downloaded 512.00 KiB";
        let prog = StreamlinkBackend::parse_progress(line).unwrap();
        assert_eq!(prog.bytes_downloaded, 524_288); // 512 * 1024

        // Test non-progress line
        let line = "[cli][info] Found matching plugin twitch";
        assert!(StreamlinkBackend::parse_progress(line).is_none());
    }

    #[test]
    fn test_extra_args() {
        let backend = StreamlinkBackend {
            streamlink_path: "streamlink".to_string(),
            extra_args: vec!["--player-passthrough".to_string(), "hls".to_string()],
        };

        let config = StreamConfig::default();
        let args = backend.build_args_stdout("https://www.twitch.tv/example", &config);

        assert!(args.contains(&"--player-passthrough".to_string()));
        assert!(args.contains(&"hls".to_string()));
    }
}