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
use crate::proxies::{GenericProxyConfig, ProxyConfig, WebshareProxyConfig};
use crate::TranscriptList;
use thiserror::Error;

/// # YouTubeTranscriptApiError
///
/// The base error type for the library.
///
/// This is mainly used as a generic error type for cases that don't fall into
/// more specific error categories.
#[derive(Debug, Error)]
pub enum YouTubeTranscriptApiError {
    #[error("YouTube Transcript API error")]
    Generic,
}

/// # CookieError
///
/// Errors related to cookie handling and authentication.
///
/// These errors occur when there are issues with loading or using cookies
/// for authenticated requests to YouTube.
#[derive(Debug, Error)]
pub enum CookieError {
    #[error("Cookie error")]
    Generic,

    /// Error when the specified cookie file path is invalid or inaccessible
    #[error("Can't load the provided cookie file: {0}")]
    PathInvalid(String),

    /// Error when the cookies are invalid (possibly expired or malformed)
    #[error("The cookies provided are not valid (may have expired): {0}")]
    Invalid(String),
}

/// Type alias for cookie path invalid errors
pub type CookiePathInvalid = CookieError;

/// Type alias for invalid cookie errors
pub type CookieInvalid = CookieError;

/// # CouldNotRetrieveTranscript
///
/// The primary error type when transcript retrieval fails.
///
/// This error provides detailed information about why a transcript couldn't be retrieved,
/// with specific reasons and helpful suggestions for resolving the issue.
///
/// ## Usage Example
///
/// ```rust,no_run
/// # use yt_transcript_rs::YouTubeTranscriptApi;
/// # async fn example() {
/// let api = YouTubeTranscriptApi::new(None, None, None).unwrap();
///
/// match api.fetch_transcript("dQw4w9WgXcQ", &["en"], false).await {
///     Ok(transcript) => {
///         println!("Successfully retrieved transcript");
///     },
///     Err(err) => {
///         // The error message contains detailed information about what went wrong
///         eprintln!("Error: {}", err);
///         
///         // You can also check for specific error types
///         if err.reason.is_some() {
///             match err.reason.as_ref().unwrap() {
///                 // Handle specific error cases
///                 _ => {}
///             }
///         }
///     }
/// }
/// # }
/// ```
#[derive(Debug, Error)]
#[error("{}", self.build_error_message())]
pub struct CouldNotRetrieveTranscript {
    /// The YouTube video ID that was being accessed
    pub video_id: String,

    /// The specific reason why the transcript couldn't be retrieved
    pub reason: Option<CouldNotRetrieveTranscriptReason>,
}

/// # CouldNotRetrieveTranscriptReason
///
/// Detailed reasons why a transcript couldn't be retrieved.
///
/// This enum provides specific information about why transcript retrieval failed,
/// which is useful for error handling and providing helpful feedback to users.
#[derive(Debug)]
pub enum CouldNotRetrieveTranscriptReason {
    /// Subtitles/transcripts are disabled for this video
    TranscriptsDisabled,

    /// No transcript was found in any of the requested languages
    NoTranscriptFound {
        /// The language codes that were requested but not found
        requested_language_codes: Vec<String>,

        /// Information about available transcripts that could be used instead
        transcript_data: TranscriptList,
    },

    /// The video is no longer available (removed, private, etc.)
    VideoUnavailable,

    /// The video cannot be played for some reason
    VideoUnplayable {
        /// The main reason why the video is unplayable
        reason: Option<String>,

        /// Additional details about why the video is unplayable
        sub_reasons: Vec<String>,
    },

    /// YouTube is blocking requests from your IP address
    IpBlocked(Option<Box<dyn ProxyConfig>>),

    /// YouTube is blocking your request (rate limiting, etc.)
    RequestBlocked(Option<Box<dyn ProxyConfig>>),

    /// The requested transcript cannot be translated with specific error details
    TranslationUnavailable(String),

    /// The requested translation language is not available with specific error details
    TranslationLanguageUnavailable(String),

    /// Failed to create a consent cookie required by YouTube
    FailedToCreateConsentCookie,

    /// The request to YouTube failed with a specific error
    YouTubeRequestFailed(String),

    /// The provided video ID is invalid
    InvalidVideoId,

    /// The video is age-restricted and requires authentication
    AgeRestricted,

    /// The YouTube data structure couldn't be parsed
    YouTubeDataUnparsable(String),
}

impl CouldNotRetrieveTranscript {
    /// Builds a detailed error message based on the error reason
    fn build_error_message(&self) -> String {
        let base_error = format!(
            "Could not retrieve a transcript for the video {}!",
            self.video_id.replace("{video_id}", &self.video_id)
        );

        match &self.reason {
            Some(reason) => {
                let cause = match reason {
                    CouldNotRetrieveTranscriptReason::TranscriptsDisabled => {
                        "Subtitles are disabled for this video".to_string()
                    },
                    CouldNotRetrieveTranscriptReason::NoTranscriptFound { requested_language_codes, transcript_data } => {
                        format!("No transcripts were found for any of the requested language codes: {:?}\n\n{}", 
                            requested_language_codes, transcript_data)
                    },
                    CouldNotRetrieveTranscriptReason::VideoUnavailable => {
                        "The video is no longer available".to_string()
                    },
                    CouldNotRetrieveTranscriptReason::VideoUnplayable { reason, sub_reasons } => {
                        let reason_str = reason.clone().unwrap_or_else(|| "No reason specified!".to_string());
                        let mut message = format!("The video is unplayable for the following reason: {}", reason_str);
                        if !sub_reasons.is_empty() {
                            message.push_str("\n\nAdditional Details:\n");
                            for sub_reason in sub_reasons {
                                message.push_str(&format!(" - {}\n", sub_reason));
                            }
                        }
                        message
                    },
                    CouldNotRetrieveTranscriptReason::IpBlocked(proxy_config) => {
                        let base_cause = "YouTube is blocking requests from your IP. This usually is due to one of the following reasons:
- You have done too many requests and your IP has been blocked by YouTube
- You are doing requests from an IP belonging to a cloud provider (like AWS, Google Cloud Platform, Azure, etc.). Unfortunately, most IPs from cloud providers are blocked by YouTube.";
                        match proxy_config {
                            Some(config) if config.as_any().is::<WebshareProxyConfig>() => {
                                format!("{}\n\nYouTube is blocking your requests, despite you using Webshare proxies. Please make sure that you have purchased \"Residential\" proxies and NOT \"Proxy Server\" or \"Static Residential\", as those won't work as reliably! The free tier also uses \"Proxy Server\" and will NOT work!\n\nThe only reliable option is using \"Residential\" proxies (not \"Static Residential\"), as this allows you to rotate through a pool of over 30M IPs, which means you will always find an IP that hasn't been blocked by YouTube yet!", base_cause)
                            },
                            Some(config) if config.as_any().is::<GenericProxyConfig>() => {
                                format!("{}\n\nYouTube is blocking your requests, despite you using proxies. Keep in mind a proxy is just a way to hide your real IP behind the IP of that proxy, but there is no guarantee that the IP of that proxy won't be blocked as well.\n\nThe only truly reliable way to prevent IP blocks is rotating through a large pool of residential IPs, by using a provider like Webshare.", base_cause)
                            },
                            _ => {
                                format!("{}\n\nIp blocked.", base_cause)
                            }
                        }
                    },
                    CouldNotRetrieveTranscriptReason::RequestBlocked(proxy_config) => {
                        let base_cause = "YouTube is blocking requests from your IP. This usually is due to one of the following reasons:
- You have done too many requests and your IP has been blocked by YouTube
- You are doing requests from an IP belonging to a cloud provider (like AWS, Google Cloud Platform, Azure, etc.). Unfortunately, most IPs from cloud providers are blocked by YouTube.";
                        match proxy_config {
                            Some(config) if config.as_any().is::<WebshareProxyConfig>() => {
                                format!("{}\n\nYouTube is blocking your requests, despite you using Webshare proxies. Please make sure that you have purchased \"Residential\" proxies and NOT \"Proxy Server\" or \"Static Residential\", as those won't work as reliably! The free tier also uses \"Proxy Server\" and will NOT work!\n\nThe only reliable option is using \"Residential\" proxies (not \"Static Residential\"), as this allows you to rotate through a pool of over 30M IPs, which means you will always find an IP that hasn't been blocked by YouTube yet!", base_cause)
                            },
                            Some(config) if config.as_any().is::<GenericProxyConfig>() => {
                                format!("{}\n\nYouTube is blocking your requests, despite you using proxies. Keep in mind a proxy is just a way to hide your real IP behind the IP of that proxy, but there is no guarantee that the IP of that proxy won't be blocked as well.\n\nThe only truly reliable way to prevent IP blocks is rotating through a large pool of residential IPs, by using a provider like Webshare.", base_cause)
                            },
                            _ => {
                                format!("{}\n\nRequest blocked.", base_cause)
                            }
                        }
                    },
                    CouldNotRetrieveTranscriptReason::TranslationUnavailable(message) => {
                        format!("The requested transcript cannot be translated: {}", message)
                    },
                    CouldNotRetrieveTranscriptReason::TranslationLanguageUnavailable(message) => {
                        format!("The requested translation language is not available: {}", message)
                    },
                    CouldNotRetrieveTranscriptReason::FailedToCreateConsentCookie => {
                        "Failed to automatically give consent to saving cookies".to_string()
                    },
                    CouldNotRetrieveTranscriptReason::YouTubeRequestFailed(error) => {
                        format!("Failed to make a request to YouTube. Error: {}", error)
                    },
                    CouldNotRetrieveTranscriptReason::InvalidVideoId => {
                        "You provided an invalid video id. Make sure you are using the video id and NOT the url!`".to_string()
                    },
                    CouldNotRetrieveTranscriptReason::AgeRestricted => {
                        "This video is age-restricted. Therefore, you will have to authenticate to be able to retrieve transcripts for it. You will have to provide a cookie to authenticate yourself.".to_string()
                    },
                    CouldNotRetrieveTranscriptReason::YouTubeDataUnparsable(details) => {
                        format!("The data required to fetch the transcript is not parsable: {}. This should not happen, please open an issue (make sure to include the video ID)!", details)
                    },
                };

                format!("{} This is most likely caused by:\n\n{}", base_error, cause)
            }
            None => base_error,
        }
    }
}

/// Type alias for when transcripts are disabled for a video
pub type TranscriptsDisabled = CouldNotRetrieveTranscript;

/// Type alias for when no transcript is found in the requested languages
pub type NoTranscriptFound = CouldNotRetrieveTranscript;

/// Type alias for when the video is no longer available
pub type VideoUnavailable = CouldNotRetrieveTranscript;

/// Type alias for when the video cannot be played
pub type VideoUnplayable = CouldNotRetrieveTranscript;

/// Type alias for when YouTube is blocking your IP address
pub type IpBlocked = CouldNotRetrieveTranscript;

/// Type alias for when YouTube is blocking your request
pub type RequestBlocked = CouldNotRetrieveTranscript;

/// Type alias for when the requested transcript cannot be translated
pub type NotTranslatable = CouldNotRetrieveTranscript;

/// Type alias for when the requested translation language is not available
pub type TranslationLanguageNotAvailable = CouldNotRetrieveTranscript;

/// Type alias for when creating a consent cookie fails
pub type FailedToCreateConsentCookie = CouldNotRetrieveTranscript;

/// Type alias for when a request to YouTube fails
pub type YouTubeRequestFailed = CouldNotRetrieveTranscript;

/// Type alias for when an invalid video ID is provided
pub type InvalidVideoId = CouldNotRetrieveTranscript;

/// Type alias for when the video is age-restricted and requires authentication
pub type AgeRestricted = CouldNotRetrieveTranscript;

/// Type alias for when YouTube data cannot be parsed
pub type YouTubeDataUnparsable = CouldNotRetrieveTranscript;

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

    // Mock implementation for testing
    #[derive(Debug)]
    struct MockProxy;

    impl ProxyConfig for MockProxy {
        fn to_requests_dict(&self) -> HashMap<String, String> {
            HashMap::new()
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[test]
    fn test_build_error_message_no_reason() {
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: None,
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("dQw4w9WgXcQ"));
        // Should be a simple message without additional cause
        assert!(!message.contains("This is most likely caused by"));
    }

    #[test]
    fn test_build_error_message_transcripts_disabled() {
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::TranscriptsDisabled),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("Subtitles are disabled"));
    }

    #[test]
    fn test_build_error_message_no_transcript_found() {
        let transcript_list = TranscriptList {
            video_id: "dQw4w9WgXcQ".to_string(),
            manually_created_transcripts: HashMap::new(),
            generated_transcripts: HashMap::new(),
            translation_languages: vec![],
        };

        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::NoTranscriptFound {
                requested_language_codes: vec!["fr".to_string(), "es".to_string()],
                transcript_data: transcript_list,
            }),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("No transcripts were found"));
        assert!(message.contains("fr"));
        assert!(message.contains("es"));
    }

    #[test]
    fn test_build_error_message_video_unavailable() {
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::VideoUnavailable),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("video is no longer available"));
    }

    #[test]
    fn test_build_error_message_video_unplayable() {
        // Test with reason and sub-reasons
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason: Some("Content is private".to_string()),
                sub_reasons: vec![
                    "The owner has made this content private".to_string(),
                    "You need permission to access".to_string(),
                ],
            }),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("video is unplayable"));
        assert!(message.contains("Content is private"));
        assert!(message.contains("The owner has made this content private"));
        assert!(message.contains("You need permission to access"));

        // Test with no reason (just sub-reasons)
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason: None,
                sub_reasons: vec!["Region restricted".to_string()],
            }),
        };

        let message = error.build_error_message();
        assert!(message.contains("No reason specified"));
        assert!(message.contains("Region restricted"));

        // Test with reason but no sub-reasons
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::VideoUnplayable {
                reason: Some("Premium content".to_string()),
                sub_reasons: vec![],
            }),
        };

        let message = error.build_error_message();
        assert!(message.contains("Premium content"));
        assert!(!message.contains("Additional Details"));
    }

    #[test]
    fn test_build_error_message_ip_blocked() {
        // Without proxy config
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::IpBlocked(None)),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("YouTube is blocking requests from your IP"));
        assert!(message.contains("Ip blocked"));

        // With MockProxy
        let mock_proxy = Box::new(MockProxy);
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::IpBlocked(Some(
                mock_proxy,
            ))),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("YouTube is blocking requests from your IP"));
    }

    #[test]
    fn test_build_error_message_request_blocked() {
        // Without proxy config
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::RequestBlocked(None)),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("YouTube is blocking requests from your IP"));
        assert!(message.contains("Request blocked"));

        // With MockProxy
        let mock_proxy = Box::new(MockProxy);
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::RequestBlocked(Some(
                mock_proxy,
            ))),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("YouTube is blocking requests from your IP"));
    }

    #[test]
    fn test_build_error_message_translation_errors() {
        // TranslationUnavailable
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::TranslationUnavailable(
                "Manual transcripts cannot be translated".to_string(),
            )),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("transcript cannot be translated"));
        assert!(message.contains("Manual transcripts cannot be translated"));

        // TranslationLanguageUnavailable
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(
                CouldNotRetrieveTranscriptReason::TranslationLanguageUnavailable(
                    "Klingon is not supported".to_string(),
                ),
            ),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("translation language is not available"));
        assert!(message.contains("Klingon is not supported"));
    }

    #[test]
    fn test_build_error_message_misc_errors() {
        // FailedToCreateConsentCookie
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::FailedToCreateConsentCookie),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("Failed to automatically give consent"));

        // YouTubeRequestFailed
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::YouTubeRequestFailed(
                "Connection timed out".to_string(),
            )),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("Failed to make a request to YouTube"));
        assert!(message.contains("Connection timed out"));

        // InvalidVideoId
        let error = CouldNotRetrieveTranscript {
            video_id: "invalid".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::InvalidVideoId),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("invalid video id"));

        // AgeRestricted
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::AgeRestricted),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("age-restricted"));
        assert!(message.contains("authenticate"));

        // YouTubeDataUnparsable
        let error = CouldNotRetrieveTranscript {
            video_id: "dQw4w9WgXcQ".to_string(),
            reason: Some(CouldNotRetrieveTranscriptReason::YouTubeDataUnparsable(
                "Invalid XML format".to_string(),
            )),
        };

        let message = error.build_error_message();
        assert!(message.contains("Could not retrieve a transcript"));
        assert!(message.contains("not parsable"));
        assert!(message.contains("open an issue"));
    }
}