mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
Replaced yt-dlp with FlacFetch for alternate audio sources
This commit is contained in:
16 files changed
+4506
-2616
No files matched your search
@@ -0,0 +1,461 @@
|
||||
"""
|
||||
Audio Fetcher module - abstraction layer for fetching audio files.
|
||||
|
||||
This module provides a clean interface for searching and downloading audio files
|
||||
using flacfetch, replacing the previous direct yt-dlp usage.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class AudioSearchResult:
|
||||
"""Represents a single search result for audio."""
|
||||
|
||||
title: str
|
||||
artist: str
|
||||
url: str
|
||||
provider: str
|
||||
duration: Optional[int] = None # Duration in seconds
|
||||
quality: Optional[str] = None # e.g., "FLAC", "320kbps", etc.
|
||||
source_id: Optional[str] = None # Unique ID from the source
|
||||
# Raw result object from the provider (for download)
|
||||
raw_result: Optional[object] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class AudioFetchResult:
|
||||
"""Result of an audio fetch operation."""
|
||||
|
||||
filepath: str
|
||||
artist: str
|
||||
title: str
|
||||
provider: str
|
||||
duration: Optional[int] = None
|
||||
quality: Optional[str] = None
|
||||
|
||||
|
||||
class AudioFetcherError(Exception):
|
||||
"""Base exception for audio fetcher errors."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NoResultsError(AudioFetcherError):
|
||||
"""Raised when no search results are found."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class DownloadError(AudioFetcherError):
|
||||
"""Raised when download fails."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class AudioFetcher(ABC):
|
||||
"""Abstract base class for audio fetching implementations."""
|
||||
|
||||
@abstractmethod
|
||||
def search(self, artist: str, title: str) -> List[AudioSearchResult]:
|
||||
"""
|
||||
Search for audio matching the given artist and title.
|
||||
|
||||
Args:
|
||||
artist: The artist name to search for
|
||||
title: The track title to search for
|
||||
|
||||
Returns:
|
||||
List of AudioSearchResult objects
|
||||
|
||||
Raises:
|
||||
NoResultsError: If no results are found
|
||||
AudioFetcherError: For other errors
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def download(
|
||||
self,
|
||||
result: AudioSearchResult,
|
||||
output_dir: str,
|
||||
output_filename: Optional[str] = None,
|
||||
) -> AudioFetchResult:
|
||||
"""
|
||||
Download audio from a search result.
|
||||
|
||||
Args:
|
||||
result: The search result to download
|
||||
output_dir: Directory to save the downloaded file
|
||||
output_filename: Optional filename (without extension)
|
||||
|
||||
Returns:
|
||||
AudioFetchResult with the downloaded file path
|
||||
|
||||
Raises:
|
||||
DownloadError: If download fails
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def search_and_download(
|
||||
self,
|
||||
artist: str,
|
||||
title: str,
|
||||
output_dir: str,
|
||||
output_filename: Optional[str] = None,
|
||||
auto_select: bool = False,
|
||||
) -> AudioFetchResult:
|
||||
"""
|
||||
Search for audio and download it in one operation.
|
||||
|
||||
In interactive mode (auto_select=False), this will present options to the user.
|
||||
In auto mode (auto_select=True), this will automatically select the best result.
|
||||
|
||||
Args:
|
||||
artist: The artist name to search for
|
||||
title: The track title to search for
|
||||
output_dir: Directory to save the downloaded file
|
||||
output_filename: Optional filename (without extension)
|
||||
auto_select: If True, automatically select the best result
|
||||
|
||||
Returns:
|
||||
AudioFetchResult with the downloaded file path
|
||||
|
||||
Raises:
|
||||
NoResultsError: If no results are found
|
||||
DownloadError: If download fails
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class FlacFetchAudioFetcher(AudioFetcher):
|
||||
"""
|
||||
Audio fetcher implementation using flacfetch library.
|
||||
|
||||
This provides access to multiple audio sources including private music trackers
|
||||
and YouTube, with intelligent prioritization of high-quality sources.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: Optional[logging.Logger] = None,
|
||||
redacted_api_key: Optional[str] = None,
|
||||
ops_api_key: Optional[str] = None,
|
||||
provider_priority: Optional[List[str]] = None,
|
||||
):
|
||||
"""
|
||||
Initialize the FlacFetch audio fetcher.
|
||||
|
||||
Args:
|
||||
logger: Logger instance for output
|
||||
redacted_api_key: API key for Redacted tracker (optional)
|
||||
ops_api_key: API key for OPS tracker (optional)
|
||||
provider_priority: Custom provider priority order (optional)
|
||||
"""
|
||||
self.logger = logger or logging.getLogger(__name__)
|
||||
self._redacted_api_key = redacted_api_key or os.environ.get("REDACTED_API_KEY")
|
||||
self._ops_api_key = ops_api_key or os.environ.get("OPS_API_KEY")
|
||||
self._provider_priority = provider_priority
|
||||
self._manager = None
|
||||
|
||||
def _get_manager(self):
|
||||
"""Lazily initialize and return the FetchManager."""
|
||||
if self._manager is None:
|
||||
# Import flacfetch here to avoid import errors if not installed
|
||||
from flacfetch.core.manager import FetchManager
|
||||
from flacfetch.providers.youtube import YouTubeProvider
|
||||
|
||||
self._manager = FetchManager()
|
||||
|
||||
# Add providers based on available API keys
|
||||
if self._redacted_api_key:
|
||||
from flacfetch.providers.redacted import RedactedProvider
|
||||
|
||||
self._manager.add_provider(RedactedProvider(api_key=self._redacted_api_key))
|
||||
self.logger.debug("Added Redacted provider")
|
||||
|
||||
if self._ops_api_key:
|
||||
from flacfetch.providers.ops import OPSProvider
|
||||
|
||||
self._manager.add_provider(OPSProvider(api_key=self._ops_api_key))
|
||||
self.logger.debug("Added OPS provider")
|
||||
|
||||
# Always add YouTube as a fallback provider
|
||||
self._manager.add_provider(YouTubeProvider())
|
||||
self.logger.debug("Added YouTube provider")
|
||||
|
||||
return self._manager
|
||||
|
||||
def search(self, artist: str, title: str) -> List[AudioSearchResult]:
|
||||
"""
|
||||
Search for audio matching the given artist and title.
|
||||
|
||||
Args:
|
||||
artist: The artist name to search for
|
||||
title: The track title to search for
|
||||
|
||||
Returns:
|
||||
List of AudioSearchResult objects
|
||||
|
||||
Raises:
|
||||
NoResultsError: If no results are found
|
||||
"""
|
||||
from flacfetch.core.models import TrackQuery
|
||||
|
||||
manager = self._get_manager()
|
||||
query = TrackQuery(artist=artist, title=title)
|
||||
|
||||
self.logger.info(f"Searching for: {artist} - {title}")
|
||||
results = manager.search(query)
|
||||
|
||||
if not results:
|
||||
raise NoResultsError(f"No results found for: {artist} - {title}")
|
||||
|
||||
# Convert to our AudioSearchResult format
|
||||
search_results = []
|
||||
for result in results:
|
||||
search_results.append(
|
||||
AudioSearchResult(
|
||||
title=getattr(result, "title", title),
|
||||
artist=getattr(result, "artist", artist),
|
||||
url=getattr(result, "url", ""),
|
||||
provider=getattr(result, "provider", "Unknown"),
|
||||
duration=getattr(result, "duration", None),
|
||||
quality=getattr(result, "quality", None),
|
||||
source_id=getattr(result, "id", None),
|
||||
raw_result=result,
|
||||
)
|
||||
)
|
||||
|
||||
self.logger.info(f"Found {len(search_results)} results")
|
||||
return search_results
|
||||
|
||||
def download(
|
||||
self,
|
||||
result: AudioSearchResult,
|
||||
output_dir: str,
|
||||
output_filename: Optional[str] = None,
|
||||
) -> AudioFetchResult:
|
||||
"""
|
||||
Download audio from a search result.
|
||||
|
||||
Args:
|
||||
result: The search result to download
|
||||
output_dir: Directory to save the downloaded file
|
||||
output_filename: Optional filename (without extension)
|
||||
|
||||
Returns:
|
||||
AudioFetchResult with the downloaded file path
|
||||
|
||||
Raises:
|
||||
DownloadError: If download fails
|
||||
"""
|
||||
manager = self._get_manager()
|
||||
|
||||
# Ensure output directory exists
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Generate filename if not provided
|
||||
if output_filename is None:
|
||||
output_filename = f"{result.artist} - {result.title}"
|
||||
|
||||
self.logger.info(f"Downloading: {result.artist} - {result.title} from {result.provider}")
|
||||
|
||||
try:
|
||||
# Use flacfetch to download
|
||||
filepath = manager.download(
|
||||
result.raw_result,
|
||||
output_path=output_dir,
|
||||
output_filename=output_filename,
|
||||
)
|
||||
|
||||
if filepath is None:
|
||||
raise DownloadError(f"Download returned no file path for: {result.artist} - {result.title}")
|
||||
|
||||
self.logger.info(f"Downloaded to: {filepath}")
|
||||
|
||||
return AudioFetchResult(
|
||||
filepath=filepath,
|
||||
artist=result.artist,
|
||||
title=result.title,
|
||||
provider=result.provider,
|
||||
duration=result.duration,
|
||||
quality=result.quality,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
raise DownloadError(f"Failed to download {result.artist} - {result.title}: {e}") from e
|
||||
|
||||
def search_and_download(
|
||||
self,
|
||||
artist: str,
|
||||
title: str,
|
||||
output_dir: str,
|
||||
output_filename: Optional[str] = None,
|
||||
auto_select: bool = False,
|
||||
) -> AudioFetchResult:
|
||||
"""
|
||||
Search for audio and download it in one operation.
|
||||
|
||||
In interactive mode (auto_select=False), this will present options to the user.
|
||||
In auto mode (auto_select=True), this will automatically select the best result.
|
||||
|
||||
Args:
|
||||
artist: The artist name to search for
|
||||
title: The track title to search for
|
||||
output_dir: Directory to save the downloaded file
|
||||
output_filename: Optional filename (without extension)
|
||||
auto_select: If True, automatically select the best result
|
||||
|
||||
Returns:
|
||||
AudioFetchResult with the downloaded file path
|
||||
|
||||
Raises:
|
||||
NoResultsError: If no results are found
|
||||
DownloadError: If download fails
|
||||
"""
|
||||
from flacfetch.core.models import TrackQuery
|
||||
|
||||
manager = self._get_manager()
|
||||
query = TrackQuery(artist=artist, title=title)
|
||||
|
||||
self.logger.info(f"Searching for: {artist} - {title}")
|
||||
results = manager.search(query)
|
||||
|
||||
if not results:
|
||||
raise NoResultsError(f"No results found for: {artist} - {title}")
|
||||
|
||||
self.logger.info(f"Found {len(results)} results")
|
||||
|
||||
if auto_select:
|
||||
# Auto mode: select best result based on flacfetch's ranking
|
||||
selected = manager.select_best(results)
|
||||
self.logger.info(f"Auto-selected: {getattr(selected, 'title', title)} from {getattr(selected, 'provider', 'Unknown')}")
|
||||
else:
|
||||
# Interactive mode: present options to user
|
||||
selected = self._interactive_select(results, artist, title)
|
||||
|
||||
if selected is None:
|
||||
raise NoResultsError(f"No result selected for: {artist} - {title}")
|
||||
|
||||
# Ensure output directory exists
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Generate filename if not provided
|
||||
if output_filename is None:
|
||||
output_filename = f"{artist} - {title}"
|
||||
|
||||
self.logger.info(f"Downloading from {getattr(selected, 'provider', 'Unknown')}...")
|
||||
|
||||
try:
|
||||
filepath = manager.download(
|
||||
selected,
|
||||
output_path=output_dir,
|
||||
output_filename=output_filename,
|
||||
)
|
||||
|
||||
if filepath is None:
|
||||
raise DownloadError(f"Download returned no file path for: {artist} - {title}")
|
||||
|
||||
self.logger.info(f"Downloaded to: {filepath}")
|
||||
|
||||
return AudioFetchResult(
|
||||
filepath=filepath,
|
||||
artist=artist,
|
||||
title=title,
|
||||
provider=getattr(selected, "provider", "Unknown"),
|
||||
duration=getattr(selected, "duration", None),
|
||||
quality=getattr(selected, "quality", None),
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
raise DownloadError(f"Failed to download {artist} - {title}: {e}") from e
|
||||
|
||||
def _interactive_select(self, results: list, artist: str, title: str) -> object:
|
||||
"""
|
||||
Present search results to the user for interactive selection.
|
||||
|
||||
Args:
|
||||
results: List of search results from flacfetch
|
||||
artist: The artist name being searched
|
||||
title: The track title being searched
|
||||
|
||||
Returns:
|
||||
The selected result object
|
||||
"""
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"Search Results for: {artist} - {title}")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
for i, result in enumerate(results, 1):
|
||||
provider = getattr(result, "provider", "Unknown")
|
||||
result_title = getattr(result, "title", "Unknown")
|
||||
result_artist = getattr(result, "artist", "Unknown")
|
||||
quality = getattr(result, "quality", "")
|
||||
duration = getattr(result, "duration", None)
|
||||
|
||||
# Format duration if available
|
||||
duration_str = ""
|
||||
if duration:
|
||||
minutes = duration // 60
|
||||
seconds = duration % 60
|
||||
duration_str = f" [{minutes}:{seconds:02d}]"
|
||||
|
||||
quality_str = f" ({quality})" if quality else ""
|
||||
|
||||
print(f" {i}. [{provider}] {result_artist} - {result_title}{quality_str}{duration_str}")
|
||||
|
||||
print()
|
||||
print(" 0. Cancel")
|
||||
print()
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = input("Enter your choice (1-{}, or 0 to cancel): ".format(len(results))).strip()
|
||||
|
||||
if choice == "0":
|
||||
self.logger.info("User cancelled selection")
|
||||
return None
|
||||
|
||||
choice_num = int(choice)
|
||||
if 1 <= choice_num <= len(results):
|
||||
selected = results[choice_num - 1]
|
||||
self.logger.info(f"User selected option {choice_num}")
|
||||
return selected
|
||||
else:
|
||||
print(f"Please enter a number between 0 and {len(results)}")
|
||||
|
||||
except ValueError:
|
||||
print("Please enter a valid number")
|
||||
except KeyboardInterrupt:
|
||||
print("\nCancelled")
|
||||
return None
|
||||
|
||||
|
||||
def create_audio_fetcher(
|
||||
logger: Optional[logging.Logger] = None,
|
||||
redacted_api_key: Optional[str] = None,
|
||||
ops_api_key: Optional[str] = None,
|
||||
) -> AudioFetcher:
|
||||
"""
|
||||
Factory function to create an appropriate AudioFetcher instance.
|
||||
|
||||
Args:
|
||||
logger: Logger instance for output
|
||||
redacted_api_key: API key for Redacted tracker (optional)
|
||||
ops_api_key: API key for OPS tracker (optional)
|
||||
|
||||
Returns:
|
||||
An AudioFetcher instance
|
||||
"""
|
||||
return FlacFetchAudioFetcher(
|
||||
logger=logger,
|
||||
redacted_api_key=redacted_api_key,
|
||||
ops_api_key=ops_api_key,
|
||||
)
|
||||
+32
-59
@@ -3,7 +3,6 @@ import glob
|
||||
import logging
|
||||
import shutil
|
||||
import tempfile
|
||||
import yt_dlp.YoutubeDL as ydl
|
||||
from .utils import sanitize_filename
|
||||
|
||||
|
||||
@@ -39,64 +38,38 @@ class FileHandler:
|
||||
|
||||
return copied_file_name
|
||||
|
||||
def download_video(self, url, output_filename_no_extension, cookies_str=None):
|
||||
self.logger.debug(f"Downloading media from URL {url} to filename {output_filename_no_extension} + (as yet) unknown extension")
|
||||
|
||||
ydl_opts = {
|
||||
"quiet": True,
|
||||
"format": "bv*+ba/b", # if a combined video + audio format is better than the best video-only format use the combined format
|
||||
"outtmpl": f"{output_filename_no_extension}.%(ext)s",
|
||||
# Enhanced anti-detection options
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
# Headers to appear more human
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
}
|
||||
|
||||
# Add cookies if provided
|
||||
if cookies_str:
|
||||
self.logger.info("Using provided cookies for enhanced YouTube download access")
|
||||
# Save cookies to a temporary file
|
||||
import tempfile
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
||||
f.write(cookies_str)
|
||||
ydl_opts['cookiefile'] = f.name
|
||||
else:
|
||||
self.logger.info("No cookies provided for download - attempting standard download")
|
||||
|
||||
try:
|
||||
with ydl(ydl_opts) as ydl_instance:
|
||||
ydl_instance.download([url])
|
||||
|
||||
# Search for the file with any extension
|
||||
downloaded_files = glob.glob(f"{output_filename_no_extension}.*")
|
||||
if downloaded_files:
|
||||
downloaded_file_name = downloaded_files[0] # Assume the first match is the correct one
|
||||
self.logger.info(f"Download finished, returning downloaded filename: {downloaded_file_name}")
|
||||
return downloaded_file_name
|
||||
else:
|
||||
self.logger.error("No files found matching the download pattern.")
|
||||
return None
|
||||
finally:
|
||||
# Clean up temporary cookie file if it was created
|
||||
if cookies_str and 'cookiefile' in ydl_opts:
|
||||
try:
|
||||
import os
|
||||
os.unlink(ydl_opts['cookiefile'])
|
||||
except:
|
||||
pass
|
||||
def download_audio_from_fetcher_result(self, filepath, output_filename_no_extension):
|
||||
"""
|
||||
Handle audio that was downloaded via the AudioFetcher.
|
||||
|
||||
This method copies/moves the downloaded file to the expected location
|
||||
and returns the path with the correct naming convention.
|
||||
|
||||
Args:
|
||||
filepath: Path to the downloaded audio file from AudioFetcher
|
||||
output_filename_no_extension: Desired output filename without extension
|
||||
|
||||
Returns:
|
||||
Path to the renamed/copied audio file
|
||||
"""
|
||||
if not os.path.isfile(filepath):
|
||||
self.logger.error(f"Downloaded file not found: {filepath}")
|
||||
return None
|
||||
|
||||
# Get the extension from the downloaded file
|
||||
ext = os.path.splitext(filepath)[1]
|
||||
target_path = f"{output_filename_no_extension}{ext}"
|
||||
|
||||
# If source and target are the same, no action needed
|
||||
if os.path.abspath(filepath) == os.path.abspath(target_path):
|
||||
self.logger.debug(f"Downloaded file already at target location: {target_path}")
|
||||
return target_path
|
||||
|
||||
# Copy the file to the target location
|
||||
self.logger.debug(f"Copying downloaded file from {filepath} to {target_path}")
|
||||
shutil.copy2(filepath, target_path)
|
||||
|
||||
return target_path
|
||||
|
||||
def extract_still_image_from_video(self, input_filename, output_filename_no_extension):
|
||||
output_filename = output_filename_no_extension + ".png"
|
||||
|
||||
+85
-49
@@ -29,6 +29,7 @@ from .audio_processor import AudioProcessor
|
||||
from .lyrics_processor import LyricsProcessor
|
||||
from .video_generator import VideoGenerator
|
||||
from .video_background_processor import VideoBackgroundProcessor
|
||||
from .audio_fetcher import create_audio_fetcher, AudioFetcherError, NoResultsError
|
||||
|
||||
|
||||
class KaraokePrep:
|
||||
@@ -74,8 +75,8 @@ class KaraokePrep:
|
||||
# Video Background Configuration
|
||||
background_video=None,
|
||||
background_video_darkness=0,
|
||||
# YouTube/Online Configuration
|
||||
cookies_str=None,
|
||||
# Audio Fetcher Configuration
|
||||
auto_download=False,
|
||||
):
|
||||
self.log_level = log_level
|
||||
self.log_formatter = log_formatter
|
||||
@@ -137,8 +138,11 @@ class KaraokePrep:
|
||||
self.background_video = background_video
|
||||
self.background_video_darkness = background_video_darkness
|
||||
|
||||
# YouTube/Online Config
|
||||
self.cookies_str = cookies_str # Passed to metadata extraction and file download
|
||||
# Audio Fetcher Config (replaces yt-dlp)
|
||||
self.auto_download = auto_download # If True, automatically select best audio source
|
||||
|
||||
# Initialize audio fetcher for searching and downloading audio when no input file is provided
|
||||
self.audio_fetcher = create_audio_fetcher(logger=self.logger)
|
||||
|
||||
# Load style parameters using the config module
|
||||
self.style_params = load_style_params(self.style_params_json, self.style_overrides, self.logger)
|
||||
@@ -240,7 +244,7 @@ class KaraokePrep:
|
||||
# Compatibility methods for tests - these call the new functions in metadata.py
|
||||
def extract_info_for_online_media(self, input_url=None, input_artist=None, input_title=None):
|
||||
"""Compatibility method that calls the function in metadata.py"""
|
||||
self.extracted_info = extract_info_for_online_media(input_url, input_artist, input_title, self.logger, self.cookies_str)
|
||||
self.extracted_info = extract_info_for_online_media(input_url, input_artist, input_title, self.logger)
|
||||
return self.extracted_info
|
||||
|
||||
def parse_single_track_metadata(self, input_artist, input_title):
|
||||
@@ -365,46 +369,68 @@ class KaraokePrep:
|
||||
processed_track["input_audio_wav"] = self.file_handler.convert_to_wav(processed_track["input_media"], output_filename_no_extension)
|
||||
|
||||
else:
|
||||
# --- URL or Existing Files Handling ---
|
||||
# --- AudioFetcher or Existing Files Handling ---
|
||||
# Construct patterns using the determined extractor
|
||||
base_pattern = os.path.join(track_output_dir, f"{artist_title} ({self.extractor}*)")
|
||||
input_media_glob = glob.glob(f"{base_pattern}.*webm") + glob.glob(f"{base_pattern}.*mp4") # Add other common formats if needed
|
||||
input_media_glob = glob.glob(f"{base_pattern}.*flac") + glob.glob(f"{base_pattern}.*mp3") + glob.glob(f"{base_pattern}.*wav") + glob.glob(f"{base_pattern}.*webm") + glob.glob(f"{base_pattern}.*mp4")
|
||||
input_png_glob = glob.glob(f"{base_pattern}.png")
|
||||
input_wav_glob = glob.glob(f"{base_pattern}.wav")
|
||||
|
||||
if input_media_glob and input_png_glob and input_wav_glob:
|
||||
if input_media_glob and input_wav_glob:
|
||||
# Existing files found
|
||||
processed_track["input_media"] = input_media_glob[0]
|
||||
processed_track["input_still_image"] = input_png_glob[0]
|
||||
processed_track["input_still_image"] = input_png_glob[0] if input_png_glob else None
|
||||
processed_track["input_audio_wav"] = input_wav_glob[0]
|
||||
self.logger.info(f"Found existing media files matching extractor '{self.extractor}', skipping download/conversion.")
|
||||
# Ensure self.extractor reflects the found files if it was a fallback
|
||||
# Extract the actual extractor string from the filename if needed, though it should match
|
||||
|
||||
elif self.url: # URL provided and files not found, proceed with download
|
||||
# Use media_id if available for better uniqueness
|
||||
filename_suffix = f"{self.extractor} {self.media_id}" if self.media_id else self.extractor
|
||||
output_filename_no_extension = os.path.join(track_output_dir, f"{artist_title} ({filename_suffix})")
|
||||
|
||||
self.logger.info(f"Downloading input media from {self.url}...")
|
||||
# Delegate to FileHandler
|
||||
processed_track["input_media"] = self.file_handler.download_video(self.url, output_filename_no_extension, self.cookies_str)
|
||||
|
||||
self.logger.info("Extracting still image from downloaded media (if input is video)...")
|
||||
# Delegate to FileHandler
|
||||
processed_track["input_still_image"] = self.file_handler.extract_still_image_from_video(
|
||||
processed_track["input_media"], output_filename_no_extension
|
||||
)
|
||||
|
||||
self.logger.info("Converting downloaded video to WAV for audio processing...")
|
||||
# Delegate to FileHandler
|
||||
processed_track["input_audio_wav"] = self.file_handler.convert_to_wav(
|
||||
processed_track["input_media"], output_filename_no_extension
|
||||
)
|
||||
elif getattr(self, '_use_audio_fetcher', False):
|
||||
# Use flacfetch to search and download audio
|
||||
self.logger.info(f"Using flacfetch to search and download: {self.artist} - {self.title}")
|
||||
|
||||
try:
|
||||
# Search and download audio using the AudioFetcher
|
||||
fetch_result = self.audio_fetcher.search_and_download(
|
||||
artist=self.artist,
|
||||
title=self.title,
|
||||
output_dir=track_output_dir,
|
||||
output_filename=f"{artist_title} (flacfetch)",
|
||||
auto_select=self.auto_download,
|
||||
)
|
||||
|
||||
# Update extractor to reflect the actual provider used
|
||||
self.extractor = f"flacfetch-{fetch_result.provider}"
|
||||
|
||||
# Set up the output paths
|
||||
output_filename_no_extension = os.path.join(track_output_dir, f"{artist_title} ({self.extractor})")
|
||||
|
||||
# Copy/move the downloaded file to the expected location
|
||||
processed_track["input_media"] = self.file_handler.download_audio_from_fetcher_result(
|
||||
fetch_result.filepath, output_filename_no_extension
|
||||
)
|
||||
|
||||
self.logger.info(f"Audio downloaded from {fetch_result.provider}: {processed_track['input_media']}")
|
||||
|
||||
# Convert to WAV for audio processing
|
||||
self.logger.info("Converting downloaded audio to WAV for processing...")
|
||||
processed_track["input_audio_wav"] = self.file_handler.convert_to_wav(
|
||||
processed_track["input_media"], output_filename_no_extension
|
||||
)
|
||||
|
||||
# No still image for audio-only downloads
|
||||
processed_track["input_still_image"] = None
|
||||
|
||||
except NoResultsError as e:
|
||||
self.logger.error(f"No audio found: {e}")
|
||||
return None
|
||||
except AudioFetcherError as e:
|
||||
self.logger.error(f"Failed to fetch audio: {e}")
|
||||
return None
|
||||
|
||||
else:
|
||||
# This case means input_media was None, not a URL, and no existing files found
|
||||
self.logger.error(f"Cannot proceed: No input file, no URL, and no existing files found for {artist_title}.")
|
||||
return None
|
||||
# This case means input_media was None, no audio fetcher flag, and no existing files found
|
||||
self.logger.error(f"Cannot proceed: No input file and no existing files found for {artist_title}.")
|
||||
self.logger.error("Please provide a local audio file or use artist+title to search for audio.")
|
||||
return None
|
||||
|
||||
if self.skip_lyrics:
|
||||
self.logger.info("Skipping lyrics fetch as requested.")
|
||||
@@ -853,21 +879,31 @@ class KaraokePrep:
|
||||
self.logger.info(f"Input media {self.input_media} is a local folder, processing each file individually...")
|
||||
return await self.process_folder()
|
||||
elif self.input_media is not None and os.path.isfile(self.input_media):
|
||||
self.logger.info(f"Input media {self.input_media} is a local file, youtube logic will be skipped")
|
||||
self.logger.info(f"Input media {self.input_media} is a local file, audio download will be skipped")
|
||||
return [await self.prep_single_track()]
|
||||
elif self.artist and self.title:
|
||||
# No input file provided - use flacfetch to search and download audio
|
||||
self.logger.info(f"No input file provided, using flacfetch to search for: {self.artist} - {self.title}")
|
||||
|
||||
# Set up the extracted_info for metadata consistency
|
||||
self.extracted_info = {
|
||||
"title": f"{self.artist} - {self.title}",
|
||||
"artist": self.artist,
|
||||
"track_title": self.title,
|
||||
"extractor_key": "flacfetch",
|
||||
"id": f"flacfetch_{self.artist}_{self.title}".replace(" ", "_"),
|
||||
"url": None,
|
||||
"source": "flacfetch",
|
||||
}
|
||||
self.extractor = "flacfetch"
|
||||
self.url = None # URL will be determined by flacfetch
|
||||
|
||||
# Mark that we need to use audio fetcher for download
|
||||
self._use_audio_fetcher = True
|
||||
|
||||
return [await self.prep_single_track()]
|
||||
else:
|
||||
self.url = self.input_media
|
||||
# Use the imported extract_info_for_online_media function
|
||||
self.extracted_info = extract_info_for_online_media(
|
||||
input_url=self.url, input_artist=self.artist, input_title=self.title, logger=self.logger, cookies_str=self.cookies_str
|
||||
raise ValueError(
|
||||
"Either a local file path or both artist and title must be provided. "
|
||||
"URL-based input has been replaced with flacfetch audio fetching."
|
||||
)
|
||||
|
||||
if self.extracted_info and "playlist_count" in self.extracted_info:
|
||||
self.persistent_artist = self.artist
|
||||
self.logger.info(f"Input URL is a playlist, beginning batch operation with persistent artist: {self.persistent_artist}")
|
||||
return await self.process_playlist()
|
||||
else:
|
||||
self.logger.info(f"Input URL is not a playlist, processing single track")
|
||||
# Parse metadata to extract artist and title before processing
|
||||
self.parse_single_track_metadata(self.artist, self.title)
|
||||
return [await self.prep_single_track()]
|
||||
+78
-73
@@ -1,86 +1,67 @@
|
||||
import logging
|
||||
import yt_dlp.YoutubeDL as ydl
|
||||
|
||||
|
||||
def extract_info_for_online_media(input_url, input_artist, input_title, logger, cookies_str=None):
|
||||
"""Extracts metadata using yt-dlp, either from a URL or via search."""
|
||||
"""
|
||||
Creates metadata info dict from provided artist and title.
|
||||
|
||||
Note: This function no longer supports URL-based metadata extraction.
|
||||
Audio search and download is now handled by the AudioFetcher class using flacfetch.
|
||||
|
||||
When both artist and title are provided, this creates a metadata dict that can be
|
||||
used by the rest of the pipeline.
|
||||
|
||||
Args:
|
||||
input_url: Deprecated - URLs should be provided as local file paths or use AudioFetcher
|
||||
input_artist: The artist name
|
||||
input_title: The track title
|
||||
logger: Logger instance
|
||||
cookies_str: Deprecated - no longer used
|
||||
|
||||
Returns:
|
||||
A dict with metadata if artist and title are provided
|
||||
|
||||
Raises:
|
||||
ValueError: If URL is provided (deprecated) or if artist/title are missing
|
||||
"""
|
||||
logger.info(f"Extracting info for input_url: {input_url} input_artist: {input_artist} input_title: {input_title}")
|
||||
|
||||
# Set up yt-dlp options with enhanced anti-detection
|
||||
base_opts = {
|
||||
"quiet": True,
|
||||
# Anti-detection options
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
# Headers to appear more human
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
}
|
||||
# URLs are no longer supported - use AudioFetcher for search and download
|
||||
if input_url is not None:
|
||||
raise ValueError(
|
||||
"URL-based audio fetching has been replaced with flacfetch. "
|
||||
"Please provide a local file path instead, or use artist and title only "
|
||||
"to search for audio via flacfetch."
|
||||
)
|
||||
|
||||
# Add cookies if provided
|
||||
if cookies_str:
|
||||
logger.info("Using provided cookies for enhanced YouTube access")
|
||||
# Save cookies to a temporary file
|
||||
import tempfile
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
||||
f.write(cookies_str)
|
||||
base_opts['cookiefile'] = f.name
|
||||
else:
|
||||
logger.info("No cookies provided - attempting standard extraction")
|
||||
# When artist and title are provided, create a synthetic metadata dict
|
||||
# The actual search and download is handled by AudioFetcher
|
||||
if input_artist and input_title:
|
||||
logger.info(f"Creating metadata for: {input_artist} - {input_title}")
|
||||
return {
|
||||
"title": f"{input_artist} - {input_title}",
|
||||
"artist": input_artist,
|
||||
"track_title": input_title,
|
||||
"extractor_key": "flacfetch",
|
||||
"id": f"flacfetch_{input_artist}_{input_title}".replace(" ", "_"),
|
||||
"url": None, # URL will be determined by flacfetch during download
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
extracted_info = None
|
||||
try:
|
||||
if input_url is not None:
|
||||
# If a URL is provided, use it to extract the metadata
|
||||
with ydl(base_opts) as ydl_instance:
|
||||
extracted_info = ydl_instance.extract_info(input_url, download=False)
|
||||
else:
|
||||
# If no URL is provided, use the query to search for the top result
|
||||
search_opts = base_opts.copy()
|
||||
search_opts.update({
|
||||
"format": "bestaudio",
|
||||
"noplaylist": "True",
|
||||
"extract_flat": True
|
||||
})
|
||||
|
||||
with ydl(search_opts) as ydl_instance:
|
||||
query = f"{input_artist} {input_title}"
|
||||
search_results = ydl_instance.extract_info(f"ytsearch1:{query}", download=False)
|
||||
if search_results and "entries" in search_results and search_results["entries"]:
|
||||
extracted_info = search_results["entries"][0]
|
||||
else:
|
||||
# Raise IndexError to match the expected exception in tests
|
||||
raise IndexError(f"No search results found on YouTube for query: {input_artist} {input_title}")
|
||||
|
||||
if not extracted_info:
|
||||
raise Exception(f"Failed to extract info for query: {input_artist} {input_title} or URL: {input_url}")
|
||||
|
||||
return extracted_info
|
||||
|
||||
finally:
|
||||
# Clean up temporary cookie file if it was created
|
||||
if cookies_str and 'cookiefile' in base_opts:
|
||||
try:
|
||||
import os
|
||||
os.unlink(base_opts['cookiefile'])
|
||||
except:
|
||||
pass
|
||||
# No valid input provided
|
||||
raise ValueError(
|
||||
f"Artist and title are required for audio search. "
|
||||
f"Received artist: {input_artist}, title: {input_title}"
|
||||
)
|
||||
|
||||
|
||||
def parse_track_metadata(extracted_info, current_artist, current_title, persistent_artist, logger):
|
||||
"""
|
||||
Parses extracted_info to determine URL, extractor, ID, artist, and title.
|
||||
Returns a dictionary with the parsed values.
|
||||
|
||||
This function now supports both legacy yt-dlp style metadata and
|
||||
the new flacfetch-based metadata format.
|
||||
"""
|
||||
parsed_data = {
|
||||
"url": None,
|
||||
@@ -93,19 +74,43 @@ def parse_track_metadata(extracted_info, current_artist, current_title, persiste
|
||||
metadata_artist = ""
|
||||
metadata_title = ""
|
||||
|
||||
# Handle flacfetch-style metadata (no URL required)
|
||||
if extracted_info.get("source") == "flacfetch":
|
||||
parsed_data["url"] = None # URL determined at download time
|
||||
parsed_data["extractor"] = "flacfetch"
|
||||
parsed_data["media_id"] = extracted_info.get("id")
|
||||
|
||||
# Use the provided artist/title directly
|
||||
if extracted_info.get("artist"):
|
||||
parsed_data["artist"] = extracted_info["artist"]
|
||||
if extracted_info.get("track_title"):
|
||||
parsed_data["title"] = extracted_info["track_title"]
|
||||
|
||||
if persistent_artist:
|
||||
parsed_data["artist"] = persistent_artist
|
||||
|
||||
logger.info(f"Using flacfetch metadata: artist: {parsed_data['artist']}, title: {parsed_data['title']}")
|
||||
return parsed_data
|
||||
|
||||
# Legacy yt-dlp style metadata handling (for backward compatibility)
|
||||
if "url" in extracted_info:
|
||||
parsed_data["url"] = extracted_info["url"]
|
||||
elif "webpage_url" in extracted_info:
|
||||
parsed_data["url"] = extracted_info["webpage_url"]
|
||||
else:
|
||||
raise Exception(f"Failed to extract URL from input media metadata: {extracted_info}")
|
||||
# For flacfetch results without URL, this is now acceptable
|
||||
logger.debug("No URL in extracted info - will be determined at download time")
|
||||
parsed_data["url"] = None
|
||||
|
||||
if "extractor_key" in extracted_info:
|
||||
parsed_data["extractor"] = extracted_info["extractor_key"]
|
||||
elif "ie_key" in extracted_info:
|
||||
parsed_data["extractor"] = extracted_info["ie_key"]
|
||||
elif extracted_info.get("source") == "flacfetch":
|
||||
parsed_data["extractor"] = "flacfetch"
|
||||
else:
|
||||
raise Exception(f"Failed to find extractor name from input media metadata: {extracted_info}")
|
||||
# Default to flacfetch if no extractor specified
|
||||
parsed_data["extractor"] = "flacfetch"
|
||||
|
||||
if "id" in extracted_info:
|
||||
parsed_data["media_id"] = extracted_info["id"]
|
||||
@@ -147,7 +152,7 @@ def parse_track_metadata(extracted_info, current_artist, current_title, persiste
|
||||
parsed_data["artist"] = persistent_artist
|
||||
|
||||
if parsed_data["artist"] and parsed_data["title"]:
|
||||
logger.info(f"Extracted url: {parsed_data['url']}, artist: {parsed_data['artist']}, title: {parsed_data['title']}")
|
||||
logger.info(f"Parsed metadata - artist: {parsed_data['artist']}, title: {parsed_data['title']}")
|
||||
else:
|
||||
logger.debug(extracted_info)
|
||||
raise Exception("Failed to extract artist and title from the input media metadata.")
|
||||
|
||||
@@ -38,7 +38,7 @@ def create_parser(prog: str = "karaoke-gen") -> argparse.ArgumentParser:
|
||||
parser.add_argument(
|
||||
"args",
|
||||
nargs="*",
|
||||
help="[Media or playlist URL] [Artist] [Title] of song to process. If URL is provided, Artist and Title are optional but increase chance of fetching the correct lyrics. If Artist and Title are provided with no URL, the top YouTube search result will be fetched.",
|
||||
help="[File path] [Artist] [Title] of song to process. If a local audio file is provided, Artist and Title are optional but increase chance of fetching the correct lyrics. If only Artist and Title are provided (no file), audio will be searched and downloaded using flacfetch.",
|
||||
)
|
||||
|
||||
parser.add_argument("-v", "--version", action="version", version=f"%(prog)s {get_version()}")
|
||||
@@ -139,6 +139,14 @@ def create_parser(prog: str = "karaoke-gen") -> argparse.ArgumentParser:
|
||||
help="Optional: output JPG format for title and end images (default: %(default)s). Example: --output_jpg=False",
|
||||
)
|
||||
|
||||
# Audio Fetching Configuration (flacfetch)
|
||||
fetch_group = parser.add_argument_group("Audio Fetching Configuration")
|
||||
fetch_group.add_argument(
|
||||
"--auto-download",
|
||||
action="store_true",
|
||||
help="Optional: Automatically select best audio source when searching by artist/title. When disabled (default), presents options for manual selection. Example: --auto-download",
|
||||
)
|
||||
|
||||
# Audio Processing Configuration
|
||||
audio_group = parser.add_argument_group("Audio Processing Configuration")
|
||||
audio_group.add_argument(
|
||||
|
||||
@@ -105,6 +105,7 @@ async def async_main():
|
||||
style_overrides=style_overrides,
|
||||
background_video=args.background_video,
|
||||
background_video_darkness=args.background_video_darkness,
|
||||
auto_download=getattr(args, 'auto_download', False),
|
||||
)
|
||||
# No await needed for constructor
|
||||
kprep = kprep_coroutine
|
||||
@@ -376,7 +377,10 @@ async def async_main():
|
||||
elif len(args.args) > 1:
|
||||
artist = args.args[0]
|
||||
title = args.args[1]
|
||||
logger.warning(f"No input media provided, the top YouTube search result for {artist} - {title} will be used.")
|
||||
if getattr(args, 'auto_download', False):
|
||||
logger.info(f"No input media provided, flacfetch will automatically search and download: {artist} - {title}")
|
||||
else:
|
||||
logger.info(f"No input media provided, flacfetch will search for: {artist} - {title} (interactive selection)")
|
||||
|
||||
else:
|
||||
parser.print_help()
|
||||
@@ -425,6 +429,7 @@ async def async_main():
|
||||
style_overrides=style_overrides,
|
||||
background_video=args.background_video,
|
||||
background_video_darkness=args.background_video_darkness,
|
||||
auto_download=getattr(args, 'auto_download', False),
|
||||
)
|
||||
# No await needed for constructor
|
||||
kprep = kprep_coroutine
|
||||
|
||||
@@ -831,6 +831,8 @@ def main():
|
||||
ignored_features.append("--existing_instrumental")
|
||||
if args.background_video:
|
||||
ignored_features.append("--background_video")
|
||||
if getattr(args, 'auto_download', False):
|
||||
ignored_features.append("--auto-download (audio search not yet supported)")
|
||||
# These are now supported but server-side handling may be partial
|
||||
if args.organised_dir:
|
||||
ignored_features.append("--organised_dir (local-only)")
|
||||
@@ -877,7 +879,11 @@ def main():
|
||||
elif len(args.args) > 1:
|
||||
artist = args.args[0]
|
||||
title = args.args[1]
|
||||
logger.error("YouTube search is not yet supported in remote mode. Please provide a file path.")
|
||||
logger.error("Audio search (artist+title) is not yet supported in remote mode.")
|
||||
logger.error("Please provide a local audio file path instead.")
|
||||
logger.error("")
|
||||
logger.error("For local flacfetch search, use karaoke-gen instead:")
|
||||
logger.error(f" karaoke-gen \"{artist}\" \"{title}\"")
|
||||
return 1
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
Generated
+2831
-1964
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -15,7 +15,7 @@ python = ">=3.10,<3.14"
|
||||
torch = ">=2.7"
|
||||
requests = ">=2"
|
||||
beautifulsoup4 = ">=4"
|
||||
yt-dlp = "*"
|
||||
flacfetch = ">=0.3"
|
||||
lyricsgenius = ">=3"
|
||||
fetch-lyrics-from-genius = ">=0.1"
|
||||
pillow = ">=10.1"
|
||||
|
||||
@@ -23,11 +23,16 @@ def temp_dir():
|
||||
yield temp_dir
|
||||
|
||||
@pytest.fixture
|
||||
def basic_karaoke_gen(mock_logger, mock_ffmpeg):
|
||||
def basic_karaoke_gen(mock_logger, mock_ffmpeg, temp_dir):
|
||||
"""Return a basic KaraokePrep instance for testing."""
|
||||
with MagicMock() as mock_os_system:
|
||||
# Create a KaraokePrep instance with a mock logger
|
||||
karaoke_gen = KaraokePrep(logger=mock_logger)
|
||||
# Create a KaraokePrep instance with required artist/title and a mock logger
|
||||
karaoke_gen = KaraokePrep(
|
||||
logger=mock_logger,
|
||||
artist="Test Artist",
|
||||
title="Test Title",
|
||||
output_dir=temp_dir,
|
||||
)
|
||||
|
||||
# Replace the ffmpeg_base_command with a mock
|
||||
karaoke_gen.ffmpeg_base_command = "mock_ffmpeg"
|
||||
|
||||
+49
-98
@@ -83,77 +83,55 @@ class TestAsync:
|
||||
assert mock_separate.call_count >= 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prep_single_track_with_url(self, basic_karaoke_gen):
|
||||
"""Test preparing a single track with a URL."""
|
||||
# Setup
|
||||
basic_karaoke_gen.input_media = "https://example.com/video"
|
||||
async def test_prep_single_track_with_flacfetch(self, basic_karaoke_gen, temp_dir):
|
||||
"""Test preparing a single track using flacfetch when no input file provided."""
|
||||
# Setup - enable audio fetcher mode
|
||||
basic_karaoke_gen.input_media = None # No input file
|
||||
basic_karaoke_gen.artist = "Test Artist"
|
||||
basic_karaoke_gen.title = "Test Title"
|
||||
basic_karaoke_gen.url = "https://example.com/video" # Explicitly set URL
|
||||
basic_karaoke_gen.extractor = "youtube" # Explicitly set extractor
|
||||
basic_karaoke_gen.media_id = "12345" # Explicitly set media_id
|
||||
basic_karaoke_gen._use_audio_fetcher = True # Enable flacfetch mode
|
||||
basic_karaoke_gen.auto_download = True # Auto-select best result
|
||||
basic_karaoke_gen.extractor = "flacfetch" # Explicitly set extractor
|
||||
|
||||
# Mock audio fetcher result
|
||||
mock_fetch_result = MagicMock()
|
||||
mock_fetch_result.filepath = os.path.join(temp_dir, "downloaded.flac")
|
||||
mock_fetch_result.provider = "YouTube"
|
||||
mock_fetch_result.duration = 180
|
||||
mock_fetch_result.quality = "FLAC"
|
||||
|
||||
# Mock dependencies
|
||||
with patch('karaoke_gen.metadata.extract_info_for_online_media') as mock_extract, \
|
||||
patch('karaoke_gen.metadata.parse_track_metadata') as mock_parse, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'setup_output_paths', return_value=("output_dir", "Test Artist - Test Title")) as mock_setup_paths, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'download_video', return_value="downloaded_file.mp4") as mock_download, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'extract_still_image_from_video', return_value="still_image.png") as mock_extract_image, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'convert_to_wav', return_value="output.wav") as mock_convert, \
|
||||
with patch.object(basic_karaoke_gen.file_handler, 'setup_output_paths', return_value=(temp_dir, "Test Artist - Test Title")) as mock_setup_paths, \
|
||||
patch.object(basic_karaoke_gen.audio_fetcher, 'search_and_download', return_value=mock_fetch_result) as mock_search_download, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'download_audio_from_fetcher_result', return_value=os.path.join(temp_dir, "downloaded.flac")) as mock_process_fetch, \
|
||||
patch.object(basic_karaoke_gen.file_handler, 'convert_to_wav', return_value=os.path.join(temp_dir, "converted.wav")) as mock_convert, \
|
||||
patch.object(basic_karaoke_gen.file_handler, '_file_exists', return_value=False) as mock_file_exists, \
|
||||
patch.object(basic_karaoke_gen.lyrics_processor, 'transcribe_lyrics', AsyncMock(return_value={'lrc_filepath': 'lyrics.lrc'})) as mock_transcribe, \
|
||||
patch.object(basic_karaoke_gen.audio_processor, 'process_audio_separation', AsyncMock(return_value={'instrumental': 'inst.flac'})) as mock_separate, \
|
||||
patch.object(basic_karaoke_gen.video_generator, 'create_title_video', MagicMock()) as mock_create_title, \
|
||||
patch.object(basic_karaoke_gen.video_generator, 'create_end_video', MagicMock()) as mock_create_end:
|
||||
|
||||
# Configure mock asyncio.gather to return mock results
|
||||
# Configure mocks
|
||||
mock_separate.return_value = {}
|
||||
|
||||
# Configure mock asyncio.create_task to return a mock future
|
||||
mock_future = AsyncMock() # Use AsyncMock for tasks
|
||||
mock_download.return_value = "downloaded_file.mp4"
|
||||
mock_extract_image.return_value = "still_image.png"
|
||||
mock_convert.return_value = "output.wav"
|
||||
mock_create_title.return_value = None
|
||||
mock_create_end.return_value = None
|
||||
mock_future.return_value = {
|
||||
"track_output_dir": "output_dir",
|
||||
"artist": "Test Artist",
|
||||
"title": "Test Title",
|
||||
"input_media": "downloaded_file.mp4",
|
||||
"input_still_image": "still_image.png",
|
||||
"input_audio_wav": "output.wav",
|
||||
"separated_audio": {},
|
||||
"extractor": "youtube",
|
||||
"extracted_info": ANY,
|
||||
"lyrics": None,
|
||||
"processed_lyrics": None,
|
||||
"title_image_png": ANY,
|
||||
"title_image_jpg": ANY,
|
||||
"title_video": ANY,
|
||||
"end_image_png": ANY,
|
||||
"end_image_jpg": ANY,
|
||||
"end_video": ANY,
|
||||
}
|
||||
|
||||
# Configure the mock to return our expected result
|
||||
# No need to mock future.result, the function returns the dict directly
|
||||
|
||||
# Call the method
|
||||
result = await basic_karaoke_gen.prep_single_track()
|
||||
|
||||
# Verify the result structure
|
||||
assert result is not None
|
||||
assert result["artist"] == mock_future.return_value["artist"]
|
||||
assert result["title"] == mock_future.return_value["title"]
|
||||
# assert result["input_media"] == "downloaded_file.mp4"
|
||||
print(f"DEBUG: Actual input_media = {result.get('input_media')}") # Debug print
|
||||
assert result["input_still_image"] == "still_image.png"
|
||||
assert result["input_audio_wav"] == "output.wav"
|
||||
assert result["extractor"].lower() == "youtube"
|
||||
if not isinstance(result["separated_audio"], asyncio.futures.Future) and not asyncio.iscoroutine(result["separated_audio"]):
|
||||
assert result["separated_audio"] == {}
|
||||
# assert result["extractor"].lower() == mock_future.return_value["extractor"].lower() # Case-insensitive compare
|
||||
assert result["artist"] == "Test Artist"
|
||||
assert result["title"] == "Test Title"
|
||||
assert result["input_audio_wav"] == os.path.join(temp_dir, "converted.wav")
|
||||
assert "flacfetch" in result["extractor"]
|
||||
|
||||
# Verify audio fetcher was called
|
||||
mock_search_download.assert_called_once_with(
|
||||
artist="Test Artist",
|
||||
title="Test Title",
|
||||
output_dir=temp_dir,
|
||||
output_filename="Test Artist - Test Title (flacfetch)",
|
||||
auto_select=True,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prep_single_track_with_existing_files(self, basic_karaoke_gen, temp_dir):
|
||||
@@ -557,28 +535,15 @@ class TestAsync:
|
||||
assert result[0] == {"track": "result"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_online_media(self, basic_karaoke_gen):
|
||||
"""Test processing online media."""
|
||||
# Setup
|
||||
basic_karaoke_gen.input_media = "https://example.com/video"
|
||||
async def test_process_with_artist_title_only(self, basic_karaoke_gen):
|
||||
"""Test processing with artist and title only (uses flacfetch)."""
|
||||
# Setup - no input file, just artist and title
|
||||
basic_karaoke_gen.input_media = None # Will trigger flacfetch
|
||||
basic_karaoke_gen.artist = "Test Artist"
|
||||
basic_karaoke_gen.title = "Test Title"
|
||||
|
||||
# Mock dependencies
|
||||
with patch('karaoke_gen.metadata.ydl') as mock_ydl, \
|
||||
patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
|
||||
|
||||
# Configure the mock ydl context manager
|
||||
mock_ydl_instance = MagicMock()
|
||||
mock_ydl_instance.extract_info.return_value = {
|
||||
"title": "Test Video",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
}
|
||||
mock_ydl.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
basic_karaoke_gen.extracted_info = {} # Not a playlist
|
||||
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
|
||||
mock_prep_single_track.return_value = {"track": "result"}
|
||||
|
||||
result = await basic_karaoke_gen.process()
|
||||
@@ -586,36 +551,22 @@ class TestAsync:
|
||||
# Verify prep_single_track was called
|
||||
mock_prep_single_track.assert_called_once()
|
||||
|
||||
# Verify flacfetch mode was enabled
|
||||
assert basic_karaoke_gen._use_audio_fetcher is True
|
||||
assert basic_karaoke_gen.extractor == "flacfetch"
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"track": "result"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_online_playlist(self, basic_karaoke_gen):
|
||||
"""Test processing an online playlist."""
|
||||
# Setup
|
||||
basic_karaoke_gen.input_media = "https://example.com/playlist"
|
||||
async def test_process_missing_artist_or_title_raises_error(self, basic_karaoke_gen):
|
||||
"""Test that processing without artist OR title raises an error."""
|
||||
# Setup - no input file and missing title
|
||||
basic_karaoke_gen.input_media = None # Not a file
|
||||
basic_karaoke_gen.artist = "Test Artist"
|
||||
basic_karaoke_gen.title = "Test Title"
|
||||
basic_karaoke_gen.title = None # Missing title
|
||||
|
||||
# Mock dependencies
|
||||
with patch('karaoke_gen.metadata.ydl') as mock_ydl, \
|
||||
patch.object(basic_karaoke_gen, 'process_playlist', new_callable=AsyncMock) as mock_process_playlist:
|
||||
|
||||
# Configure the mock ydl context manager
|
||||
mock_ydl_instance = MagicMock()
|
||||
mock_ydl_instance.extract_info.return_value = {"playlist_count": 2}
|
||||
mock_ydl.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
basic_karaoke_gen.extracted_info = {"playlist_count": 2} # Is a playlist
|
||||
mock_process_playlist.return_value = [{"track": "result1"}, {"track": "result2"}]
|
||||
|
||||
result = await basic_karaoke_gen.process()
|
||||
|
||||
# Verify process_playlist was called
|
||||
mock_process_playlist.assert_called_once()
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 2
|
||||
assert result[0] == {"track": "result1"}
|
||||
assert result[1] == {"track": "result2"}
|
||||
# Test that ValueError is raised
|
||||
with pytest.raises(ValueError, match="Either a local file path or both artist and title must be provided"):
|
||||
await basic_karaoke_gen.process()
|
||||
@@ -0,0 +1,539 @@
|
||||
"""
|
||||
Unit tests for audio_fetcher module.
|
||||
|
||||
Tests cover the AudioFetcher abstraction layer and FlacFetchAudioFetcher implementation.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import os
|
||||
import tempfile
|
||||
import logging
|
||||
from unittest.mock import MagicMock, patch, PropertyMock
|
||||
|
||||
from karaoke_gen.audio_fetcher import (
|
||||
AudioSearchResult,
|
||||
AudioFetchResult,
|
||||
AudioFetcher,
|
||||
AudioFetcherError,
|
||||
NoResultsError,
|
||||
DownloadError,
|
||||
FlacFetchAudioFetcher,
|
||||
create_audio_fetcher,
|
||||
)
|
||||
|
||||
|
||||
class TestAudioSearchResult:
|
||||
"""Tests for AudioSearchResult dataclass."""
|
||||
|
||||
def test_basic_creation(self):
|
||||
"""Test creating an AudioSearchResult with required fields."""
|
||||
result = AudioSearchResult(
|
||||
title="Test Song",
|
||||
artist="Test Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
)
|
||||
assert result.title == "Test Song"
|
||||
assert result.artist == "Test Artist"
|
||||
assert result.url == "https://example.com/song"
|
||||
assert result.provider == "YouTube"
|
||||
assert result.duration is None
|
||||
assert result.quality is None
|
||||
assert result.source_id is None
|
||||
assert result.raw_result is None
|
||||
|
||||
def test_full_creation(self):
|
||||
"""Test creating an AudioSearchResult with all fields."""
|
||||
raw = {"some": "data"}
|
||||
result = AudioSearchResult(
|
||||
title="Test Song",
|
||||
artist="Test Artist",
|
||||
url="https://example.com/song",
|
||||
provider="Redacted",
|
||||
duration=240,
|
||||
quality="FLAC",
|
||||
source_id="123456",
|
||||
raw_result=raw,
|
||||
)
|
||||
assert result.duration == 240
|
||||
assert result.quality == "FLAC"
|
||||
assert result.source_id == "123456"
|
||||
assert result.raw_result == raw
|
||||
|
||||
|
||||
class TestAudioFetchResult:
|
||||
"""Tests for AudioFetchResult dataclass."""
|
||||
|
||||
def test_basic_creation(self):
|
||||
"""Test creating an AudioFetchResult with required fields."""
|
||||
result = AudioFetchResult(
|
||||
filepath="/path/to/song.flac",
|
||||
artist="Test Artist",
|
||||
title="Test Song",
|
||||
provider="YouTube",
|
||||
)
|
||||
assert result.filepath == "/path/to/song.flac"
|
||||
assert result.artist == "Test Artist"
|
||||
assert result.title == "Test Song"
|
||||
assert result.provider == "YouTube"
|
||||
assert result.duration is None
|
||||
assert result.quality is None
|
||||
|
||||
def test_full_creation(self):
|
||||
"""Test creating an AudioFetchResult with all fields."""
|
||||
result = AudioFetchResult(
|
||||
filepath="/path/to/song.flac",
|
||||
artist="Test Artist",
|
||||
title="Test Song",
|
||||
provider="Redacted",
|
||||
duration=180,
|
||||
quality="FLAC 24bit",
|
||||
)
|
||||
assert result.duration == 180
|
||||
assert result.quality == "FLAC 24bit"
|
||||
|
||||
|
||||
class TestExceptions:
|
||||
"""Tests for custom exceptions."""
|
||||
|
||||
def test_audio_fetcher_error(self):
|
||||
"""Test AudioFetcherError can be raised and caught."""
|
||||
with pytest.raises(AudioFetcherError) as exc_info:
|
||||
raise AudioFetcherError("Test error")
|
||||
assert str(exc_info.value) == "Test error"
|
||||
|
||||
def test_no_results_error(self):
|
||||
"""Test NoResultsError inherits from AudioFetcherError."""
|
||||
with pytest.raises(AudioFetcherError):
|
||||
raise NoResultsError("No results found")
|
||||
|
||||
with pytest.raises(NoResultsError) as exc_info:
|
||||
raise NoResultsError("No results found")
|
||||
assert str(exc_info.value) == "No results found"
|
||||
|
||||
def test_download_error(self):
|
||||
"""Test DownloadError inherits from AudioFetcherError."""
|
||||
with pytest.raises(AudioFetcherError):
|
||||
raise DownloadError("Download failed")
|
||||
|
||||
with pytest.raises(DownloadError) as exc_info:
|
||||
raise DownloadError("Download failed")
|
||||
assert str(exc_info.value) == "Download failed"
|
||||
|
||||
|
||||
class TestFlacFetchAudioFetcher:
|
||||
"""Tests for FlacFetchAudioFetcher implementation."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_logger(self):
|
||||
"""Create a mock logger."""
|
||||
return MagicMock(spec=logging.Logger)
|
||||
|
||||
@pytest.fixture
|
||||
def fetcher(self, mock_logger):
|
||||
"""Create a FlacFetchAudioFetcher instance with mocked dependencies."""
|
||||
return FlacFetchAudioFetcher(logger=mock_logger)
|
||||
|
||||
@patch.dict(os.environ, {}, clear=True)
|
||||
def test_init_default(self):
|
||||
"""Test default initialization without environment variables."""
|
||||
# Clear env vars to test default behavior
|
||||
with patch.dict(os.environ, {"REDACTED_API_KEY": "", "OPS_API_KEY": ""}, clear=False):
|
||||
fetcher = FlacFetchAudioFetcher()
|
||||
assert fetcher._manager is None
|
||||
|
||||
def test_init_with_api_keys(self, mock_logger):
|
||||
"""Test initialization with API keys."""
|
||||
fetcher = FlacFetchAudioFetcher(
|
||||
logger=mock_logger,
|
||||
redacted_api_key="red_key",
|
||||
ops_api_key="ops_key",
|
||||
)
|
||||
assert fetcher._redacted_api_key == "red_key"
|
||||
assert fetcher._ops_api_key == "ops_key"
|
||||
|
||||
@patch.dict(os.environ, {"REDACTED_API_KEY": "env_red_key", "OPS_API_KEY": "env_ops_key"})
|
||||
def test_init_from_environment(self, mock_logger):
|
||||
"""Test initialization reads API keys from environment."""
|
||||
fetcher = FlacFetchAudioFetcher(logger=mock_logger)
|
||||
assert fetcher._redacted_api_key == "env_red_key"
|
||||
assert fetcher._ops_api_key == "env_ops_key"
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_search_success(self, mock_get_manager, fetcher):
|
||||
"""Test successful search returns results."""
|
||||
# Set up mock
|
||||
mock_manager = MagicMock()
|
||||
mock_result = MagicMock()
|
||||
mock_result.title = "Found Song"
|
||||
mock_result.artist = "Found Artist"
|
||||
mock_result.url = "https://example.com/song"
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.duration = 200
|
||||
mock_result.quality = "320kbps"
|
||||
mock_result.id = "abc123"
|
||||
mock_manager.search.return_value = [mock_result]
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
# Execute
|
||||
results = fetcher.search("Test Artist", "Test Song")
|
||||
|
||||
# Verify
|
||||
assert len(results) == 1
|
||||
assert results[0].title == "Found Song"
|
||||
assert results[0].artist == "Found Artist"
|
||||
assert results[0].provider == "YouTube"
|
||||
assert results[0].raw_result == mock_result
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_search_no_results(self, mock_get_manager, fetcher):
|
||||
"""Test search raises NoResultsError when no results found."""
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.search.return_value = []
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
with pytest.raises(NoResultsError) as exc_info:
|
||||
fetcher.search("Unknown Artist", "Unknown Song")
|
||||
|
||||
assert "No results found" in str(exc_info.value)
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_success(self, mock_get_manager, fetcher, tmp_path):
|
||||
"""Test successful download returns AudioFetchResult."""
|
||||
# Set up mock
|
||||
mock_manager = MagicMock()
|
||||
downloaded_path = str(tmp_path / "song.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
# Create search result
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Test Song",
|
||||
artist="Test Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
duration=180,
|
||||
quality="FLAC",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
# Execute
|
||||
result = fetcher.download(search_result, str(tmp_path))
|
||||
|
||||
# Verify
|
||||
assert result.filepath == downloaded_path
|
||||
assert result.artist == "Test Artist"
|
||||
assert result.title == "Test Song"
|
||||
assert result.provider == "YouTube"
|
||||
mock_manager.download.assert_called_once()
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_failure(self, mock_get_manager, fetcher, tmp_path):
|
||||
"""Test download raises DownloadError on failure."""
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.download.return_value = None
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Test Song",
|
||||
artist="Test Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
with pytest.raises(DownloadError) as exc_info:
|
||||
fetcher.download(search_result, str(tmp_path))
|
||||
|
||||
assert "Download returned no file path" in str(exc_info.value)
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_exception(self, mock_get_manager, fetcher, tmp_path):
|
||||
"""Test download wraps exceptions in DownloadError."""
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.download.side_effect = Exception("Network error")
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Test Song",
|
||||
artist="Test Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
with pytest.raises(DownloadError) as exc_info:
|
||||
fetcher.download(search_result, str(tmp_path))
|
||||
|
||||
assert "Failed to download" in str(exc_info.value)
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_search_and_download_auto_select(self, mock_get_manager, fetcher, tmp_path):
|
||||
"""Test search_and_download with auto_select=True uses select_best."""
|
||||
# Set up mock
|
||||
mock_manager = MagicMock()
|
||||
mock_result = MagicMock()
|
||||
mock_result.title = "Test Song"
|
||||
mock_result.artist = "Test Artist"
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.duration = 180
|
||||
mock_result.quality = "FLAC"
|
||||
mock_manager.search.return_value = [mock_result]
|
||||
mock_manager.select_best.return_value = mock_result
|
||||
downloaded_path = str(tmp_path / "song.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
# Execute
|
||||
result = fetcher.search_and_download(
|
||||
artist="Test Artist",
|
||||
title="Test Song",
|
||||
output_dir=str(tmp_path),
|
||||
auto_select=True,
|
||||
)
|
||||
|
||||
# Verify
|
||||
mock_manager.select_best.assert_called_once()
|
||||
assert result.filepath == downloaded_path
|
||||
assert result.artist == "Test Artist"
|
||||
assert result.title == "Test Song"
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
@patch('builtins.input', return_value='1')
|
||||
def test_search_and_download_interactive_select(self, mock_input, mock_get_manager, fetcher, tmp_path, capsys):
|
||||
"""Test search_and_download with interactive selection."""
|
||||
# Set up mock
|
||||
mock_manager = MagicMock()
|
||||
mock_result = MagicMock()
|
||||
mock_result.title = "Test Song"
|
||||
mock_result.artist = "Test Artist"
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.duration = 180
|
||||
mock_result.quality = "320kbps"
|
||||
mock_manager.search.return_value = [mock_result]
|
||||
downloaded_path = str(tmp_path / "song.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
# Execute
|
||||
result = fetcher.search_and_download(
|
||||
artist="Test Artist",
|
||||
title="Test Song",
|
||||
output_dir=str(tmp_path),
|
||||
auto_select=False,
|
||||
)
|
||||
|
||||
# Verify
|
||||
mock_manager.select_best.assert_not_called()
|
||||
assert result.filepath == downloaded_path
|
||||
|
||||
# Check output shows options
|
||||
captured = capsys.readouterr()
|
||||
assert "Search Results" in captured.out
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_search_and_download_no_results(self, mock_get_manager, fetcher, tmp_path):
|
||||
"""Test search_and_download raises NoResultsError when no results."""
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.search.return_value = []
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
with pytest.raises(NoResultsError):
|
||||
fetcher.search_and_download(
|
||||
artist="Unknown",
|
||||
title="Unknown",
|
||||
output_dir=str(tmp_path),
|
||||
auto_select=True,
|
||||
)
|
||||
|
||||
@patch('builtins.input', return_value='0')
|
||||
def test_interactive_select_cancel(self, mock_input, fetcher):
|
||||
"""Test interactive selection returns None when user cancels."""
|
||||
mock_result = MagicMock()
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.title = "Test"
|
||||
mock_result.artist = "Artist"
|
||||
mock_result.quality = "FLAC"
|
||||
mock_result.duration = 180
|
||||
|
||||
result = fetcher._interactive_select([mock_result], "Artist", "Test")
|
||||
assert result is None
|
||||
|
||||
@patch('builtins.input', side_effect=['invalid', '1'])
|
||||
def test_interactive_select_invalid_then_valid(self, mock_input, fetcher):
|
||||
"""Test interactive selection handles invalid input gracefully."""
|
||||
mock_result = MagicMock()
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.title = "Test"
|
||||
mock_result.artist = "Artist"
|
||||
mock_result.quality = "FLAC"
|
||||
mock_result.duration = 180
|
||||
|
||||
result = fetcher._interactive_select([mock_result], "Artist", "Test")
|
||||
assert result == mock_result
|
||||
|
||||
@patch('builtins.input', side_effect=['5', '1'])
|
||||
def test_interactive_select_out_of_range_then_valid(self, mock_input, fetcher, capsys):
|
||||
"""Test interactive selection handles out-of-range input."""
|
||||
mock_result = MagicMock()
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.title = "Test"
|
||||
mock_result.artist = "Artist"
|
||||
mock_result.quality = "FLAC"
|
||||
mock_result.duration = 180
|
||||
|
||||
result = fetcher._interactive_select([mock_result], "Artist", "Test")
|
||||
assert result == mock_result
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Please enter a number between" in captured.out
|
||||
|
||||
@patch('builtins.input', side_effect=KeyboardInterrupt)
|
||||
def test_interactive_select_keyboard_interrupt(self, mock_input, fetcher, capsys):
|
||||
"""Test interactive selection handles keyboard interrupt."""
|
||||
mock_result = MagicMock()
|
||||
mock_result.provider = "YouTube"
|
||||
mock_result.title = "Test"
|
||||
mock_result.artist = "Artist"
|
||||
mock_result.quality = None
|
||||
mock_result.duration = None
|
||||
|
||||
result = fetcher._interactive_select([mock_result], "Artist", "Test")
|
||||
assert result is None
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out
|
||||
|
||||
|
||||
class TestCreateAudioFetcher:
|
||||
"""Tests for create_audio_fetcher factory function."""
|
||||
|
||||
def test_creates_flacfetch_fetcher(self):
|
||||
"""Test factory creates FlacFetchAudioFetcher."""
|
||||
fetcher = create_audio_fetcher()
|
||||
assert isinstance(fetcher, FlacFetchAudioFetcher)
|
||||
|
||||
def test_passes_parameters(self):
|
||||
"""Test factory passes parameters to constructor."""
|
||||
logger = MagicMock()
|
||||
fetcher = create_audio_fetcher(
|
||||
logger=logger,
|
||||
redacted_api_key="red_key",
|
||||
ops_api_key="ops_key",
|
||||
)
|
||||
assert fetcher.logger == logger
|
||||
assert fetcher._redacted_api_key == "red_key"
|
||||
assert fetcher._ops_api_key == "ops_key"
|
||||
|
||||
|
||||
class TestFlacFetchManagerInitialization:
|
||||
"""Tests for lazy FetchManager initialization."""
|
||||
|
||||
def test_manager_is_lazily_initialized(self):
|
||||
"""Test FetchManager is lazily initialized (not created at init time)."""
|
||||
fetcher = FlacFetchAudioFetcher()
|
||||
# Manager should be None until first use
|
||||
assert fetcher._manager is None
|
||||
|
||||
def test_api_keys_stored_at_init(self):
|
||||
"""Test API keys are stored at init for later use."""
|
||||
fetcher = FlacFetchAudioFetcher(
|
||||
redacted_api_key="red_key",
|
||||
ops_api_key="ops_key",
|
||||
)
|
||||
assert fetcher._redacted_api_key == "red_key"
|
||||
assert fetcher._ops_api_key == "ops_key"
|
||||
|
||||
|
||||
class TestDownloadWithCustomFilename:
|
||||
"""Tests for download with custom filename."""
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_uses_custom_filename(self, mock_get_manager, tmp_path):
|
||||
"""Test download uses provided custom filename."""
|
||||
mock_manager = MagicMock()
|
||||
mock_get_manager.return_value = mock_manager
|
||||
downloaded_path = str(tmp_path / "custom_name.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
|
||||
fetcher = FlacFetchAudioFetcher()
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Original Title",
|
||||
artist="Original Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
result = fetcher.download(
|
||||
search_result,
|
||||
str(tmp_path),
|
||||
output_filename="custom_name",
|
||||
)
|
||||
|
||||
# Verify custom filename was passed
|
||||
mock_manager.download.assert_called_once_with(
|
||||
raw_result,
|
||||
output_path=str(tmp_path),
|
||||
output_filename="custom_name",
|
||||
)
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_generates_filename_from_metadata(self, mock_get_manager, tmp_path):
|
||||
"""Test download generates filename from artist/title if not provided."""
|
||||
mock_manager = MagicMock()
|
||||
mock_get_manager.return_value = mock_manager
|
||||
downloaded_path = str(tmp_path / "Artist - Title.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
|
||||
fetcher = FlacFetchAudioFetcher()
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Title",
|
||||
artist="Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
result = fetcher.download(search_result, str(tmp_path))
|
||||
|
||||
# Verify filename was generated from metadata
|
||||
mock_manager.download.assert_called_once_with(
|
||||
raw_result,
|
||||
output_path=str(tmp_path),
|
||||
output_filename="Artist - Title",
|
||||
)
|
||||
|
||||
|
||||
class TestOutputDirectoryCreation:
|
||||
"""Tests for output directory handling."""
|
||||
|
||||
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
|
||||
def test_download_creates_output_directory(self, mock_get_manager, tmp_path):
|
||||
"""Test download creates output directory if it doesn't exist."""
|
||||
mock_manager = MagicMock()
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
new_dir = tmp_path / "new_directory"
|
||||
downloaded_path = str(new_dir / "song.flac")
|
||||
mock_manager.download.return_value = downloaded_path
|
||||
|
||||
fetcher = FlacFetchAudioFetcher()
|
||||
raw_result = MagicMock()
|
||||
search_result = AudioSearchResult(
|
||||
title="Test",
|
||||
artist="Artist",
|
||||
url="https://example.com/song",
|
||||
provider="YouTube",
|
||||
raw_result=raw_result,
|
||||
)
|
||||
|
||||
# Directory should be created
|
||||
assert not new_dir.exists()
|
||||
result = fetcher.download(search_result, str(new_dir))
|
||||
assert new_dir.exists()
|
||||
@@ -4,7 +4,6 @@ import glob
|
||||
import shutil
|
||||
from unittest.mock import MagicMock, patch, mock_open, call, DEFAULT
|
||||
from karaoke_gen.karaoke_gen import KaraokePrep
|
||||
import yt_dlp # Keep import for patching target
|
||||
from karaoke_gen.utils import sanitize_filename # Import utility
|
||||
|
||||
class TestFileOperations:
|
||||
@@ -41,102 +40,45 @@ class TestFileOperations:
|
||||
# Verify the correct file path was returned
|
||||
assert result == file_path
|
||||
|
||||
def test_download_video(self, basic_karaoke_gen, temp_dir):
|
||||
"""Test downloading a video from a URL."""
|
||||
url = "https://example.com/video"
|
||||
def test_download_audio_from_fetcher_result(self, basic_karaoke_gen, temp_dir):
|
||||
"""Test processing downloaded audio from flacfetch."""
|
||||
# Create a mock source file
|
||||
source_file = os.path.join(temp_dir, "source.flac")
|
||||
with open(source_file, "w") as f:
|
||||
f.write("test audio content")
|
||||
|
||||
output_filename = os.path.join(temp_dir, "output")
|
||||
downloaded_file = output_filename + ".mp4"
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager and its methods
|
||||
mock_ydl_instance = MagicMock()
|
||||
# Mock extract_info to prevent network calls during download process
|
||||
mock_ydl_instance.extract_info.return_value = {'id': 'video_id', 'formats': [], 'ext': 'mp4'}
|
||||
# Completely mock the download method to do nothing
|
||||
mock_ydl_instance.download = MagicMock(return_value=None)
|
||||
|
||||
# Mock glob.glob to return our "downloaded" file
|
||||
mock_glob_result = [downloaded_file]
|
||||
|
||||
# Patch the 'ydl' class used in file_handler.py and glob.glob
|
||||
with patch('karaoke_gen.file_handler.ydl') as mock_ydl_context, \
|
||||
patch('glob.glob', return_value=mock_glob_result):
|
||||
|
||||
# Configure the context manager to return our mock instance
|
||||
mock_ydl_context.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
# Create the file that glob will "find"
|
||||
os.makedirs(os.path.dirname(downloaded_file), exist_ok=True)
|
||||
with open(downloaded_file, "w") as f:
|
||||
f.write("test video content")
|
||||
|
||||
# Call the method
|
||||
result = basic_karaoke_gen.file_handler.download_video(url, output_filename)
|
||||
# Test with mocked shutil.copy2
|
||||
with patch('shutil.copy2') as mock_copy:
|
||||
result = basic_karaoke_gen.file_handler.download_audio_from_fetcher_result(
|
||||
source_file, output_filename
|
||||
)
|
||||
|
||||
# Verify the correct file path was returned
|
||||
assert result == downloaded_file
|
||||
assert result == output_filename + ".flac"
|
||||
|
||||
# Verify ydl was instantiated with correct options
|
||||
expected_ydl_opts = {
|
||||
"quiet": True,
|
||||
"format": "bv*+ba/b",
|
||||
"outtmpl": f"{output_filename}.%(ext)s",
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
}
|
||||
mock_ydl_context.assert_called_once_with(expected_ydl_opts)
|
||||
|
||||
# Verify download was called on the instance
|
||||
mock_ydl_instance.download.assert_called_once_with([url])
|
||||
|
||||
# Verify glob was called
|
||||
glob.glob.assert_called_once_with(f"{output_filename}.*")
|
||||
# Verify shutil.copy2 was called with correct arguments
|
||||
mock_copy.assert_called_once_with(source_file, output_filename + ".flac")
|
||||
|
||||
def test_download_video_no_files_found(self, basic_karaoke_gen):
|
||||
"""Test downloading a video when no files are found after download."""
|
||||
url = "https://example.com/video"
|
||||
output_filename = "output"
|
||||
def test_download_audio_from_fetcher_result_same_location(self, basic_karaoke_gen, temp_dir):
|
||||
"""Test processing when source and target are effectively the same."""
|
||||
# Create a source file
|
||||
source_file = os.path.join(temp_dir, "output.flac")
|
||||
with open(source_file, "w") as f:
|
||||
f.write("test audio content")
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager and its methods
|
||||
mock_ydl_instance = MagicMock()
|
||||
# Mock extract_info to prevent network calls during download process
|
||||
mock_ydl_instance.extract_info.return_value = {'id': 'video_id', 'formats': [], 'ext': 'mp4'}
|
||||
# Completely mock the download method to do nothing
|
||||
mock_ydl_instance.download = MagicMock(return_value=None)
|
||||
output_filename = os.path.join(temp_dir, "output")
|
||||
|
||||
# Mock glob.glob to return empty list (no files found)
|
||||
with patch('karaoke_gen.file_handler.ydl') as mock_ydl_context, \
|
||||
patch('glob.glob', return_value=[]):
|
||||
# Test when source and target are the same
|
||||
with patch('shutil.copy2') as mock_copy:
|
||||
result = basic_karaoke_gen.file_handler.download_audio_from_fetcher_result(
|
||||
source_file, output_filename
|
||||
)
|
||||
|
||||
# Configure the context manager to return our mock instance
|
||||
mock_ydl_context.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
# Call the method
|
||||
result = basic_karaoke_gen.file_handler.download_video(url, output_filename)
|
||||
|
||||
# Verify None was returned
|
||||
assert result is None
|
||||
|
||||
# Verify ydl was instantiated
|
||||
mock_ydl_context.assert_called_once()
|
||||
|
||||
# Verify download was called on the instance
|
||||
mock_ydl_instance.download.assert_called_once_with([url])
|
||||
|
||||
# Verify glob was called
|
||||
glob.glob.assert_called_once_with(f"{output_filename}.*")
|
||||
# Should return the source file without copying
|
||||
assert result == source_file
|
||||
mock_copy.assert_not_called()
|
||||
|
||||
def test_extract_still_image_from_video(self, basic_karaoke_gen):
|
||||
"""Test extracting a still image from a video."""
|
||||
|
||||
@@ -90,6 +90,7 @@ def mock_base_args():
|
||||
style_override=None,
|
||||
background_video=None,
|
||||
background_video_darkness=0,
|
||||
auto_download=False, # New flacfetch parameter
|
||||
enable_cdg=False,
|
||||
enable_txt=False,
|
||||
brand_prefix=None,
|
||||
@@ -210,8 +211,8 @@ async def test_arg_parsing_artist_title_only(mock_kprep_class, mock_isdir, mock_
|
||||
assert mock_kprep_class.call_args.kwargs["title"] == "Test Title"
|
||||
assert mock_kprep_class.call_args.kwargs["input_media"] is None
|
||||
mock_kprep_instance.process.assert_awaited_once()
|
||||
# Verify warning about YouTube search is shown
|
||||
assert "No input media provided, the top YouTube search result for" in caplog.text
|
||||
# Verify message about flacfetch search is shown
|
||||
assert "flacfetch will search for" in caplog.text
|
||||
|
||||
@patch("karaoke_gen.utils.gen_cli.is_url", return_value=False)
|
||||
@patch("karaoke_gen.utils.gen_cli.is_file", return_value=False)
|
||||
|
||||
+364
-273
@@ -1,302 +1,393 @@
|
||||
"""
|
||||
Unit tests for metadata module.
|
||||
|
||||
Tests cover the metadata extraction and parsing functions after flacfetch integration.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch, call
|
||||
# Import the specific class for patching is not needed if we patch the target correctly
|
||||
# from yt_dlp import YoutubeDL
|
||||
from unittest.mock import MagicMock, patch
|
||||
from karaoke_gen.karaoke_gen import KaraokePrep
|
||||
from karaoke_gen.metadata import extract_info_for_online_media, parse_track_metadata
|
||||
|
||||
class TestMetadata:
|
||||
def test_extract_info_for_online_media_with_url(self, basic_karaoke_gen):
|
||||
"""Test extracting info from online media with a URL."""
|
||||
url = "https://example.com/video"
|
||||
mock_info = {
|
||||
|
||||
class TestExtractInfoForOnlineMedia:
|
||||
"""Tests for extract_info_for_online_media function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_logger(self):
|
||||
"""Create a mock logger."""
|
||||
return MagicMock()
|
||||
|
||||
def test_url_raises_value_error(self, mock_logger):
|
||||
"""Test that providing a URL raises ValueError since URLs are no longer supported."""
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
extract_info_for_online_media(
|
||||
input_url="https://example.com/video",
|
||||
input_artist="Test Artist",
|
||||
input_title="Test Title",
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert "URL-based audio fetching has been replaced" in str(exc_info.value)
|
||||
assert "flacfetch" in str(exc_info.value)
|
||||
|
||||
def test_artist_title_creates_metadata(self, mock_logger):
|
||||
"""Test that artist and title create synthetic metadata dict."""
|
||||
result = extract_info_for_online_media(
|
||||
input_url=None,
|
||||
input_artist="Test Artist",
|
||||
input_title="Test Title",
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert result["title"] == "Test Artist - Test Title"
|
||||
assert result["artist"] == "Test Artist"
|
||||
assert result["track_title"] == "Test Title"
|
||||
assert result["extractor_key"] == "flacfetch"
|
||||
assert result["source"] == "flacfetch"
|
||||
assert result["url"] is None
|
||||
|
||||
def test_missing_artist_raises_value_error(self, mock_logger):
|
||||
"""Test that missing artist raises ValueError."""
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
extract_info_for_online_media(
|
||||
input_url=None,
|
||||
input_artist=None,
|
||||
input_title="Test Title",
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert "Artist and title are required" in str(exc_info.value)
|
||||
|
||||
def test_missing_title_raises_value_error(self, mock_logger):
|
||||
"""Test that missing title raises ValueError."""
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
extract_info_for_online_media(
|
||||
input_url=None,
|
||||
input_artist="Test Artist",
|
||||
input_title=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert "Artist and title are required" in str(exc_info.value)
|
||||
|
||||
def test_cookies_str_deprecated(self, mock_logger):
|
||||
"""Test that cookies_str parameter is accepted but ignored."""
|
||||
# Should not raise an error even with cookies_str
|
||||
result = extract_info_for_online_media(
|
||||
input_url=None,
|
||||
input_artist="Test Artist",
|
||||
input_title="Test Title",
|
||||
logger=mock_logger,
|
||||
cookies_str="some_cookies",
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert result["artist"] == "Test Artist"
|
||||
|
||||
|
||||
class TestParseTrackMetadata:
|
||||
"""Tests for parse_track_metadata function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_logger(self):
|
||||
"""Create a mock logger."""
|
||||
return MagicMock()
|
||||
|
||||
def test_flacfetch_metadata(self, mock_logger):
|
||||
"""Test parsing flacfetch-style metadata."""
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"artist": "Test Artist",
|
||||
"track_title": "Test Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "flacfetch_test",
|
||||
"url": None,
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["artist"] == "Test Artist"
|
||||
assert result["title"] == "Test Title"
|
||||
assert result["extractor"] == "flacfetch"
|
||||
assert result["url"] is None
|
||||
|
||||
def test_flacfetch_with_persistent_artist(self, mock_logger):
|
||||
"""Test flacfetch metadata with persistent artist override."""
|
||||
extracted_info = {
|
||||
"title": "Original Artist - Test Title",
|
||||
"artist": "Original Artist",
|
||||
"track_title": "Test Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "flacfetch_test",
|
||||
"url": None,
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist="Persistent Artist",
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["artist"] == "Persistent Artist"
|
||||
assert result["title"] == "Test Title"
|
||||
|
||||
def test_legacy_metadata_with_url(self, mock_logger):
|
||||
"""Test parsing legacy-style metadata with URL (backward compatibility)."""
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": url
|
||||
"url": "https://example.com/video",
|
||||
}
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager and its extract_info method
|
||||
mock_ydl_instance = MagicMock()
|
||||
mock_ydl_instance.extract_info.return_value = mock_info
|
||||
|
||||
# Patch the 'ydl' alias used in metadata.py
|
||||
with patch('karaoke_gen.metadata.ydl') as mock_ydl_context:
|
||||
# Configure the context manager mock
|
||||
mock_ydl_context.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
# Call the method
|
||||
basic_karaoke_gen.extract_info_for_online_media(input_url=url)
|
||||
|
||||
# Verify ydl was instantiated (implicitly checks context manager usage)
|
||||
expected_ydl_opts = {
|
||||
"quiet": True,
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
}
|
||||
mock_ydl_context.assert_called_once_with(expected_ydl_opts)
|
||||
|
||||
# Verify extract_info was called on the instance
|
||||
mock_ydl_instance.extract_info.assert_called_once_with(url, download=False)
|
||||
|
||||
# Verify the extracted_info was set correctly
|
||||
assert basic_karaoke_gen.extracted_info == mock_info
|
||||
|
||||
def test_extract_info_for_online_media_with_search(self, basic_karaoke_gen):
|
||||
"""Test extracting info from online media with artist and title search."""
|
||||
artist = "Test Artist"
|
||||
title = "Test Title"
|
||||
mock_search_result = {
|
||||
"entries": [
|
||||
{
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager and its extract_info method
|
||||
mock_ydl_instance = MagicMock()
|
||||
mock_ydl_instance.extract_info.return_value = mock_search_result
|
||||
|
||||
# Patch the 'ydl' alias used in metadata.py
|
||||
with patch('karaoke_gen.metadata.ydl') as mock_ydl_context:
|
||||
# Configure the context manager mock
|
||||
mock_ydl_context.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
# Call the method
|
||||
basic_karaoke_gen.extract_info_for_online_media(input_artist=artist, input_title=title)
|
||||
|
||||
# Verify ydl was instantiated with correct options
|
||||
expected_ydl_opts = {
|
||||
"quiet": True,
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
"format": "bestaudio",
|
||||
"noplaylist": "True",
|
||||
"extract_flat": True
|
||||
}
|
||||
mock_ydl_context.assert_called_once_with(expected_ydl_opts)
|
||||
|
||||
# Verify extract_info was called with the correct arguments
|
||||
expected_query = f"ytsearch1:{artist} {title}"
|
||||
mock_ydl_instance.extract_info.assert_called_once_with(expected_query, download=False)
|
||||
|
||||
# Verify the extracted_info was set correctly
|
||||
assert basic_karaoke_gen.extracted_info == mock_search_result["entries"][0]
|
||||
|
||||
def test_extract_info_for_online_media_no_results(self, basic_karaoke_gen):
|
||||
"""Test extracting info from online media with no search results."""
|
||||
artist = "Test Artist"
|
||||
title = "Test Title"
|
||||
mock_search_result = {"entries": []}
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager
|
||||
|
||||
# Mock the yt_dlp.YoutubeDL context manager and its extract_info method
|
||||
mock_ydl_instance = MagicMock()
|
||||
mock_ydl_instance.extract_info.return_value = mock_search_result
|
||||
|
||||
# Patch the 'ydl' alias used in metadata.py
|
||||
with patch('karaoke_gen.metadata.ydl') as mock_ydl_context:
|
||||
# Configure the context manager mock
|
||||
mock_ydl_context.return_value.__enter__.return_value = mock_ydl_instance
|
||||
|
||||
# Assert that calling the method with no results raises an IndexError
|
||||
with pytest.raises(IndexError):
|
||||
basic_karaoke_gen.extract_info_for_online_media(input_artist=artist, input_title=title)
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
# Verify ydl was instantiated with correct options
|
||||
expected_ydl_opts = {
|
||||
"quiet": True,
|
||||
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"referer": "https://www.youtube.com/",
|
||||
"sleep_interval": 1,
|
||||
"max_sleep_interval": 3,
|
||||
"fragment_retries": 3,
|
||||
"extractor_retries": 3,
|
||||
"retries": 3,
|
||||
"http_headers": {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-us,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"DNT": "1",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
"format": "bestaudio",
|
||||
"noplaylist": "True",
|
||||
"extract_flat": True
|
||||
}
|
||||
mock_ydl_context.assert_called_once_with(expected_ydl_opts)
|
||||
assert result["url"] == "https://example.com/video"
|
||||
assert result["extractor"] == "Youtube"
|
||||
assert result["media_id"] == "12345"
|
||||
assert result["artist"] == "Test Artist"
|
||||
assert result["title"] == "Test Title"
|
||||
|
||||
# Verify extract_info was called with the correct arguments
|
||||
expected_query = f"ytsearch1:{artist} {title}"
|
||||
mock_ydl_instance.extract_info.assert_called_once_with(expected_query, download=False)
|
||||
|
||||
def test_parse_single_track_metadata_complete(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata from extracted info with complete information."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
def test_legacy_metadata_with_webpage_url(self, mock_logger):
|
||||
"""Test parsing metadata with webpage_url instead of url."""
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
"webpage_url": "https://example.com/video",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.url == "https://example.com/video"
|
||||
assert basic_karaoke_gen.extractor == "Youtube"
|
||||
assert basic_karaoke_gen.media_id == "12345"
|
||||
assert basic_karaoke_gen.artist == "Test Artist"
|
||||
assert basic_karaoke_gen.title == "Test Title"
|
||||
|
||||
def test_parse_single_track_metadata_with_input_values(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with provided input values."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Wrong Artist - Wrong Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
}
|
||||
|
||||
input_artist = "Input Artist"
|
||||
input_title = "Input Title"
|
||||
|
||||
# Set artist and title on the instance *before* calling parse,
|
||||
# because the method uses these values in its final check.
|
||||
basic_karaoke_gen.artist = input_artist
|
||||
basic_karaoke_gen.title = input_title
|
||||
|
||||
# Call the method
|
||||
basic_karaoke_gen.parse_single_track_metadata(input_artist, input_title)
|
||||
|
||||
# Verify URL, extractor, and ID are parsed correctly
|
||||
assert basic_karaoke_gen.url == "https://example.com/video"
|
||||
assert basic_karaoke_gen.extractor == "Youtube"
|
||||
assert basic_karaoke_gen.media_id == "12345"
|
||||
|
||||
# Verify the artist and title remain the input values
|
||||
assert basic_karaoke_gen.artist == input_artist
|
||||
assert basic_karaoke_gen.title == input_title
|
||||
|
||||
def test_parse_single_track_metadata_with_persistent_artist(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with persistent artist."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
}
|
||||
|
||||
basic_karaoke_gen.persistent_artist = "Persistent Artist"
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.artist == "Persistent Artist"
|
||||
assert basic_karaoke_gen.title == "Test Title"
|
||||
|
||||
def test_parse_single_track_metadata_with_ie_key(self, basic_karaoke_gen):
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["url"] == "https://example.com/video"
|
||||
|
||||
def test_metadata_with_ie_key(self, mock_logger):
|
||||
"""Test parsing metadata with ie_key instead of extractor_key."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"ie_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
"url": "https://example.com/video",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.extractor == "Youtube"
|
||||
|
||||
def test_parse_single_track_metadata_with_webpage_url(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with webpage_url instead of url."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["extractor"] == "Youtube"
|
||||
|
||||
def test_metadata_with_input_values(self, mock_logger):
|
||||
"""Test parsing metadata preserves input artist/title."""
|
||||
extracted_info = {
|
||||
"title": "Wrong Artist - Wrong Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "12345",
|
||||
"webpage_url": "https://example.com/video"
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.url == "https://example.com/video"
|
||||
|
||||
def test_parse_single_track_metadata_missing_url(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with missing URL."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345"
|
||||
}
|
||||
|
||||
with pytest.raises(Exception, match="Failed to extract URL from input media metadata"):
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
def test_parse_single_track_metadata_missing_extractor(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with missing extractor."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
}
|
||||
|
||||
with pytest.raises(Exception, match="Failed to find extractor name from input media metadata"):
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
def test_parse_single_track_metadata_with_uploader(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with uploader as artist fallback."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist="Input Artist",
|
||||
current_title="Input Title",
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["artist"] == "Input Artist"
|
||||
assert result["title"] == "Input Title"
|
||||
|
||||
def test_metadata_with_uploader_fallback(self, mock_logger):
|
||||
"""Test parsing metadata uses uploader as artist fallback."""
|
||||
extracted_info = {
|
||||
"title": "Test Title", # No artist in title
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video",
|
||||
"uploader": "Test Uploader"
|
||||
"uploader": "Test Uploader",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.artist == "Test Uploader"
|
||||
assert basic_karaoke_gen.title == "Test Title"
|
||||
|
||||
def test_parse_single_track_metadata_missing_artist_title(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with missing artist and title."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["artist"] == "Test Uploader"
|
||||
assert result["title"] == "Test Title"
|
||||
|
||||
def test_metadata_missing_artist_title_raises(self, mock_logger):
|
||||
"""Test parsing metadata raises when artist/title cannot be extracted."""
|
||||
extracted_info = {
|
||||
"extractor_key": "Youtube",
|
||||
"id": "12345",
|
||||
"url": "https://example.com/video"
|
||||
"url": "https://example.com/video",
|
||||
# No title, no uploader
|
||||
}
|
||||
|
||||
with pytest.raises(Exception, match="Failed to extract artist and title from the input media metadata"):
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert "Failed to extract artist and title" in str(exc_info.value)
|
||||
|
||||
def test_no_url_defaults_to_none(self, mock_logger):
|
||||
"""Test metadata without URL defaults to None (for flacfetch)."""
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "12345",
|
||||
# No url or webpage_url
|
||||
}
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["url"] is None
|
||||
|
||||
def test_no_extractor_defaults_to_flacfetch(self, mock_logger):
|
||||
"""Test metadata without extractor defaults to flacfetch."""
|
||||
extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"id": "12345",
|
||||
# No extractor_key or ie_key
|
||||
}
|
||||
|
||||
result = parse_track_metadata(
|
||||
extracted_info,
|
||||
current_artist=None,
|
||||
current_title=None,
|
||||
persistent_artist=None,
|
||||
logger=mock_logger,
|
||||
)
|
||||
|
||||
assert result["extractor"] == "flacfetch"
|
||||
|
||||
|
||||
class TestKaraokePrepMetadataIntegration:
|
||||
"""Integration tests for KaraokePrep metadata methods."""
|
||||
|
||||
@pytest.fixture
|
||||
def basic_karaoke_gen(self, tmp_path):
|
||||
"""Create a basic KaraokePrep instance for testing."""
|
||||
return KaraokePrep(
|
||||
input_media=None,
|
||||
artist="Test Artist",
|
||||
title="Test Title",
|
||||
output_dir=str(tmp_path),
|
||||
)
|
||||
|
||||
def test_extract_info_for_online_media_with_artist_title(self, basic_karaoke_gen):
|
||||
"""Test extracting info creates flacfetch metadata."""
|
||||
result = basic_karaoke_gen.extract_info_for_online_media(
|
||||
input_artist="Test Artist",
|
||||
input_title="Test Title",
|
||||
)
|
||||
|
||||
assert result["source"] == "flacfetch"
|
||||
assert result["artist"] == "Test Artist"
|
||||
assert result["track_title"] == "Test Title"
|
||||
assert basic_karaoke_gen.extracted_info == result
|
||||
|
||||
def test_extract_info_with_url_raises(self, basic_karaoke_gen):
|
||||
"""Test extracting info with URL raises ValueError."""
|
||||
with pytest.raises(ValueError):
|
||||
basic_karaoke_gen.extract_info_for_online_media(
|
||||
input_url="https://example.com/video",
|
||||
)
|
||||
|
||||
def test_parse_single_track_metadata_complete(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata from flacfetch-style extracted info."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"artist": "Test Artist",
|
||||
"track_title": "Test Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "flacfetch_test",
|
||||
"url": None,
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.url is None
|
||||
assert basic_karaoke_gen.extractor == "flacfetch"
|
||||
assert basic_karaoke_gen.artist == "Test Artist"
|
||||
assert basic_karaoke_gen.title == "Test Title"
|
||||
|
||||
def test_parse_single_track_metadata_with_input_values(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with provided input values."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Wrong Artist - Wrong Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "12345",
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
input_artist = "Input Artist"
|
||||
input_title = "Input Title"
|
||||
|
||||
basic_karaoke_gen.artist = input_artist
|
||||
basic_karaoke_gen.title = input_title
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(input_artist, input_title)
|
||||
|
||||
assert basic_karaoke_gen.artist == input_artist
|
||||
assert basic_karaoke_gen.title == input_title
|
||||
|
||||
def test_parse_single_track_metadata_with_persistent_artist(self, basic_karaoke_gen):
|
||||
"""Test parsing metadata with persistent artist."""
|
||||
basic_karaoke_gen.extracted_info = {
|
||||
"title": "Test Artist - Test Title",
|
||||
"artist": "Test Artist",
|
||||
"track_title": "Test Title",
|
||||
"extractor_key": "flacfetch",
|
||||
"id": "12345",
|
||||
"source": "flacfetch",
|
||||
}
|
||||
|
||||
basic_karaoke_gen.persistent_artist = "Persistent Artist"
|
||||
|
||||
basic_karaoke_gen.parse_single_track_metadata(None, None)
|
||||
|
||||
assert basic_karaoke_gen.artist == "Persistent Artist"
|
||||
assert basic_karaoke_gen.title == "Test Title"
|
||||
@@ -815,11 +815,11 @@ class TestMain:
|
||||
@patch('karaoke_gen.utils.remote_cli.is_file')
|
||||
@patch('os.path.isfile')
|
||||
@patch('os.path.isdir')
|
||||
def test_main_youtube_search_not_supported(
|
||||
def test_main_audio_search_not_supported(
|
||||
self, mock_isdir, mock_isfile, mock_is_file, mock_is_url,
|
||||
mock_check, mock_auth, mock_create_parser
|
||||
):
|
||||
"""Test main rejects YouTube search."""
|
||||
"""Test main rejects audio search (artist+title without file)."""
|
||||
mock_parser = MagicMock()
|
||||
mock_args = MagicMock()
|
||||
mock_args.service_url = "https://api.example.com"
|
||||
@@ -831,7 +831,7 @@ class TestMain:
|
||||
mock_args.edit_lyrics = False
|
||||
mock_args.test_email_template = False
|
||||
mock_args.resume = None
|
||||
mock_args.args = ["Artist", "Title"] # YouTube search format
|
||||
mock_args.args = ["Artist", "Title"] # Audio search format (not yet supported in remote)
|
||||
mock_parser.parse_args.return_value = mock_args
|
||||
mock_create_parser.return_value = mock_parser
|
||||
mock_auth.return_value = "token"
|
||||
|
||||
Reference in new issue
Block a user