From efca939c7d1c6221cb61d8645bab5265318c2fdf Mon Sep 17 00:00:00 2001 From: gilesb Date: Mon, 12 Jan 2026 17:00:53 +0000 Subject: [PATCH] Add --raw flag to view command and improve metadata display - Add --raw/-r flag to fetch from /cache/{cid}/raw endpoint - Automatically use /raw when outputting to file or stdout - Show friendly_name, title, filename in metadata view - Use JSON API for metadata instead of streaming response Co-Authored-By: Claude Opus 4.5 --- artdag.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/artdag.py b/artdag.py index 5369208..fc31a0e 100755 --- a/artdag.py +++ b/artdag.py @@ -748,12 +748,18 @@ def cache(limit, offset, media_type): @cli.command() @click.argument("cid") @click.option("--output", "-o", type=click.Path(), help="Save to file (use - for stdout)") -def view(cid, output): +@click.option("--raw", "-r", is_flag=True, help="Get raw content (use with -o to download)") +def view(cid, output, raw): """View or download cached content. Use -o - to pipe to stdout, e.g.: artdag view -o - | mpv - + Use --raw to get the raw file content instead of metadata. """ - url = f"{get_server()}/cache/{cid}" + # Use /raw endpoint if --raw flag or if outputting to file/stdout + if raw or output: + url = f"{get_server()}/cache/{cid}/raw" + else: + url = f"{get_server()}/cache/{cid}" try: if output == "-": @@ -771,16 +777,21 @@ def view(cid, output): f.write(chunk) click.echo(f"Saved to: {output}", err=True) else: - # Get info via GET with stream to check size - resp = requests.get(url, stream=True) + # Get info - use JSON endpoint for metadata + headers = {"Accept": "application/json"} + resp = requests.get(f"{get_server()}/cache/{cid}", headers=headers) resp.raise_for_status() - size = resp.headers.get("content-length", "unknown") - content_type = resp.headers.get("content-type", "unknown") + info = resp.json() click.echo(f"CID: {cid}") - click.echo(f"Size: {size} bytes") - click.echo(f"Type: {content_type}") - click.echo(f"URL: {url}") - resp.close() + click.echo(f"Size: {info.get('size', 'unknown')} bytes") + click.echo(f"Type: {info.get('mime_type') or info.get('media_type', 'unknown')}") + if info.get('friendly_name'): + click.echo(f"Friendly Name: {info['friendly_name']}") + if info.get('title'): + click.echo(f"Title: {info['title']}") + if info.get('filename'): + click.echo(f"Filename: {info['filename']}") + click.echo(f"Raw URL: {get_server()}/cache/{cid}/raw") except requests.HTTPError as e: if e.response.status_code == 404: click.echo(f"Not found: {cid}", err=True)