from jinja2 import Environment, FileSystemLoader from itertools import chain from pathlib import Path import mimetypes MEDIA_FOLDER = "/home/thibaud/Vidéos/Programmation/" OUTPUT_DIR = "./out" def find(name: str, path: Path) -> Path: for file in path.glob("*.*"): if name in file.name: return file def generate_index(folder: Path, output: Path, env: Environment): template = env.get_template("main.tpl") file = output.joinpath(f"{folder.stem}.html") with open(file, "w") as outfile: articles = [] for file in chain(folder.glob("*.mkv"), folder.glob("*.mp4")): articles.append( env.get_template("video.tpl").render( staticdir="/static/", title=file.stem, src="/" + str(file.relative_to(Path(MEDIA_FOLDER).parent)), mime=mimetypes.guess_type(file), ) ) outfile.writelines(template.render(content="\n".join(articles))) def main(): out = Path(OUTPUT_DIR) with open(out.joinpath("index.html"), "w") as outfile: env = Environment(loader=FileSystemLoader("templates")) template = env.get_template("main.tpl") articles = [] for folder in (x for x in Path(MEDIA_FOLDER).iterdir() if x.is_dir()): # Main index thumbnail = find("thumbnail.png", folder) articles.append( env.get_template("folder.tpl").render( staticdir="/static/", title=folder.stem, src=str(thumbnail), ref=str(folder) + '.html' ) ) generate_index(folder, out, env) outfile.writelines(template.render(content="\n".join(articles))) if __name__ == "__main__": main()