Update for coockies use

This commit is contained in:
2025-12-04 00:33:20 +03:00
parent 907d6b521f
commit 9497ad33df
2 changed files with 98 additions and 24 deletions

View File

@@ -83,6 +83,12 @@ async def download_media(
Dictionary with downloaded file information or None
"""
try:
# Log cookies file configuration
if cookies_file:
logger.info(f"Cookies file configured: {cookies_file}")
else:
logger.debug("No cookies file configured")
# URL validation
from bot.utils.helpers import is_valid_url
if not is_valid_url(url):
@@ -174,22 +180,42 @@ async def download_media(
# Add cookies if specified (for Instagram and other sites)
if cookies_file:
# Resolve cookies file path (support relative and absolute paths)
cookies_path = Path(cookies_file)
if not cookies_path.is_absolute():
# If path is relative, search relative to project root
project_root = Path(__file__).parent.parent.parent.parent
cookies_path = project_root / cookies_file
# Also check current working directory
if not cookies_path.exists():
cookies_path = Path(cookies_file).resolve()
cookies_path = None
original_path = Path(cookies_file)
if cookies_path.exists():
# Try multiple locations for cookies file
search_paths = []
# 1. If absolute path, use it directly
if original_path.is_absolute():
search_paths.append(original_path)
else:
# 2. Try relative to project root (where this file is located)
project_root = Path(__file__).parent.parent.parent.parent
search_paths.append(project_root / cookies_file)
# 3. Try relative to current working directory
import os
cwd = Path(os.getcwd())
search_paths.append(cwd / cookies_file)
# 4. Try just the filename in current directory
search_paths.append(Path(cookies_file).resolve())
# Find first existing path
for path in search_paths:
if path.exists() and path.is_file():
cookies_path = path
break
if cookies_path and cookies_path.exists():
ydl_opts['cookiefile'] = str(cookies_path)
logger.info(f"Using cookies from file: {cookies_path}")
logger.info(f"Using cookies from file: {cookies_path} (resolved from: {cookies_file})")
else:
logger.warning(
f"Cookies file not found: {cookies_path} (original path: {cookies_file}). "
f"Continuing without cookies. Check COOKIES_FILE path in configuration."
f"Cookies file not found. Searched in:\n"
f" - {chr(10).join(f' {p}' for p in search_paths)}\n"
f"Original path: {cookies_file}. Continuing without cookies."
)
def run_download():
@@ -334,20 +360,43 @@ async def get_media_info(url: str, cookies_file: Optional[str] = None) -> Option
# Add cookies if specified
if cookies_file:
# Resolve cookies file path (support relative and absolute paths)
cookies_path = Path(cookies_file)
if not cookies_path.is_absolute():
# If path is relative, search relative to project root
project_root = Path(__file__).parent.parent.parent.parent
cookies_path = project_root / cookies_file
# Also check current working directory
if not cookies_path.exists():
cookies_path = Path(cookies_file).resolve()
cookies_path = None
original_path = Path(cookies_file)
if cookies_path.exists():
ydl_opts['cookiefile'] = str(cookies_path)
logger.debug(f"Using cookies to get info: {cookies_path}")
# Try multiple locations for cookies file
search_paths = []
# 1. If absolute path, use it directly
if original_path.is_absolute():
search_paths.append(original_path)
else:
logger.warning(f"Cookies file not found for get_media_info: {cookies_path}")
# 2. Try relative to project root (where this file is located)
project_root = Path(__file__).parent.parent.parent.parent
search_paths.append(project_root / cookies_file)
# 3. Try relative to current working directory
import os
cwd = Path(os.getcwd())
search_paths.append(cwd / cookies_file)
# 4. Try just the filename in current directory
search_paths.append(Path(cookies_file).resolve())
# Find first existing path
for path in search_paths:
if path.exists() and path.is_file():
cookies_path = path
break
if cookies_path and cookies_path.exists():
ydl_opts['cookiefile'] = str(cookies_path)
logger.debug(f"Using cookies to get info: {cookies_path} (resolved from: {cookies_file})")
else:
logger.warning(
f"Cookies file not found for get_media_info. Searched in:\n"
f" - {chr(10).join(f' {p}' for p in search_paths)}\n"
f"Original path: {cookies_file}"
)
def extract_info_sync():
"""Synchronous function for extracting information"""

25
main.py
View File

@@ -38,6 +38,31 @@ async def run_bot():
"""Run Telegram bot"""
logger.info("Starting Telegram bot...")
# Check cookies file if configured
from shared.config import settings
if settings.COOKIES_FILE:
from pathlib import Path
import os
cookies_paths = [
Path(settings.COOKIES_FILE),
Path(__file__).parent / settings.COOKIES_FILE,
Path(os.getcwd()) / settings.COOKIES_FILE,
]
cookies_found = False
for path in cookies_paths:
if path.exists() and path.is_file():
logger.info(f"✅ Cookies file found: {path} (configured as: {settings.COOKIES_FILE})")
cookies_found = True
break
if not cookies_found:
logger.warning(
f"⚠️ Cookies file not found: {settings.COOKIES_FILE}. "
f"Searched in: {[str(p) for p in cookies_paths]}. "
f"Instagram downloads may fail without cookies."
)
else:
logger.info(" No cookies file configured (COOKIES_FILE not set)")
# Initialize database
await init_db()
logger.info("Database initialized")