diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index e9eb15a7..385de922 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -257,13 +257,17 @@ class VideoDownloader(Downloader, ABC): else: logger.info('Could not find channel') - # Use the default directory if this video has no channel. if destination: + # Download to the directory specified in the settings. out_dir = pathlib.Path(settings['destination']) + logger.debug(f'Downloading {url} to destination from settings') + elif channel: + out_dir = channel.directory + logger.debug(f'Downloading {url} to channel directory') else: + # Download to the default directory if this video has no channel. out_dir = get_no_channel_directory() - if channel: - out_dir = channel.directory + logger.debug(f'Downloading {url} to default directory') out_dir.mkdir(exist_ok=True, parents=True) logs = None # noqa diff --git a/modules/videos/test/test_downloader.py b/modules/videos/test/test_downloader.py index d777cb6c..3e46b863 100644 --- a/modules/videos/test/test_downloader.py +++ b/modules/videos/test/test_downloader.py @@ -447,14 +447,24 @@ async def test_download_result(test_session, test_directory, video_download_mana async def test_download_destination(test_session, test_directory, video_download_manager, mock_video_process_runner, mock_video_extract_info, mock_video_prepare_filename): """A Video can be downloaded to a directory other than it's Channel's directory.""" + # yt-dlp would return the video json. mock_video_extract_info.return_value = example_video_json + # Download process is successful with no logs. + mock_video_process_runner.return_value = (0, {'stdout': '', 'stderr': ''}) + # This result will be ignored during the test, we just need some value so the download does not fail. + mock_video_prepare_filename.return_value = str(test_directory / 'test video.mp4') + + video_download_manager.create_download('https://example.com/1', downloader=VideoDownloader.name) + await video_download_manager.wait_for_all_downloads() + # Output directory matches the channel directory. + assert mock_video_prepare_filename.call_args_list[0].kwargs['ydl'].params['outtmpl']['default']\ + .startswith(f'{test_directory}/videos/channel name/%(uploader)s') + + mock_video_prepare_filename.reset_mock() settings = dict(destination=f'{test_directory}/custom') - video_download_manager.create_download('https://example.com', downloader=VideoDownloader.name, settings=settings) - mock_video_process_runner.return_value = (0, {'stdout': '', 'stderr': ''}) - mock_video_prepare_filename.return_value = str(test_directory / 'test video.mp4') + video_download_manager.create_download('https://example.com/2', downloader=VideoDownloader.name, settings=settings) await video_download_manager.wait_for_all_downloads() - # Output directory matches the custom directory specified. assert mock_video_prepare_filename.call_args_list[0].kwargs['ydl'].params['outtmpl']['default']\ .startswith(f'{test_directory}/custom/%(uploader)s') diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 419eb764..7975ec06 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -221,8 +221,8 @@ async def post_download(_: Request, body: DownloadRequest): downloader = body.downloader if not downloader or downloader in ('auto', 'None', 'null'): downloader = None - destination = get_media_directory() / body.destination if body.destination else None - settings = dict(excluded_urls=excluded_urls, destination=str(destination)) + destination = str(get_media_directory() / body.destination) if body.destination else None + settings = dict(excluded_urls=excluded_urls, destination=destination) if body.frequency: download_manager.recurring_download(urls[0], body.frequency, downloader=downloader, sub_downloader=body.sub_downloader, reset_attempts=True,