Update main.py
Browse files
main.py
CHANGED
|
@@ -32,23 +32,23 @@ logger = logging.getLogger(__name__)
|
|
| 32 |
# Pydantic models
|
| 33 |
class VideoInfoRequest(BaseModel):
|
| 34 |
url: HttpUrl
|
| 35 |
-
use_cookies: bool =
|
| 36 |
|
| 37 |
class BatchVideoInfoRequest(BaseModel):
|
| 38 |
urls: List[HttpUrl]
|
| 39 |
-
use_cookies: bool =
|
| 40 |
|
| 41 |
class DownloadRequest(BaseModel):
|
| 42 |
url: HttpUrl
|
| 43 |
quality: str = "best"
|
| 44 |
audio_only: bool = False
|
| 45 |
-
use_cookies: bool =
|
| 46 |
|
| 47 |
class BatchDownloadRequest(BaseModel):
|
| 48 |
urls: List[HttpUrl]
|
| 49 |
quality: str = "best"
|
| 50 |
audio_only: bool = False
|
| 51 |
-
use_cookies: bool =
|
| 52 |
max_concurrent: int = 2 # Limit concurrent downloads
|
| 53 |
|
| 54 |
class VideoInfo(BaseModel):
|
|
@@ -281,15 +281,18 @@ class BatchYouTubeDownloader:
|
|
| 281 |
|
| 282 |
return cmd
|
| 283 |
|
| 284 |
-
def get_video_info(self, url: str, use_cookies: bool =
|
| 285 |
"""Get video information with cookie support"""
|
| 286 |
max_retries = 3
|
| 287 |
|
|
|
|
|
|
|
|
|
|
| 288 |
try:
|
| 289 |
-
base_cmd = [
|
| 290 |
-
cmd = self._build_command(base_cmd,
|
| 291 |
|
| 292 |
-
logger.info(f"Getting video info (attempt {retry_count + 1}, cookies: {
|
| 293 |
|
| 294 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=60)
|
| 295 |
video_info = json.loads(result.stdout)
|
|
@@ -338,31 +341,34 @@ class BatchYouTubeDownloader:
|
|
| 338 |
return None
|
| 339 |
|
| 340 |
def download_video(self, url: str, quality: str = "best",
|
| 341 |
-
audio_only: bool = False, use_cookies: bool =
|
| 342 |
retry_count: int = 0) -> Optional[str]:
|
| 343 |
"""Download video with cookie support"""
|
| 344 |
max_retries = 2
|
| 345 |
|
|
|
|
|
|
|
|
|
|
| 346 |
try:
|
| 347 |
-
base_cmd = [
|
| 348 |
|
| 349 |
output_template = str(self.download_dir / "%(title)s.%(ext)s")
|
| 350 |
-
base_cmd.extend([
|
| 351 |
|
| 352 |
if audio_only:
|
| 353 |
-
base_cmd.extend([
|
| 354 |
else:
|
| 355 |
if quality == "best":
|
| 356 |
-
base_cmd.extend([
|
| 357 |
elif quality == "worst":
|
| 358 |
-
base_cmd.extend([
|
| 359 |
else:
|
| 360 |
-
base_cmd.extend([
|
| 361 |
|
| 362 |
base_cmd.append(str(url))
|
| 363 |
-
cmd = self._build_command(base_cmd,
|
| 364 |
|
| 365 |
-
logger.info(f"Downloading video (attempt {retry_count + 1}, cookies: {
|
| 366 |
|
| 367 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=300)
|
| 368 |
|
|
|
|
| 32 |
# Pydantic models
|
| 33 |
class VideoInfoRequest(BaseModel):
|
| 34 |
url: HttpUrl
|
| 35 |
+
use_cookies: Optional[bool] = None
|
| 36 |
|
| 37 |
class BatchVideoInfoRequest(BaseModel):
|
| 38 |
urls: List[HttpUrl]
|
| 39 |
+
use_cookies: Optional[bool] = None
|
| 40 |
|
| 41 |
class DownloadRequest(BaseModel):
|
| 42 |
url: HttpUrl
|
| 43 |
quality: str = "best"
|
| 44 |
audio_only: bool = False
|
| 45 |
+
use_cookies: Optional[bool] = None
|
| 46 |
|
| 47 |
class BatchDownloadRequest(BaseModel):
|
| 48 |
urls: List[HttpUrl]
|
| 49 |
quality: str = "best"
|
| 50 |
audio_only: bool = False
|
| 51 |
+
use_cookies: Optional[bool] = None
|
| 52 |
max_concurrent: int = 2 # Limit concurrent downloads
|
| 53 |
|
| 54 |
class VideoInfo(BaseModel):
|
|
|
|
| 281 |
|
| 282 |
return cmd
|
| 283 |
|
| 284 |
+
def get_video_info(self, url: str, use_cookies: Optional[bool] = None, retry_count: int = 0) -> Optional[Dict[str, Any]]:
|
| 285 |
"""Get video information with cookie support"""
|
| 286 |
max_retries = 3
|
| 287 |
|
| 288 |
+
# Determine actual use_cookies value
|
| 289 |
+
actual_use_cookies = use_cookies if use_cookies is not None else self.cookie_manager.get_cookie_path() is not None
|
| 290 |
+
|
| 291 |
try:
|
| 292 |
+
base_cmd = ["yt-dlp", "--dump-json", "--no-download", str(url)]
|
| 293 |
+
cmd = self._build_command(base_cmd, actual_use_cookies)
|
| 294 |
|
| 295 |
+
logger.info(f"Getting video info (attempt {retry_count + 1}, cookies: {actual_use_cookies})")
|
| 296 |
|
| 297 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=60)
|
| 298 |
video_info = json.loads(result.stdout)
|
|
|
|
| 341 |
return None
|
| 342 |
|
| 343 |
def download_video(self, url: str, quality: str = "best",
|
| 344 |
+
audio_only: bool = False, use_cookies: Optional[bool] = None,
|
| 345 |
retry_count: int = 0) -> Optional[str]:
|
| 346 |
"""Download video with cookie support"""
|
| 347 |
max_retries = 2
|
| 348 |
|
| 349 |
+
# Determine actual use_cookies value
|
| 350 |
+
actual_use_cookies = use_cookies if use_cookies is not None else self.cookie_manager.get_cookie_path() is not None
|
| 351 |
+
|
| 352 |
try:
|
| 353 |
+
base_cmd = ["yt-dlp"]
|
| 354 |
|
| 355 |
output_template = str(self.download_dir / "%(title)s.%(ext)s")
|
| 356 |
+
base_cmd.extend(["-o", output_template])
|
| 357 |
|
| 358 |
if audio_only:
|
| 359 |
+
base_cmd.extend(["-f", "bestaudio/best"])
|
| 360 |
else:
|
| 361 |
if quality == "best":
|
| 362 |
+
base_cmd.extend(["-f", "best[height<=720]"])
|
| 363 |
elif quality == "worst":
|
| 364 |
+
base_cmd.extend(["-f", "worst"])
|
| 365 |
else:
|
| 366 |
+
base_cmd.extend(["-f", quality])
|
| 367 |
|
| 368 |
base_cmd.append(str(url))
|
| 369 |
+
cmd = self._build_command(base_cmd, actual_use_cookies)
|
| 370 |
|
| 371 |
+
logger.info(f"Downloading video (attempt {retry_count + 1}, cookies: {actual_use_cookies})")
|
| 372 |
|
| 373 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=300)
|
| 374 |
|