From b9bdbcb1ebc700725a04cb3d5a768692f1a24074 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 19 Sep 2025 23:47:14 +0300 Subject: [PATCH] fix: add cookie for yt --- .gitignore | 1 + docker-compose.yml | 4 +++- src/download.rs | 13 ++++++++++--- src/handlers/youtube.rs | 5 +++-- src/main.rs | 2 +- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index a95ca69..9f1bf4c 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ target/ .logs/ .env +cookies.txt diff --git a/docker-compose.yml b/docker-compose.yml index 19277b1..3b83042 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,10 @@ services: BINARY_NAME: tg-relay-rs environment: TELOXIDE_TOKEN: ${TELOXIDE_TOKEN} + COOKIES_PATH: /app/yt-cookies.txt RUST_LOG: ${RUST_LOG:-info} restart: unless-stopped # If you need persistent storage (e.g. caching): volumes: - - ./data:/app/data + - ./comments.txt:/app/comments.txt:ro + - ~/.config/yt-cookies.txt:/app/yt-cookies.txt:ro diff --git a/src/download.rs b/src/download.rs index 3b1d54d..6a00224 100644 --- a/src/download.rs +++ b/src/download.rs @@ -88,16 +88,23 @@ pub async fn download_instaloader(shortcode: &str) -> Result { /// # Errors /// /// - Propagates `run_command_in_tempdir` errors. -pub async fn download_ytdlp(url: &str) -> Result { - let args = [ +pub async fn download_ytdlp(url: &str, cookies: Option<&str>) -> Result { + let mut args = vec![ "--no-playlist", "--merge-output-format", "mp4", "--restrict-filenames", "-o", "%(id)s.%(ext)s", - url, ]; + + if let Some(c) = cookies { + args.push("--cookies"); + args.push(c); + } + + args.push(url); + run_command_in_tempdir("yt-dlp", &args).await } diff --git a/src/handlers/youtube.rs b/src/handlers/youtube.rs index 6d17ae1..4501ad0 100644 --- a/src/handlers/youtube.rs +++ b/src/handlers/youtube.rs @@ -3,7 +3,7 @@ use crate::{ error::Result, }; use regex::Regex; -use std::sync::OnceLock; +use std::{env, sync::OnceLock}; use teloxide::{Bot, types::ChatId}; use tracing::info; @@ -44,7 +44,8 @@ impl SocialHandler for YouTubeShortsHandler { async fn handle(&self, bot: &Bot, chat_id: ChatId, url: String) -> Result<()> { info!(handler = %self.name(), url = %url, "handling youtube code"); - let dr = download_ytdlp(&url).await?; + let cookies_path = env::var("COOKIES_PATH"); + let dr = download_ytdlp(&url, cookies_path.as_deref().ok()).await?; process_download_result(bot, chat_id, dr).await } diff --git a/src/main.rs b/src/main.rs index c3030b4..841e69f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,7 @@ async fn main() -> color_eyre::Result<()> { error!(%err, "handler failed"); let _ = bot_for_task - .send_message(chat, "Failed to fetch media, you foking dokey.") + .send_message(chat, "Failed to fetch media, you foking donkey.") .await; } });