diff --git a/funkwhale_cli/cli.py b/funkwhale_cli/cli.py index fb9c3b5b56ee90417545d92b56cf58e02707c7a5..4465c1fd50b4d442817686fc550be1a789d8ff70 100644 --- a/funkwhale_cli/cli.py +++ b/funkwhale_cli/cli.py @@ -643,6 +643,8 @@ def flatten(d, parent_key="", sep="_"): @click.option("--format", "-f") @click.option("-d", "--directory", type=click.Path(exists=True)) @click.option("-o", "--overwrite", is_flag=True, default=False) +@click.option("-s", "--skip-existing", is_flag=True, default=False) +@click.option("-i", "--ignore-errors", multiple=True, type=int) @click.option( "-t", "--template", @@ -651,7 +653,7 @@ def flatten(d, parent_key="", sep="_"): ) @click.pass_context @async_command -async def track_download(ctx, id, format, directory, template, overwrite): +async def track_download(ctx, id, format, directory, template, overwrite, ignore_errors, skip_existing): async with ctx.obj["remote"]: progressbar = tqdm.tqdm(id, unit="Files") for i in progressbar: @@ -661,7 +663,14 @@ async def track_download(ctx, id, format, directory, template, overwrite): 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'] @@ -680,7 +689,11 @@ async def track_download(ctx, id, format, directory, template, overwrite): 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)) - if not overwrite and os.path.exists(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)) + continue + elif not overwrite and existing: raise click.ClickException( "'{}' already exists on disk. Relaunch this command with --overwrite if you want to replace it".format( full_path