From 4f8f72f26b0e4d4a273412350eb09b49d33b88a1 Mon Sep 17 00:00:00 2001 From: Emad Rad Date: Sun, 25 Feb 2024 21:37:41 +0330 Subject: [PATCH] fix: prompt when confirm flag is and quality is not provided With this, the prompt is not used when the confirm flag it True, which is desirable for batch downloading. Close #2 --- apyrat/cli.py | 19 +++++++++++++------ tests/cli_test.py | 6 +++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/apyrat/cli.py b/apyrat/cli.py index 34bb93f..d891588 100644 --- a/apyrat/cli.py +++ b/apyrat/cli.py @@ -1,7 +1,8 @@ """Console script for apyrat.""" + import click -from apyrat.apyrat import Downloader, URLType +from apyrat.apyrat import Downloader, URLType, VideoQuality from apyrat.utils import get_about_information @@ -54,7 +55,7 @@ def display_help(ctx, param, value): ) @click.argument("url", type=str, required=True) @click.pass_context -def main(ctx, url, quality, filename, confirm): +def main(ctx, url: str, quality: str, filename: str, confirm: bool): """ Download Aparat videos from your terminal """ @@ -68,16 +69,22 @@ def main(ctx, url, quality, filename, confirm): if filename and downloader.url_type == URLType.VIDEO: downloader.file_name = filename - quality_choice = get_quality(downloader, quality, confirm) + quality_choice = str( + downloader.default_quality() + if confirm + else get_quality( + downloader, + quality, + ) + ) downloader.download(quality_choice) -def get_quality(downloader, quality, confirm): +def get_quality(downloader: Downloader, quality: VideoQuality): if quality and quality not in downloader.qualities: click.echo(f"Quality {quality} is not available", err=True) - if not confirm: - quality = None + quality = None if not quality: quality_choice = click.prompt( diff --git a/tests/cli_test.py b/tests/cli_test.py index b7c25ee..d98bf95 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -36,18 +36,18 @@ def test_get_quality_available(): downloader = Downloader("https://www.aparat.com/v/qur3I") downloader.qualities = ["480", "720", "1080"] with patch("click.prompt", return_value="720"): - assert get_quality(downloader, "720", True) == "720" + assert get_quality(downloader, "720") == "720" def test_get_quality_not_available(): downloader = Downloader("https://www.aparat.com/v/qur3I") downloader.qualities = ["480", "720", "1080"] with patch("click.prompt", return_value="720"): - assert get_quality(downloader, "240", False) == "720" + assert get_quality(downloader, "240") == "720" def test_get_quality_not_available_no_confirm(): downloader = Downloader("https://www.aparat.com/v/qur3I") downloader.qualities = ["480", "720", "1080"] with patch("click.prompt", return_value="480"): - assert get_quality(downloader, "240", False) == "480" + assert get_quality(downloader, "240") == "480"