use anyhow::Result;
use std::env;
use std::path::Path;
use yt_transcript_rs::api::YouTubeTranscriptApi;
use yt_transcript_rs::proxies::{GenericProxyConfig, ProxyConfig};
#[tokio::main]
async fn main() -> Result<()> {
println!("YouTube Transcript API - Advanced Example");
println!("-----------------------------------------");
let cookie_file = env::var("YOUTUBE_COOKIE_FILE").ok();
if let Some(ref path) = cookie_file {
println!("Using cookie file: {}", path);
} else {
println!("No cookie file specified (set YOUTUBE_COOKIE_FILE env var to use one)");
}
let http_proxy = env::var("HTTP_PROXY").ok();
let https_proxy = env::var("HTTPS_PROXY").ok();
let using_proxy = http_proxy.is_some() || https_proxy.is_some();
if using_proxy {
println!("Using proxy configuration from environment variables");
} else {
println!("No proxy configuration found (set HTTP_PROXY/HTTPS_PROXY env vars to use one)");
}
let video_id = env::var("VIDEO_ID").unwrap_or_else(|_| {
if cookie_file.is_some() {
"mQvteoFiMlg".to_string() } else {
"arj7oStGLkU".to_string() }
});
println!("Using video ID: {}", video_id);
let proxy_config = if using_proxy {
match GenericProxyConfig::new(http_proxy.clone(), https_proxy.clone()) {
Ok(config) => {
println!("Successfully created proxy configuration");
Some(Box::new(config) as Box<dyn ProxyConfig + Send + Sync>)
}
Err(e) => {
println!("Failed to create proxy configuration: {}", e);
None
}
}
} else {
None
};
let cookie_path = cookie_file.as_ref().map(Path::new);
let api = YouTubeTranscriptApi::new(cookie_path, proxy_config, None)?;
println!("\nFetching transcript for video ID: {}", video_id);
println!("(The API will handle cookie consent if needed)");
let language_codes = &["en", "es", "fr"]; let preserve_formatting = false;
match api
.fetch_transcript(&video_id, language_codes, preserve_formatting)
.await
{
Ok(transcript) => {
println!("\n✅ Successfully fetched transcript!");
println!("Video ID: {}", transcript.video_id);
println!(
"Language: {} ({})",
transcript.language, transcript.language_code
);
println!("Is auto-generated: {}", transcript.is_generated);
println!("Number of snippets: {}", transcript.snippets.len());
let json_output = serde_json::to_string_pretty(&transcript).unwrap();
println!("\n--- JSON Sample ---");
println!(
"{}",
if json_output.len() > 200 {
format!("{}... (truncated)", &json_output[..200])
} else {
json_output
}
);
}
Err(e) => {
println!("\n❌ Failed to fetch transcript: {:?}", e);
let error_str = format!("{:?}", e);
if error_str.contains("AgeRestricted") {
println!("\nThis video is age-restricted. To access it, you need to:");
println!("1. Export cookies from a browser where you're logged into YouTube");
println!(
"2. Set the YOUTUBE_COOKIE_FILE environment variable to point to your cookie file"
);
println!("3. Run this example again");
} else if error_str.contains("IpBlocked") || error_str.contains("RequestBlocked") {
println!("\nYouTube is blocking requests from your IP address.");
println!("To bypass this restriction, you can:");
println!(
"1. Set up an HTTP proxy by setting the HTTP_PROXY and HTTPS_PROXY environment variables"
);
println!("2. Wait a while before trying again from your current IP");
println!("3. Try using a VPN service");
} else if error_str.contains("FailedToCreateConsentCookie") {
println!("\nCouldn't automatically handle YouTube's cookie consent requirements.");
println!("To resolve this:");
println!(
"1. Export cookies from a browser where you've already accepted YouTube's consent"
);
println!(
"2. Set the YOUTUBE_COOKIE_FILE environment variable to point to your cookie file"
);
} else if error_str.contains("NoTranscriptFound") {
println!("\nNo transcript was found for this video in the requested languages.");
println!("You can:");
println!(
"1. Try different language codes (the example tried: {})",
language_codes.join(", ")
);
println!("2. First check available transcripts using the list() method");
println!(" (see the youtube_transcript_list example)");
}
}
}
Ok(())
}