mirror of
https://github.com/Dasemu/x-space-downloader.git
synced 2026-10-13 13:39:45 +00:00
refactor: improve downloader robustness and test coverage
- Add test for cookies_file parameter verification - Add test for yt-dlp configuration (format, outtmpl, postprocessors) - Use validator module for robust space_id extraction - Replace fragile URL splitting with validate_space_url() - Handles trailing slashes and query parameters correctly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from src.validator import validate_space_url
|
||||||
|
|
||||||
|
|
||||||
def download_space(
|
def download_space(
|
||||||
@@ -19,8 +20,13 @@ def download_space(
|
|||||||
Returns:
|
Returns:
|
||||||
Path to the downloaded MP3 file
|
Path to the downloaded MP3 file
|
||||||
"""
|
"""
|
||||||
# Extract space ID from URL for filename
|
# Extract space ID from URL using validator for robustness
|
||||||
space_id = space_url.split('/')[-1]
|
is_valid, space_id, error_message = validate_space_url(space_url)
|
||||||
|
if not is_valid:
|
||||||
|
raise ValueError(f"Invalid space URL: {error_message}")
|
||||||
|
|
||||||
|
if space_id is None:
|
||||||
|
raise ValueError("Could not extract space ID from URL")
|
||||||
|
|
||||||
# Configure yt-dlp options
|
# Configure yt-dlp options
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
|
|||||||
@@ -25,3 +25,61 @@ def test_download_space_basic(tmp_path):
|
|||||||
|
|
||||||
# Verify result contains expected filename
|
# Verify result contains expected filename
|
||||||
assert "space_1ZkKzZWnVRRKv" in result
|
assert "space_1ZkKzZWnVRRKv" in result
|
||||||
|
|
||||||
|
|
||||||
|
def test_download_space_with_cookies(tmp_path):
|
||||||
|
"""Test that cookies file is passed to yt-dlp when provided."""
|
||||||
|
space_url = "https://x.com/i/spaces/1ZkKzZWnVRRKv"
|
||||||
|
cookies_path = "/path/to/cookies.txt"
|
||||||
|
|
||||||
|
with patch('src.downloader.yt_dlp.YoutubeDL') as mock_ytdl_class:
|
||||||
|
# Mock the YoutubeDL instance
|
||||||
|
mock_ytdl = Mock()
|
||||||
|
mock_ytdl_class.return_value.__enter__.return_value = mock_ytdl
|
||||||
|
mock_ytdl.extract_info.return_value = {'id': '1ZkKzZWnVRRKv'}
|
||||||
|
|
||||||
|
# Call download_space with cookies
|
||||||
|
result = download_space(
|
||||||
|
space_url=space_url,
|
||||||
|
output_dir=str(tmp_path),
|
||||||
|
cookies_file=cookies_path
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify YoutubeDL was initialized with correct options
|
||||||
|
call_args = mock_ytdl_class.call_args[0][0]
|
||||||
|
assert 'cookiefile' in call_args
|
||||||
|
assert call_args['cookiefile'] == cookies_path
|
||||||
|
|
||||||
|
|
||||||
|
def test_download_space_ytdlp_configuration(tmp_path):
|
||||||
|
"""Test that yt-dlp is configured correctly with format, outtmpl, and postprocessors."""
|
||||||
|
space_url = "https://x.com/i/spaces/1ZkKzZWnVRRKv"
|
||||||
|
|
||||||
|
with patch('src.downloader.yt_dlp.YoutubeDL') as mock_ytdl_class:
|
||||||
|
# Mock the YoutubeDL instance
|
||||||
|
mock_ytdl = Mock()
|
||||||
|
mock_ytdl_class.return_value.__enter__.return_value = mock_ytdl
|
||||||
|
mock_ytdl.extract_info.return_value = {'id': '1ZkKzZWnVRRKv'}
|
||||||
|
|
||||||
|
# Call download_space
|
||||||
|
result = download_space(
|
||||||
|
space_url=space_url,
|
||||||
|
output_dir=str(tmp_path),
|
||||||
|
cookies_file=None
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify YoutubeDL was initialized with correct options
|
||||||
|
call_args = mock_ytdl_class.call_args[0][0]
|
||||||
|
|
||||||
|
# Check format
|
||||||
|
assert call_args['format'] == 'bestaudio/best'
|
||||||
|
|
||||||
|
# Check outtmpl contains the space_id
|
||||||
|
assert 'outtmpl' in call_args
|
||||||
|
assert 'space_1ZkKzZWnVRRKv' in call_args['outtmpl']
|
||||||
|
|
||||||
|
# Check postprocessors for MP3 conversion
|
||||||
|
assert 'postprocessors' in call_args
|
||||||
|
assert len(call_args['postprocessors']) > 0
|
||||||
|
assert call_args['postprocessors'][0]['key'] == 'FFmpegExtractAudio'
|
||||||
|
assert call_args['postprocessors'][0]['preferredcodec'] == 'mp3'
|
||||||
|
|||||||
Reference in New Issue
Block a user