yt-transcript-rs 0.1.8

A Rust library for fetching and working with YouTube video transcripts
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/// YouTube video playability status checking.
///
/// This module provides functionality to check if a YouTube video is playable
/// and handles various error conditions such as age restrictions, unavailable videos,
/// region restrictions, and more.
///
/// YouTube returns a playability status for each video which determines whether
/// a video can be viewed. This module parses that status and converts it into
/// appropriate error types that callers can handle.
///
/// Playability status is essential for the transcript API to determine whether
/// it should attempt to fetch transcripts, as many error conditions that affect
/// video playability also affect transcript availability.
use crate::errors::{CouldNotRetrieveTranscript, CouldNotRetrieveTranscriptReason};

/// Responsible for checking if a YouTube video is playable
/// and handling various error conditions (age restriction, unavailable, etc.)
///
/// The `PlayabilityAsserter` examines the player data returned by YouTube
/// and determines if the video can be played. If not, it provides detailed
/// error information about why the video cannot be played.
///
/// # Features
///
/// * Detects age-restricted videos
/// * Identifies unavailable videos (removed, private, etc.)
/// * Extracts detailed error messages from YouTube's response
/// * Converts YouTube playability status to library-specific error types
///
/// # Example
///
/// ```rust,no_run
/// # use yt_transcript_rs::playability_asserter::PlayabilityAsserter;
/// # use serde_json::json;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // JSON data from YouTube player response
/// let player_data = json!({
///     "playabilityStatus": {
///         "status": "OK"
///     }
/// });
///
/// // Check if the video is playable
/// let video_id = "dQw4w9WgXcQ";
/// match PlayabilityAsserter::assert_playability(&player_data, video_id) {
///     Ok(()) => {
///         println!("Video is playable, can fetch transcripts");
///         // Proceed with transcript fetching...
///     },
///     Err(e) => {
///         println!("Video is not playable: {:?}", e.reason);
///         // Handle the error appropriately...
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub struct PlayabilityAsserter;

impl PlayabilityAsserter {
    /// Checks if a video is playable and handles various error conditions.
    ///
    /// This method examines the playability status in the YouTube player data
    /// and returns an appropriate error if the video cannot be played.
    ///
    /// # Parameters
    ///
    /// * `player_data` - JSON data from YouTube's player response
    /// * `video_id` - The YouTube video ID being checked
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the video is playable
    /// * `Err(CouldNotRetrieveTranscript)` - With a specific reason if the video is not playable
    ///
    /// # Error Conditions
    ///
    /// This method returns different error types based on the playability status:
    ///
    /// * `AgeRestricted` - The video is age-restricted and requires login
    /// * `VideoUnavailable` - The video doesn't exist or has been removed
    /// * `VideoUnplayable` - Other reasons with detailed information from YouTube
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use yt_transcript_rs::playability_asserter::PlayabilityAsserter;
    /// # use yt_transcript_rs::errors::CouldNotRetrieveTranscriptReason;
    /// # use serde_json::json;
    /// # fn example() {
    /// // Example of an age-restricted video
    /// let age_restricted_data = json!({
    ///     "playabilityStatus": {
    ///         "status": "LOGIN_REQUIRED",
    ///         "reason": "This video may be inappropriate for some users. Sign in to confirm your age."
    ///     }
    /// });
    ///
    /// let result = PlayabilityAsserter::assert_playability(&age_restricted_data, "restricted_video_id");
    /// assert!(matches!(result,
    ///     Err(e) if matches!(e.reason, Some(CouldNotRetrieveTranscriptReason::AgeRestricted))
    /// ));
    ///
    /// // Example of a normal playable video
    /// let playable_data = json!({
    ///     "playabilityStatus": {
    ///         "status": "OK"
    ///     }
    /// });
    ///
    /// let result = PlayabilityAsserter::assert_playability(&playable_data, "playable_video_id");
    /// assert!(result.is_ok());
    /// # }
    /// ```
    pub fn assert_playability(
        player_data: &serde_json::Value,
        video_id: &str,
    ) -> Result<(), CouldNotRetrieveTranscript> {
        let status = player_data
            .get("playabilityStatus")
            .and_then(|s| s.get("status"))
            .and_then(|s| s.as_str())
            .unwrap_or("ERROR");

        match status {
            "OK" => Ok(()),
            "LOGIN_REQUIRED" => {
                let reason = player_data
                    .get("playabilityStatus")
                    .and_then(|s| s.get("reason"))
                    .and_then(|s| s.as_str())
                    .unwrap_or("");

                if reason.contains("age") {
                    Err(CouldNotRetrieveTranscript {
                        video_id: video_id.to_string(),
                        reason: Some(CouldNotRetrieveTranscriptReason::AgeRestricted),
                    })
                } else {
                    let mut sub_reasons = Vec::new();

                    if let Some(messages) = player_data
                        .get("playabilityStatus")
                        .and_then(|s| s.get("errorScreen"))
                        .and_then(|s| s.get("playerErrorMessageRenderer"))
                        .and_then(|s| s.get("subreason"))
                        .and_then(|s| s.get("runs"))
                        .and_then(|s| s.as_array())
                    {
                        for msg in messages {
                            if let Some(text) = msg.get("text").and_then(|t| t.as_str()) {
                                sub_reasons.push(text.to_string());
                            }
                        }
                    }

                    Err(CouldNotRetrieveTranscript {
                        video_id: video_id.to_string(),
                        reason: Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                            reason: Some(reason.to_string()),
                            sub_reasons,
                        }),
                    })
                }
            }
            // "ERROR" | _ => {
            _ => {
                let reason = player_data
                    .get("playabilityStatus")
                    .and_then(|s| s.get("reason"))
                    .and_then(|s| s.as_str())
                    .unwrap_or("");

                if reason.contains("Video unavailable") {
                    Err(CouldNotRetrieveTranscript {
                        video_id: video_id.to_string(),
                        reason: Some(CouldNotRetrieveTranscriptReason::VideoUnavailable),
                    })
                } else {
                    let mut sub_reasons = Vec::new();

                    if let Some(messages) = player_data
                        .get("playabilityStatus")
                        .and_then(|s| s.get("errorScreen"))
                        .and_then(|s| s.get("playerErrorMessageRenderer"))
                        .and_then(|s| s.get("subreason"))
                        .and_then(|s| s.get("runs"))
                        .and_then(|s| s.as_array())
                    {
                        for msg in messages {
                            if let Some(text) = msg.get("text").and_then(|t| t.as_str()) {
                                sub_reasons.push(text.to_string());
                            }
                        }
                    }

                    Err(CouldNotRetrieveTranscript {
                        video_id: video_id.to_string(),
                        reason: Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                            reason: Some(reason.to_string()),
                            sub_reasons,
                        }),
                    })
                }
            }
        }
    }

    /// Extracts detailed error messages from YouTube's player data.
    ///
    /// This helper method parses the nested JSON structure containing YouTube's
    /// error messages to provide more detailed information about why a video
    /// is unplayable.
    ///
    /// # Parameters
    ///
    /// * `player_data` - JSON data from YouTube's player response
    ///
    /// # Returns
    ///
    /// A vector of strings containing detailed error messages from YouTube.
    /// Returns an empty vector if no detailed messages are found.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use yt_transcript_rs::playability_asserter::PlayabilityAsserter;
    /// # use serde_json::json;
    /// # fn example() {
    /// let player_data = json!({
    ///     "playabilityStatus": {
    ///         "status": "ERROR",
    ///         "reason": "Video unavailable",
    ///         "errorScreen": {
    ///             "playerErrorMessageRenderer": {
    ///                 "subreason": {
    ///                     "runs": [
    ///                         {"text": "This video has been removed by the uploader"},
    ///                         {"text": "Contact the creator for more information"}
    ///                     ]
    ///                 }
    ///             }
    ///         }
    ///     }
    /// });
    ///
    /// let reasons = PlayabilityAsserter::extract_subreasons(&player_data);
    /// assert_eq!(reasons.len(), 2);
    /// assert_eq!(reasons[0], "This video has been removed by the uploader");
    /// # }
    /// ```
    pub fn extract_subreasons(player_data: &serde_json::Value) -> Vec<String> {
        let mut sub_reasons = Vec::new();

        if let Some(messages) = player_data
            .get("playabilityStatus")
            .and_then(|s| s.get("errorScreen"))
            .and_then(|s| s.get("playerErrorMessageRenderer"))
            .and_then(|s| s.get("subreason"))
            .and_then(|s| s.get("runs"))
            .and_then(|s| s.as_array())
        {
            for msg in messages {
                if let Some(text) = msg.get("text").and_then(|t| t.as_str()) {
                    sub_reasons.push(text.to_string());
                }
            }
        }

        sub_reasons
    }
}

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

    #[test]
    fn test_playable_video() {
        // Test a normal playable video
        let video_id = "dQw4w9WgXcQ";
        let player_data = json!({
            "playabilityStatus": {
                "status": "OK"
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_ok());
    }

    #[test]
    fn test_age_restricted_video() {
        // Test an age-restricted video
        let video_id = "age_restricted_video";
        let player_data = json!({
            "playabilityStatus": {
                "status": "LOGIN_REQUIRED",
                "reason": "This video may be inappropriate for some users. Sign in to confirm your age."
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);
        assert!(matches!(
            error.reason,
            Some(CouldNotRetrieveTranscriptReason::AgeRestricted)
        ));
    }

    #[test]
    fn test_unavailable_video() {
        // Test a video that is unavailable
        let video_id = "unavailable_video";
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "Video unavailable",
                "errorScreen": {
                    "playerErrorMessageRenderer": {
                        "subreason": {
                            "runs": [
                                {"text": "This video is no longer available"}
                            ]
                        }
                    }
                }
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);
        assert!(matches!(
            error.reason,
            Some(CouldNotRetrieveTranscriptReason::VideoUnavailable)
        ));
    }

    #[test]
    fn test_unplayable_video() {
        // Test a video that is unplayable for other reasons
        let video_id = "unplayable_video";
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "This video is not available in your country.",
                "errorScreen": {
                    "playerErrorMessageRenderer": {
                        "subreason": {
                            "runs": [
                                {"text": "Due to copyright restrictions"},
                                {"text": "Try using a VPN"}
                            ]
                        }
                    }
                }
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);

        match error.reason {
            Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason,
                sub_reasons,
            }) => {
                assert_eq!(
                    reason,
                    Some("This video is not available in your country.".to_string())
                );
                assert_eq!(sub_reasons.len(), 2);
                assert_eq!(sub_reasons[0], "Due to copyright restrictions");
                assert_eq!(sub_reasons[1], "Try using a VPN");
            }
            _ => panic!(
                "Expected VideoUnplayable reason but got: {:?}",
                error.reason
            ),
        }
    }

    #[test]
    fn test_login_required_non_age() {
        // Test a video requiring login but not for age restriction
        let video_id = "premium_video";
        let player_data = json!({
            "playabilityStatus": {
                "status": "LOGIN_REQUIRED",
                "reason": "This is a premium video. Please sign in to watch."
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);

        match error.reason {
            Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason,
                sub_reasons,
            }) => {
                assert_eq!(
                    reason,
                    Some("This is a premium video. Please sign in to watch.".to_string())
                );
                assert!(sub_reasons.is_empty());
            }
            _ => panic!(
                "Expected VideoUnplayable reason but got: {:?}",
                error.reason
            ),
        }
    }

    #[test]
    fn test_missing_playability_status() {
        // Test with missing playability status data
        let video_id = "missing_status";
        let player_data = json!({
            "otherData": {
                "something": "value"
            }
            // Missing playabilityStatus section
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);

        // Should default to ERROR status and empty reason
        match error.reason {
            Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason,
                sub_reasons,
            }) => {
                assert_eq!(reason, Some("".to_string()));
                assert!(sub_reasons.is_empty());
            }
            _ => panic!(
                "Expected VideoUnplayable reason but got: {:?}",
                error.reason
            ),
        }
    }

    #[test]
    fn test_extract_subreasons_empty() {
        // Test with no subreasons
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "Some error"
                // No errorScreen section
            }
        });

        let reasons = PlayabilityAsserter::extract_subreasons(&player_data);
        assert!(reasons.is_empty());
    }

    #[test]
    fn test_extract_subreasons_single() {
        // Test with a single subreason
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "Some error",
                "errorScreen": {
                    "playerErrorMessageRenderer": {
                        "subreason": {
                            "runs": [
                                {"text": "This is the only reason"}
                            ]
                        }
                    }
                }
            }
        });

        let reasons = PlayabilityAsserter::extract_subreasons(&player_data);
        assert_eq!(reasons.len(), 1);
        assert_eq!(reasons[0], "This is the only reason");
    }

    #[test]
    fn test_extract_subreasons_multiple() {
        // Test with multiple subreasons
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "Some error",
                "errorScreen": {
                    "playerErrorMessageRenderer": {
                        "subreason": {
                            "runs": [
                                {"text": "First reason"},
                                {"text": "Second reason"},
                                {"text": "Third reason"}
                            ]
                        }
                    }
                }
            }
        });

        let reasons = PlayabilityAsserter::extract_subreasons(&player_data);
        assert_eq!(reasons.len(), 3);
        assert_eq!(reasons[0], "First reason");
        assert_eq!(reasons[1], "Second reason");
        assert_eq!(reasons[2], "Third reason");
    }

    #[test]
    fn test_extract_subreasons_malformed() {
        // Test with malformed subreasons structure
        let player_data = json!({
            "playabilityStatus": {
                "status": "ERROR",
                "reason": "Some error",
                "errorScreen": {
                    "playerErrorMessageRenderer": {
                        "subreason": {
                            "runs": [
                                {"not_text": "This should be skipped"},
                                {"text": "This should be included"},
                                null,
                                {"text": null},
                                {"text": "This should also be included"}
                            ]
                        }
                    }
                }
            }
        });

        let reasons = PlayabilityAsserter::extract_subreasons(&player_data);
        assert_eq!(reasons.len(), 2);
        assert_eq!(reasons[0], "This should be included");
        assert_eq!(reasons[1], "This should also be included");
    }

    #[test]
    fn test_different_error_status() {
        // Test with a different error status (not OK or LOGIN_REQUIRED)
        let video_id = "different_error";
        let player_data = json!({
            "playabilityStatus": {
                "status": "CONTENT_CHECK_REQUIRED",
                "reason": "The following content may not be appropriate for some users."
            }
        });

        let result = PlayabilityAsserter::assert_playability(&player_data, video_id);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.video_id, video_id);

        match error.reason {
            Some(CouldNotRetrieveTranscriptReason::VideoUnplayable { reason, .. }) => {
                assert_eq!(
                    reason,
                    Some(
                        "The following content may not be appropriate for some users.".to_string()
                    )
                );
            }
            _ => panic!(
                "Expected VideoUnplayable reason but got: {:?}",
                error.reason
            ),
        }
    }
}