From a9342d8bf4461fc7f4ea89156b5683f0d7689a2e Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Tue, 9 Jul 2019 19:39:17 +0200 Subject: [PATCH] Improved check for existing files --- funkwhale_cli/cli.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/funkwhale_cli/cli.py b/funkwhale_cli/cli.py index 60b6a72..3ed9c51 100644 --- a/funkwhale_cli/cli.py +++ b/funkwhale_cli/cli.py @@ -718,15 +718,6 @@ async def track_download(ctx, id, format, directory, template, overwrite, ignore ) logs.logger.info("Downloading from {}".format(download_url)) - response = await ctx.obj["remote"].request("get", download_url, timeout=0) - try: - response.raise_for_status() - except aiohttp.ClientResponseError as e: - if response.status in ignore_errors: - logs.logger.warning("Remote answered with {} for url {}, skipping".format(response.status, download_url)) - continue - else: - raise click.ClickException("Remote answered with {} for url {}, exiting".format(response.status, download_url)) filename_params = flatten(track_data) filename_params["album"] = filename_params['album_title'] filename_params["artist"] = filename_params['artist_name'] @@ -742,9 +733,6 @@ async def track_download(ctx, id, format, directory, template, overwrite, ignore if directory: filename = template.format(**filename_params) full_path = os.path.join(directory, filename) - final_directory = os.path.dirname(full_path) - pathlib.Path(final_directory).mkdir(parents=True, exist_ok=True) - logs.logger.info("Downloading to {}".format(full_path)) existing = os.path.exists(full_path) if skip_existing and existing: logs.logger.info("'{}' already exists on disk, skipping download".format(full_path)) @@ -755,6 +743,20 @@ async def track_download(ctx, id, format, directory, template, overwrite, ignore full_path ) ) + + async with ctx.obj["remote"].request("get", download_url, timeout=0) as response: + try: + response.raise_for_status() + except aiohttp.ClientResponseError as e: + if response.status in ignore_errors: + logs.logger.warning("Remote answered with {} for url {}, skipping".format(response.status, download_url)) + continue + else: + raise click.ClickException("Remote answered with {} for url {}, exiting".format(response.status, download_url)) + if directory: + final_directory = os.path.dirname(full_path) + pathlib.Path(final_directory).mkdir(parents=True, exist_ok=True) + logs.logger.info("Downloading to {}".format(full_path)) out = open(full_path, "wb") else: out = click.get_binary_stream("stdout") -- GitLab