mirror of
https://github.com/kristoferssolo/tg-relay-rs.git
synced 2025-12-20 11:04:41 +00:00
fix(yt): cookies
This commit is contained in:
parent
5ad4aea252
commit
fa59e0c0e5
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1746,7 +1746,6 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"shlex",
|
|
||||||
"teloxide",
|
"teloxide",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror 2.0.16",
|
"thiserror 2.0.16",
|
||||||
|
|||||||
@ -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"
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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("ed_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`.
|
||||||
|
|||||||
@ -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),
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user