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",
"regex",
"serde",
"shlex",
"teloxide",
"tempfile",
"thiserror 2.0.16",

View File

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

View File

@ -8,9 +8,8 @@ services:
environment:
TELOXIDE_TOKEN: ${TELOXIDE_TOKEN}
COOKIES_PATH: ${COOKIES_PATH:-/app/yt-cookies.txt}
COOKIES: ${COOKIES:-false}
RUST_LOG: ${RUST_LOG:-info}
restart: unless-stopped
volumes:
- ./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
}
/// Download a URL with yt-dlp. `format` can be "best" or a merged selector
/// like "bestvideo[ext=mp4]+bestaudio/best".
/// Download a URL with yt-dlp.
///
/// # 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 format_selector = env::var("YTDLP_FORMAT").unwrap_or_else(|_| default_format.into());
let mut args = vec![
let base_args = [
"--no-playlist",
"--merge-output-format",
"mp4",
"-f",
&format_selector,
// format_selector
"--restrict-filenames",
"-o",
"%(id)s.%(ext)s",
@ -144,22 +146,31 @@ pub async fn download_ytdlp(url: &str, cookies: Option<&str>) -> Result<Download
"--quiet",
];
let with_cookies = env::var("COOKIES")
.unwrap_or_else(|_| "false".into())
.parse::<bool>()
.unwrap_or(false);
if with_cookies && let Some(cookie_path) = cookies {
if Path::new(cookie_path).exists() {
args.extend(["--cookies", cookie_path]);
let mut args = base_args
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
match args.iter().position(|s| s == "-f") {
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 {
warn!("Cookies file not found: {cookie_path}");
warn!("Cookies file not found: {path_str}");
}
}
let quoted_url = shlex::try_quote(url)?;
args.push(&quoted_url);
args.push(url.into());
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`.

View File

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