|
| 1 | +import click |
| 2 | + |
| 3 | +from typing import Any, Dict, List, Optional |
| 4 | +from rich import print |
| 5 | +from etspi.cli import pass_environment, Environment |
| 6 | +from etspi.etsyv3 import EtsyAPI, Includes |
| 7 | +from etspi.etsyv3.models import UploadListingImageRequest, UpdateListingImageIDRequest |
| 8 | + |
| 9 | +def upload_listing_image(ctx: Any, id: int, shop_id: int, src_file: Any, img_id: int, rank: int, |
| 10 | + overwrite: bool, watermark: bool, alt_text: str, silent: bool) -> None: |
| 11 | + ctx.vlog(f"Upload or Update Image Action for Listing ID: {id} Image ID {img_id}") |
| 12 | + etsy = ctx.get_etsy("LISTING-UPDATE") |
| 13 | + if not src_file is None: |
| 14 | + img_content = src_file.read() |
| 15 | + listing_img = UploadListingImageRequest(image_bytes = img_content, listing_image_id = img_id, rank = rank, overwrite = overwrite, |
| 16 | + is_watermarked = watermark, alt_text = alt_text) |
| 17 | + res = etsy.upload_listing_image(shop_id, id, listing_img) |
| 18 | + elif not img_id is None: |
| 19 | + listing_img = UpdateListingImageIDRequest(listing_image_id = img_id, rank = rank, overwrite = overwrite, |
| 20 | + is_watermarked = watermark, alt_text = alt_text) |
| 21 | + res = etsy.update_listing_image_id(shop_id, id, listing_img) |
| 22 | + else: |
| 23 | + raise click.BadArgumentUsage("No Source File or Image Id provided for upload or update.") |
| 24 | + if not silent: |
| 25 | + print(res) |
| 26 | + return |
| 27 | + |
| 28 | +@click.command("image-upload", short_help="Upload a new listing image or update by Image ID") |
| 29 | +@click.option("-i", "--id", required=True, type=click.INT, help="Listing ID to which apply action.") |
| 30 | +@click.option("-s", "--shop-id", required=False, type=click.INT, help="Shop ID to use for update and other actions.") |
| 31 | +@click.option("-f", "--src-file", required=False, help="Source image file from which to read content to upload.", |
| 32 | + type=click.File(mode="rb", encoding="utf-8", errors="strict", lazy=None, atomic=False)) |
| 33 | +@click.option("-ii", "--img-id", required=False, default=1, type=click.INT, help="Image ID to update for existing.") |
| 34 | +@click.option("-r", "--rank", required=False, default=1, type=click.INT, help="Image rank to upload or update.") |
| 35 | +@click.option("-O", "--overwrite", required=False, default=False, is_flag=True, help="Overwrite file flag to replace existing image.") |
| 36 | +@click.option("-W", "--watermark", required=False, default=False, is_flag=True, help="Set to indicate image has a watermark.") |
| 37 | +@click.option("-a", "--alt-text", required=False, default="", type=click.STRING, help="Image ALT Text to set upon upload or update.") |
| 38 | +@click.option("-S", "--silent", required=False, default=False, is_flag=True, help="Supress console output.") |
| 39 | +@pass_environment |
| 40 | +def cli(ctx, id, shop_id, src_file, img_id, rank, overwrite, watermark, alt_text, silent): |
| 41 | + """Upload a new image or Update existing image by Listing ID and Image ID.""" |
| 42 | + ctx.check_auth("IMAGE-UPLOAD") |
| 43 | + if not id is None: |
| 44 | + ctx.vlog(f"Process Upload or Update Image Listing ID: {id} Image ID: {img_id}") |
| 45 | + try: |
| 46 | + upload_listing_image(ctx, id, shop_id, src_file, img_id, rank, overwrite, watermark, alt_text, silent) |
| 47 | + except Exception as ex: |
| 48 | + ctx.log(f"Error processing command - {ex}") |
0 commit comments