ytdown 0.2.0

A Rust library mirroring yt-dlp's core: extract, select, and download media. Library only — no CLI.
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
//! Lazy continuation-based pagination for YouTube collections.

use futures::stream::{self, BoxStream};
use serde_json::Value;
use std::sync::Arc;

use super::innertube::{BrowseRequest, InnerTube};
use crate::types::{Entry, Thumbnail};

/// Which kind of collection a page belongs to. Renderer paths differ by kind.
///
/// [`PageKind::Search`] carries the original query because continuation pages
/// for search go through the `search` endpoint, which requires it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PageKind {
    /// A playlist (`playlistVideoRenderer`).
    Playlist,
    /// A channel's uploads (`richItemRenderer` / `videoRenderer`).
    Channel,
    /// Search results (`videoRenderer`) for the given query.
    Search(String),
}

/// Maximum number of continuation pages fetched for a single collection.
///
/// A hard cap against an adversarial or buggy InnerTube response that keeps
/// returning continuation tokens forever (including a token cycle). 10k pages of
/// ~100 entries comfortably exceeds any real playlist/channel/search while
/// bounding the work an iterate-to-completion consumer can be made to do.
const MAX_PAGES: u32 = 10_000;

/// Maximum number of *consecutive* continuation pages that may yield zero entries
/// before pagination gives up.
///
/// `MAX_PAGES` and seen-token cycle detection do not stop a server that hands
/// back a FRESH continuation token on every page while never producing an entry:
/// such a stream makes no progress yet defeats cycle detection and could spin up
/// to `MAX_PAGES` fetches. A real collection interleaves entries with its
/// continuations, so a short run of entry-less pages is a reliable "no progress"
/// signal to terminate on.
const MAX_CONSECUTIVE_EMPTY_PAGES: u32 = 3;

/// Per-step state threaded through the [`stream::unfold`] driving pagination.
struct PageState {
    it: Arc<InnerTube>,
    kind: PageKind,
    /// Pending continuation token; `None` once the collection is exhausted.
    next: Option<String>,
    /// Entries parsed from the current page but not yet yielded.
    buffer: std::collections::VecDeque<Entry>,
    /// Whether the first (already-fetched) page still needs parsing.
    first_page: Option<Value>,
    /// Number of continuation pages fetched so far (bounds infinite streams).
    pages_fetched: u32,
    /// Continuation tokens already seen, to break token cycles.
    seen_tokens: std::collections::HashSet<String>,
    /// Consecutive continuation fetches that produced no entries (no-progress guard).
    consecutive_empty: u32,
}

/// Turn an initial browse/search response into a lazy entry stream.
///
/// Drives [`stream::unfold`] over an `Option<continuation_token>`: each step
/// drains buffered entries, and only when the buffer empties does it fetch the
/// next page. The stream ends once no continuation token remains.
pub(crate) fn entry_stream(
    it: Arc<InnerTube>,
    first_page: Value,
    kind: PageKind,
) -> BoxStream<'static, crate::Result<Entry>> {
    let state = PageState {
        it,
        kind,
        next: None,
        buffer: std::collections::VecDeque::new(),
        first_page: Some(first_page),
        pages_fetched: 0,
        seen_tokens: std::collections::HashSet::new(),
        consecutive_empty: 0,
    };

    let stream = stream::unfold(state, |mut state| async move {
        loop {
            // Yield any buffered entry first.
            if let Some(entry) = state.buffer.pop_front() {
                return Some((Ok(entry), state));
            }

            // Parse the initial page lazily on first poll.
            if let Some(page) = state.first_page.take() {
                let (entries, token) = parse_entries(&page, &state.kind);
                state.buffer.extend(entries);
                state.next = token;
                continue;
            }

            // Fetch the next page only when there is a continuation token.
            let token = state.next.take()?;

            // Bound total pages and break continuation-token cycles, defending
            // against adversarial/buggy responses that paginate forever.
            if state.pages_fetched >= MAX_PAGES || !state.seen_tokens.insert(token.clone()) {
                return None;
            }
            state.pages_fetched += 1;

            let fetched = match &state.kind {
                PageKind::Playlist | PageKind::Channel => {
                    state
                        .it
                        .browse(BrowseRequest {
                            continuation: Some(token),
                            ..BrowseRequest::default()
                        })
                        .await
                }
                PageKind::Search(query) => state.it.search(query, Some(&token)).await,
            };
            match fetched {
                Ok(page) => {
                    let (entries, next) = parse_entries(&page, &state.kind);
                    // No-progress guard: a page that yields no entries but keeps
                    // handing back a (fresh) continuation token makes no progress
                    // yet evades both MAX_PAGES (slowly) and cycle detection
                    // (tokens differ). Stop after a short run of empty pages.
                    if entries.is_empty() {
                        state.consecutive_empty += 1;
                        if state.consecutive_empty >= MAX_CONSECUTIVE_EMPTY_PAGES {
                            return None;
                        }
                    } else {
                        state.consecutive_empty = 0;
                    }
                    state.buffer.extend(entries);
                    state.next = next;
                    // Loop: drain the freshly-filled buffer (or stop if empty).
                }
                Err(e) => return Some((Err(e), state)),
            }
        }
    });

    Box::pin(stream)
}

/// Parse the entries and the next continuation token from a single page.
///
/// Renderer layouts vary by collection kind and between the initial page and
/// continuation responses, so this walks the tree recursively: it collects
/// every `playlistVideoRenderer`/`videoRenderer`/`richItemRenderer` it finds
/// and picks up the first `continuationCommand` token anywhere in the tree.
fn parse_entries(page: &Value, _kind: &PageKind) -> (Vec<Entry>, Option<String>) {
    // The recursive scan recognizes every collection kind's renderer, so the
    // `kind` hint is not needed to disambiguate paths in v1; it is retained in
    // the signature for callers and future path-specific optimizations.
    let mut entries = Vec::new();
    let mut token = None;
    collect(page, &mut entries, &mut token);
    (entries, token)
}

/// Recursive walk collecting entries and the continuation token.
fn collect(node: &Value, entries: &mut Vec<Entry>, token: &mut Option<String>) {
    match node {
        Value::Object(map) => {
            for (key, child) in map {
                match key.as_str() {
                    "playlistVideoRenderer" | "videoRenderer" => {
                        if let Some(entry) = parse_renderer(child) {
                            entries.push(entry);
                        }
                    }
                    "richItemRenderer" => {
                        // richItemRenderer wraps a videoRenderer under `content`.
                        if let Some(inner) =
                            child.get("content").and_then(|c| c.get("videoRenderer"))
                        {
                            if let Some(entry) = parse_renderer(inner) {
                                entries.push(entry);
                            }
                        } else {
                            collect(child, entries, token);
                        }
                    }
                    "continuationCommand" => {
                        if token.is_none() {
                            if let Some(t) = child.get("token").and_then(Value::as_str) {
                                *token = Some(t.to_string());
                            }
                        }
                    }
                    _ => collect(child, entries, token),
                }
            }
        }
        Value::Array(items) => {
            for item in items {
                collect(item, entries, token);
            }
        }
        _ => {}
    }
}

/// Build an [`Entry`] from a single `*VideoRenderer` object.
fn parse_renderer(r: &Value) -> Option<Entry> {
    let id = r.get("videoId").and_then(Value::as_str)?.to_string();
    let title = extract_text(r.get("title"));
    let duration = r
        .get("lengthSeconds")
        .and_then(Value::as_str)
        .and_then(|s| s.parse::<u64>().ok())
        .map(std::time::Duration::from_secs);
    let thumbnails = r
        .get("thumbnail")
        .and_then(|t| t.get("thumbnails"))
        .and_then(Value::as_array)
        .map(|arr| arr.iter().filter_map(parse_thumbnail).collect())
        .unwrap_or_default();
    Some(Entry {
        url: format!("https://www.youtube.com/watch?v={id}"),
        id,
        title,
        duration,
        thumbnails,
    })
}

/// Parse one thumbnail object.
fn parse_thumbnail(t: &Value) -> Option<Thumbnail> {
    let url = t.get("url").and_then(Value::as_str)?.to_string();
    Some(Thumbnail {
        url,
        width: t.get("width").and_then(Value::as_u64).map(|w| w as u32),
        height: t.get("height").and_then(Value::as_u64).map(|h| h as u32),
    })
}

/// Extract a display string from a YouTube text node (`{"runs":[{"text":..}]}`
/// or `{"simpleText":..}`).
fn extract_text(node: Option<&Value>) -> Option<String> {
    let node = node?;
    if let Some(simple) = node.get("simpleText").and_then(Value::as_str) {
        return Some(simple.to_string());
    }
    let runs = node.get("runs").and_then(Value::as_array)?;
    let mut out = String::new();
    for run in runs {
        if let Some(t) = run.get("text").and_then(Value::as_str) {
            out.push_str(t);
        }
    }
    if out.is_empty() {
        None
    } else {
        Some(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, Request, ResponseTemplate};

    fn fixture(name: &str) -> Value {
        let raw = std::fs::read_to_string(format!(
            "{}/tests/fixtures/innertube/{name}",
            env!("CARGO_MANIFEST_DIR")
        ))
        .expect("fixture readable");
        serde_json::from_str(&raw).expect("fixture is valid JSON")
    }

    #[tokio::test]
    async fn playlist_stream_paginates_until_no_continuation() {
        let server = MockServer::start().await;

        // The continuation (page 2) browse call returns one entry, no token.
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/browse"))
            .respond_with(|req: &Request| {
                let body: Value = serde_json::from_slice(&req.body).unwrap_or(Value::Null);
                assert_eq!(
                    body.get("continuation").and_then(Value::as_str),
                    Some("CONT_TOKEN_PAGE2"),
                    "continuation token must be forwarded in the browse body"
                );
                ResponseTemplate::new(200)
                    .set_body_json(super::tests::fixture("browse_playlist_page2.json"))
            })
            .expect(1)
            .mount(&server)
            .await;

        let it = Arc::new(InnerTube::with_base_url(
            reqwest::Client::new(),
            server.uri(),
        ));
        let first = fixture("browse_playlist_page1.json");

        let entries: Vec<Entry> = entry_stream(it, first, PageKind::Playlist)
            .map(|r| r.expect("entry"))
            .collect()
            .await;

        let ids: Vec<&str> = entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["aaaaaaaaaaa", "bbbbbbbbbbb", "ccccccccccc"]);
        assert_eq!(
            entries[0].url,
            "https://www.youtube.com/watch?v=aaaaaaaaaaa"
        );
        assert_eq!(entries[0].title.as_deref(), Some("First Video"));
        assert_eq!(
            entries[0].duration,
            Some(std::time::Duration::from_secs(100))
        );
        assert_eq!(
            entries[2].duration,
            Some(std::time::Duration::from_secs(300))
        );
    }

    /// Regression for the unbounded-pagination defect: a response that always
    /// returns the SAME continuation token (a token cycle) must terminate via
    /// cycle detection instead of looping forever.
    #[tokio::test]
    async fn pagination_breaks_continuation_token_cycle() {
        let server = MockServer::start().await;

        // Every browse continuation returns one entry AND the same token again.
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/browse"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "onResponseReceivedActions": [{
                    "appendContinuationItemsAction": {
                        "continuationItems": [
                            { "playlistVideoRenderer": { "videoId": "loopvideo00", "title": { "runs": [{ "text": "Loop" }] } } },
                            { "continuationItemRenderer": { "continuationEndpoint": {
                                "continuationCommand": { "token": "SAME_TOKEN" } } } }
                        ]
                    }
                }]
            })))
            .mount(&server)
            .await;

        let it = Arc::new(InnerTube::with_base_url(
            reqwest::Client::new(),
            server.uri(),
        ));
        // First page yields one entry and the looping token.
        let first = serde_json::json!({
            "contents": { "x": [
                { "playlistVideoRenderer": { "videoId": "firstentry0", "title": { "runs": [{ "text": "First" }] } } },
                { "continuationItemRenderer": { "continuationEndpoint": {
                    "continuationCommand": { "token": "SAME_TOKEN" } } } }
            ]}
        });

        let entries: Vec<Entry> = entry_stream(it, first, PageKind::Playlist)
            .map(|r| r.expect("entry"))
            .collect()
            .await;

        // First page entry + exactly one continuation fetch before the cycle is
        // detected and the stream terminates.
        let ids: Vec<&str> = entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["firstentry0", "loopvideo00"]);
    }

    /// Finding 1: a server that returns NO entries but a FRESH continuation
    /// token on every page makes no progress yet defeats cycle detection (tokens
    /// always differ). The no-progress guard must terminate the stream after a
    /// short run of empty pages instead of spinning up to MAX_PAGES.
    #[tokio::test]
    async fn pagination_stops_on_no_progress_fresh_tokens() {
        let server = MockServer::start().await;
        let hits = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
        let hits_resp = hits.clone();

        // Each continuation page: zero entries, but a brand-new token each time.
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/browse"))
            .respond_with(move |_req: &Request| {
                let n = hits_resp.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "onResponseReceivedActions": [{ "appendContinuationItemsAction": {
                        "continuationItems": [
                            { "continuationItemRenderer": { "continuationEndpoint": {
                                "continuationCommand": { "token": format!("FRESH_{n}") } } } }
                        ]
                    }}]
                }))
            })
            .mount(&server)
            .await;

        let it = Arc::new(InnerTube::with_base_url(
            reqwest::Client::new(),
            server.uri(),
        ));
        // First page: one entry + a fresh continuation token.
        let first = serde_json::json!({
            "contents": { "x": [
                { "playlistVideoRenderer": { "videoId": "firstentry0", "title": { "runs": [{ "text": "First" }] } } },
                { "continuationItemRenderer": { "continuationEndpoint": {
                    "continuationCommand": { "token": "FRESH_START" } } } }
            ]}
        });

        let entries: Vec<Entry> = entry_stream(it, first, PageKind::Playlist)
            .map(|r| r.expect("entry"))
            .collect()
            .await;

        // Only the first-page entry; the empty-page run terminates the stream.
        let ids: Vec<&str> = entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["firstentry0"]);
        // Far fewer than MAX_PAGES: bounded by MAX_CONSECUTIVE_EMPTY_PAGES.
        assert!(
            hits.load(std::sync::atomic::Ordering::SeqCst) <= MAX_CONSECUTIVE_EMPTY_PAGES,
            "no-progress guard must stop after a few empty pages"
        );
    }

    /// Regression for finding 19: search continuation must re-issue via the
    /// search endpoint (query re-sent on page 1, continuation-only on page 2),
    /// and entries from both pages must be yielded.
    #[tokio::test]
    async fn search_stream_paginates_via_search_endpoint() {
        let server = MockServer::start().await;

        // Page 1 (no continuation): videoRenderer + a continuation token.
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/search"))
            .and(|req: &Request| {
                let body: Value = serde_json::from_slice(&req.body).unwrap_or(Value::Null);
                body.get("continuation").is_none() && body["query"] == "rust"
            })
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "contents": { "twoColumnSearchResultsRenderer": { "primaryContents": {
                    "sectionListRenderer": { "contents": [
                        { "itemSectionRenderer": { "contents": [
                            { "videoRenderer": { "videoId": "searchres01", "title": { "runs": [{ "text": "S1" }] } } }
                        ]}},
                        { "continuationItemRenderer": { "continuationEndpoint": {
                            "continuationCommand": { "token": "SEARCH_TOK_2" } } } }
                    ]}
                }}}
            })))
            .mount(&server)
            .await;

        // Page 2 (continuation, query re-sent): videoRenderer, no further token.
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/search"))
            .and(|req: &Request| {
                let body: Value = serde_json::from_slice(&req.body).unwrap_or(Value::Null);
                body.get("continuation").and_then(Value::as_str) == Some("SEARCH_TOK_2")
            })
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "onResponseReceivedCommands": [{ "appendContinuationItemsAction": {
                    "continuationItems": [
                        { "itemSectionRenderer": { "contents": [
                            { "videoRenderer": { "videoId": "searchres02", "title": { "runs": [{ "text": "S2" }] } } }
                        ]}}
                    ]
                }}]
            })))
            .mount(&server)
            .await;

        let it = Arc::new(InnerTube::with_base_url(
            reqwest::Client::new(),
            server.uri(),
        ));
        // First page is fetched up front, like the real extractor does.
        let first = it.search("rust", None).await.unwrap();

        let entries: Vec<Entry> = entry_stream(it, first, PageKind::Search("rust".to_string()))
            .map(|r| r.expect("entry"))
            .collect()
            .await;
        let ids: Vec<&str> = entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["searchres01", "searchres02"]);
    }

    /// Regression for finding 19: the channel `richItemRenderer` -> `videoRenderer`
    /// unwrap path must be exercised and produce entries.
    #[tokio::test]
    async fn channel_stream_unwraps_rich_item_renderer() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/youtubei/v1/browse"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "onResponseReceivedActions": [{ "appendContinuationItemsAction": {
                    "continuationItems": [
                        { "richItemRenderer": { "content": {
                            "videoRenderer": { "videoId": "chanvideo02", "title": { "runs": [{ "text": "C2" }] } }
                        }}}
                    ]
                }}]
            })))
            .mount(&server)
            .await;

        let it = Arc::new(InnerTube::with_base_url(
            reqwest::Client::new(),
            server.uri(),
        ));
        let first = serde_json::json!({
            "contents": { "x": [
                { "richItemRenderer": { "content": {
                    "videoRenderer": { "videoId": "chanvideo01", "title": { "runs": [{ "text": "C1" }] } }
                }}},
                { "continuationItemRenderer": { "continuationEndpoint": {
                    "continuationCommand": { "token": "CHAN_TOK_2" } } } }
            ]}
        });

        let entries: Vec<Entry> = entry_stream(it, first, PageKind::Channel)
            .map(|r| r.expect("entry"))
            .collect()
            .await;
        let ids: Vec<&str> = entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, vec!["chanvideo01", "chanvideo02"]);
    }
}