Spaces:
Sleeping
Sleeping
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) | |