from hashlib import md5 import json import os from dotenv import load_dotenv load_dotenv() def save_product_spec(url: str, product_spec: dict) -> bool: """ Save the product specification to the cache. Args: url (str): The URL of the product. product_spec (dict): The product specification to save. Returns: bool: True if the product specification was saved successfully in the cache location, False otherwise. """ filename = md5(url.encode()).hexdigest() filename = f"{filename}.json" filepath = os.path.join(os.getenv("PROD_SPEC_DIR", "prod_spec"), filename) try: with open(filepath, "w") as f: json.dump(product_spec, f, ensure_ascii=False, indent=2) return True except Exception as e: return False if __name__ == "__main__": url = "https://comfy.ua/ua/smartfon/apple-iphone-13-128" product_spec = { "Діагональ дисплея": '6,7"', "Тип екрану": "Super Retina XDR", "Модель процесора": "Apple A14 Bionic", "Основна камера": "12 Мп", "Ємність акумулятора": "3687 мАг", "name": "Смартфон Apple iPhone 12 Pro Max 256Gb Graphite (REF, A)", "price": "29 499 ₴", } print(save_product_spec(url, product_spec))