File size: 1,671 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from dotenv import load_dotenv
import json
from hashlib import md5
from typing import List


load_dotenv()


def get_product_spec(url: str) -> dict:
    """
    Get the product specification from the cache.

    Args:
    url (str): The URL to get the product specification for.

    Returns:
    dict: The product specification from the cache.
    """

    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, "r") as f:
            return json.load(f)
    except Exception as e:
        return False

def get_latest_dir(dir: str) -> str:
    """
    Get the latest directory in the given directory.

    Args:
    dir (str): The parent directory to search for the latest directory.

    Returns:
    str: The path to the latest directory.
    """

    dirs: List[str] = [
        d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))
    ]
    if not dirs:
        raise ValueError(f"No directories found in {dir}")

    latest_dir = max(dirs, key=lambda x: int(x))
    return os.path.join(dir, latest_dir)

def get_latest_html_file(directory: str) -> str:
    """
    Get the latest HTML file in the given directory.

    Args:
    directory (str): The directory to search for the latest HTML file.

    Returns:
    str: The path to the latest HTML
    """
    
    html_files = [f for f in os.listdir(directory) if f.endswith(".html")]
    if not html_files:
        return None

    latest_file = max(html_files, key=lambda x: int(os.path.splitext(x)[0]))
    return os.path.join(directory, latest_file)