fix(yt): cookies

This commit is contained in:
Kristofers Solo 2025-09-23 10:50:53 +03:00
parent 5ad4aea252
commit fa59e0c0e5
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
5 changed files with 28 additions and 23 deletions

1
Cargo.lock generated
View File

@ -1746,7 +1746,6 @@ dependencies = [
"rand", "rand",
"regex", "regex",
"serde", "serde",
"shlex",
"teloxide", "teloxide",
"tempfile", "tempfile",
"thiserror 2.0.16", "thiserror 2.0.16",

View File

@ -14,7 +14,6 @@ infer = "0.19"
rand = "0.9" rand = "0.9"
regex = "1.11" regex = "1.11"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
shlex = "1.3.0"
teloxide = { version = "0.17", features = ["macros"] } teloxide = { version = "0.17", features = ["macros"] }
tempfile = "3" tempfile = "3"
thiserror = "2.0" thiserror = "2.0"

View File

@ -8,9 +8,8 @@ services:
environment: environment:
TELOXIDE_TOKEN: ${TELOXIDE_TOKEN} TELOXIDE_TOKEN: ${TELOXIDE_TOKEN}
COOKIES_PATH: ${COOKIES_PATH:-/app/yt-cookies.txt} COOKIES_PATH: ${COOKIES_PATH:-/app/yt-cookies.txt}
COOKIES: ${COOKIES:-false}
RUST_LOG: ${RUST_LOG:-info} RUST_LOG: ${RUST_LOG:-info}
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- ./comments.txt:/app/comments.txt:ro - ./comments.txt:/app/comments.txt:ro
- /etc/secrets/yt-cookies.txt:/app/yt-cookies.txt:ro - /etc/secrets/yt-cookies.txt:/app/yt-cookies.txt

View File

@ -121,22 +121,24 @@ pub async fn download_instaloader(shortcode: &str) -> Result<DownloadResult> {
run_command_in_tempdir("instaloader", &args).await run_command_in_tempdir("instaloader", &args).await
} }
/// Download a URL with yt-dlp. `format` can be "best" or a merged selector /// Download a URL with yt-dlp.
/// like "bestvideo[ext=mp4]+bestaudio/best".
/// ///
/// # Errors /// # Errors
/// ///
/// - Propagates `run_command_in_tempdir` errors. /// - Propagates `run_command_in_tempdir` errors.
pub async fn download_ytdlp(url: &str, cookies: Option<&str>) -> Result<DownloadResult> { pub async fn download_ytdlp<P: AsRef<Path>>(
url: &str,
cookies_path: Option<P>,
) -> Result<DownloadResult> {
let default_format = "bestvideo[ext=mp4][vcodec^=avc1]+bestaudio/best"; let default_format = "bestvideo[ext=mp4][vcodec^=avc1]+bestaudio/best";
let format_selector = env::var("YTDLP_FORMAT").unwrap_or_else(|_| default_format.into()); let format_selector = env::var("YTDLP_FORMAT").unwrap_or_else(|_| default_format.into());
let mut args = vec![ let base_args = [
"--no-playlist", "--no-playlist",
"--merge-output-format", "--merge-output-format",
"mp4", "mp4",
"-f", "-f",
&format_selector, // format_selector
"--restrict-filenames", "--restrict-filenames",
"-o", "-o",
"%(id)s.%(ext)s", "%(id)s.%(ext)s",
@ -144,22 +146,31 @@ pub async fn download_ytdlp(url: &str, cookies: Option<&str>) -> Result<Download
"--quiet", "--quiet",
]; ];
let with_cookies = env::var("COOKIES") let mut args = base_args
.unwrap_or_else(|_| "false".into()) .iter()
.parse::<bool>() .map(ToString::to_string)
.unwrap_or(false); .collect::<Vec<_>>();
if with_cookies && let Some(cookie_path) = cookies {
if Path::new(cookie_path).exists() { match args.iter().position(|s| s == "-f") {
args.extend(["--cookies", cookie_path]); Some(pos) => args.insert(pos + 1, format_selector),
None => args.extend(["-f".into(), format_selector]),
}
if let Some(cookie_path) = cookies_path {
let path = cookie_path.as_ref();
let path_str = path.to_string_lossy().to_string();
if path.exists() {
args.extend(["--cookies".into(), path_str]);
} else { } else {
warn!("Cookies file not found: {cookie_path}"); warn!("Cookies file not found: {path_str}");
} }
} }
let quoted_url = shlex::try_quote(url)?; args.push(url.into());
args.push(&quoted_url);
run_command_in_tempdir("yt-dlp", &args).await let args_ref = args.iter().map(String::as_ref).collect::<Vec<_>>();
run_command_in_tempdir("yt-dlp", &args_ref).await
} }
/// Post-process a `DownloadResult`. /// Post-process a `DownloadResult`.

View File

@ -29,9 +29,6 @@ pub enum Error {
#[error("rate limit exceeded")] #[error("rate limit exceeded")]
RateLimit, RateLimit,
#[error("")]
QuoteError(#[from] shlex::QuoteError),
#[error("other: {0}")] #[error("other: {0}")]
Other(String), Other(String),
} }