Verified Commit a9342d8b authored by Agate's avatar Agate 💬

Improved check for existing files

parent b434e2b2
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment