File size: 1,359 Bytes
eb957df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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))