From bca96971b4c7183f02533568c247968ca615422e Mon Sep 17 00:00:00 2001 From: TDKorn <96394652+TDKorn@users.noreply.github.com> Date: Sun, 7 Apr 2024 11:13:06 -0400 Subject: [PATCH] Add method to download a `MediaEntry` image --- magento/models/product.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/magento/models/product.py b/magento/models/product.py index 037abd4..9965097 100644 --- a/magento/models/product.py +++ b/magento/models/product.py @@ -1,5 +1,7 @@ from __future__ import annotations +import requests from . import Model +from pathlib import Path from functools import cached_property from magento.exceptions import MagentoError from typing import Union, TYPE_CHECKING, Optional, List, Dict @@ -595,6 +597,30 @@ def enable(self, scope: Optional[str] = None) -> bool: self.data['disabled'] = False return self.update(scope) + def download(self, filename: Optional[str] = None) -> Optional[str]: + """Downloads the MediaEntry image + + :param filename: the name of the file to save the image to; uses the filename on Magento if not provided. + :return: the absolute path of the downloaded image file, or ``None`` if the download failed + """ + if filename is None: + filename = Path(self.file).name + + try: + response = requests.get(self.link) + response.raise_for_status() + + except requests.RequestException as e: + self.logger.error(f"Failed to download {self}: {e}") + return None + + fpath = Path(filename).resolve() + with open(fpath, 'wb') as f: + f.write(response.content) + + self.logger.info(f"Downloaded {self} to {fpath}") + return str(fpath) + def add_media_type(self, media_type: str, scope: Optional[str] = None) -> bool: """Add a media type to the MediaEntry on the given scope