neuronslabs/ComfyKnowledgeGraph
0
1import os2from dotenv import load_dotenv3import json4from hashlib import md55from typing import List6 7 8load_dotenv()9 10 11def get_product_spec(url: str) -> dict:12 """13 Get the product specification from the cache.14 15 Args:16 url (str): The URL to get the product specification for.17 18 Returns:19 dict: The product specification from the cache.20 """21 22 filename = md5(url.encode()).hexdigest()23 filename = f"{filename}.json"24 25 filepath = os.path.join(os.getenv("PROD_SPEC_DIR", "prod_spec"), filename)26 try:27 with open(filepath, "r") as f:28 return json.load(f)29 except Exception as e:30 return False31 32def get_latest_dir(dir: str) -> str:33 """34 Get the latest directory in the given directory.35 36 Args:37 dir (str): The parent directory to search for the latest directory.38 39 Returns:40 str: The path to the latest directory.41 """42 43 dirs: List[str] = [44 d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))45 ]46 if not dirs:47 raise ValueError(f"No directories found in {dir}")48 49 latest_dir = max(dirs, key=lambda x: int(x))50 return os.path.join(dir, latest_dir)51 52def get_latest_html_file(directory: str) -> str:53 """54 Get the latest HTML file in the given directory.55 56 Args:57 directory (str): The directory to search for the latest HTML file.58 59 Returns:60 str: The path to the latest HTML61 """62 63 html_files = [f for f in os.listdir(directory) if f.endswith(".html")]64 if not html_files:65 return None66 67 latest_file = max(html_files, key=lambda x: int(os.path.splitext(x)[0]))68 return os.path.join(directory, latest_file)69 