From 58f63d69874e1e3c2c62e807fa90c9747df59d27 Mon Sep 17 00:00:00 2001 From: Flo Greistorfer Date: Mon, 1 Jul 2024 13:13:24 +0200 Subject: [PATCH] removed old generator --- cclicense.py | 126 -------------- generate_html.old.py | 392 ------------------------------------------- 2 files changed, 518 deletions(-) delete mode 100644 generate_html.old.py diff --git a/cclicense.py b/cclicense.py index ec707d6..bb95454 100644 --- a/cclicense.py +++ b/cclicense.py @@ -6,132 +6,6 @@ class License: pics: list[str] -def licenseswitch(cclicense: str) -> str: - switch = { - "cc-zero": """ -
- $title by $author is marked with - CC0 1.0 -
-""", - "cc-by": """ -
- $title by $author is licensed under - CC BY 4.0 -
-""", - "cc-by-sa": """ -
- $title by $author is licensed under - CC BY-SA 4.0 -
-""", - "cc-by-nd": """ -
- $title by $author is licensed under - CC BY-ND 4.0 -
-""", - "cc-by-nc": """ -
- $title by $author is licensed under - CC BY-NC 4.0 -
-""", - "cc-by-nc-sa": """ -
- $title by $author is licensed under - CC BY-NC-SA 4.0 -
-""", - "cc-by-nc-nd": """ -
- $title by $author is licensed under - CC BY-NC-ND 4.0 -
-""", - } - - return switch.get(cclicense, "") - - def licenseurlswitch(cclicense: str) -> str: switch = { "cc-zero": "https://creativecommons.org/publicdomain/zero/1.0/", diff --git a/generate_html.old.py b/generate_html.old.py deleted file mode 100644 index 30a3a1d..0000000 --- a/generate_html.old.py +++ /dev/null @@ -1,392 +0,0 @@ -#!/usr/bin/env python3 -import os -import argparse -import urllib.parse -import shutil -from multiprocessing import Pool -from string import Template -from pathlib import Path -import numpy as np -from tqdm.auto import tqdm - -import cclicense - -environment = Environment(loader=FileSystemLoader("templates/")) - -_ROOT = "/data/pictures/" -_WEBROOT = "https://pictures.example.com/" -_FOLDERICON = "https://www.svgrepo.com/show/400249/folder.svg" -_ROOTTITLE = "Pictures" -_FAVICON = "favicon.ico" -_AUTHOR = "Author" -imgext = [".jpg", ".jpeg"] -rawext = [".3fr", ".ari", ".arw", ".bay", ".braw", ".crw", ".cr2", ".cr3", ".cap", ".data", ".dcs", ".dcr", ".dng", ".drf", ".eip", ".erf", ".fff", ".gpr", ".iiq", ".k25", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".obm", ".orf", ".pef", ".ptx", ".pxn", ".r3d", ".raf", ".raw", ".rwl", ".rw2", ".rwz", ".sr2", ".srf", ".srw", ".tif", ".tiff", ".x3f"] -excludes = [".lock", _FAVICON, "index.html", ".previews"] -notlist = ["Galleries", "Archives"] - -thumbnails: list[tuple[str, str]] = [] - -HTMLHEADER = """ - - - - - - $title - - - - -""" - -NAVBAR = """ - -""" - - -def thumbnail_convert(arguments: tuple[str, str]): - folder, item = arguments - if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0]) + ".jpg") or args.regenerate: - if shutil.which("magick"): - os.system(f'magick "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0])}.jpg"') - else: - os.system(f'convert "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0])}.jpg"') - - -def listfolder(folder: str, title: str): - if not args.non_interactive: - pbar.desc = f"Generating html files - {folder}" - pbar.update(0) - items: list[str] = os.listdir(folder) - items.sort() - images: list[str] = [] - subfolders: list[str] = [] - - if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root))): - os.mkdir(os.path.join(args.root, ".previews", folder.removeprefix(args.root))) - - body = Template(HTMLHEADER) - navbar = Template(NAVBAR) - contains_files = False - for item in items: - if item not in excludes: - if os.path.isdir(os.path.join(folder, item)): - subfolders.extend([f'
Folder icon
{item}
']) - if item not in notlist: - listfolder(os.path.join(folder, item), os.path.join(folder, item).removeprefix(args.root)) - else: - if not args.non_interactive: - pbar.desc = f"Generating html files - {folder}" - pbar.update(0) - contains_files = True - if os.path.splitext(item)[1].lower() in imgext: - image = f'
{item}
{item}' - if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), item)): - thumbnails.append((folder, item)) - for raw in rawext: - if os.path.exists(os.path.join(folder, os.path.splitext(item)[0] + raw)): - if raw in (".tif", ".tiff"): - image += f': TIFF' - else: - image += f': RAW' - elif os.path.exists(os.path.join(folder, os.path.splitext(item)[0] + raw.upper())): - if raw in (".tif", ".tiff"): - image += f': TIFF' - else: - image += f': RAW' - image += "
" - images.extend([image]) - if not args.non_interactive: - pbar.desc = f"Generating html files - {folder}" - pbar.update(0) - if len(images) > 0 or (args.fancyfolders and not contains_files): - with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f: - f.write(body.substitute(title=title, favicon=f"{args.webroot}{_FAVICON}")) - f.write('
\n') - if folder == args.root: - f.write(f"

{os.path.basename(folder)}

\n") - else: - if args.license: - f.write(navbar.substitute(home=args.webroot, parent=f"{args.webroot}{urllib.parse.quote(folder.removeprefix(args.root).removesuffix(folder.split('/')[-1]))}", title=os.path.basename(folder), license=f'
  • License
  • \n')) - else: - f.write(navbar.substitute(home=args.webroot, parent=f"{args.webroot}{urllib.parse.quote(folder.removeprefix(args.root).removesuffix(folder.split('/')[-1]))}", title=os.path.basename(folder), license="")) - f.write('
    \n') - for subfolder in subfolders: - f.write(subfolder) - f.write("\n") - f.write("
    \n") - f.write("
    \n") - if len(images) > 0: - f.write('
    \n') - for chunk in np.array_split(images, 8): - f.write('
    \n') - for image in chunk: - f.write(f" {image}\n") - f.write("
    \n") - f.write("
    \n") - if args.license: - f.write(_cclicense.substitute(webroot=args.webroot, title=args.title, author=args.author)) - f.write(" \n") - f.close() - else: - if os.path.exists(os.path.join(folder, "index.html")): - os.remove(os.path.join(folder, "index.html")) - if not args.non_interactive: - pbar.update(1) - - -def gettotal(folder): - global total - - if not args.non_interactive: - pbar.desc = f"Traversing filesystem - {folder}" - pbar.update(0) - - items: list[str] = os.listdir(folder) - items.sort() - - for item in items: - if item not in excludes: - if os.path.isdir(os.path.join(folder, item)): - total += 1 - if not args.non_interactive: - pbar.update(1) - if item not in notlist: - gettotal(os.path.join(folder, item)) - - -def main(): - global total - global args - global pbar - global _cclicense - - total = 0 - # Parse command-line arguments - parser = argparse.ArgumentParser(description="Generate html files for static image host.") - parser.add_argument("-p", "--root", help="Root folder", default=_ROOT, required=False, type=str, dest="root") - parser.add_argument("-w", "--webroot", help="Webroot url", default=_WEBROOT, required=False, type=str, dest="webroot") - parser.add_argument("-i", "--foldericon", help="Foldericon url", default=_FOLDERICON, required=False, type=str, dest="foldericon", metavar="ICON") - parser.add_argument("-r", "--regenerate", help="Regenerate thumbnails", action="store_true", default=False, required=False, dest="regenerate") - parser.add_argument("-n", "--non-interactive", help="Disable interactive mode", action="store_true", default=False, required=False, dest="non_interactive") - parser.add_argument("-l", "--license", help="License", default=None, required=False, choices=["cc-zero", "cc-by", "cc-by-sa", "cc-by-nd", "cc-by-nc", "cc-by-nc-sa", "cc-by-nc-nd"], dest="license") - parser.add_argument("-a", "--author", help="Author", default=_AUTHOR, required=False, type=str, dest="author") - parser.add_argument("-t", "--title", help="Title", default=_ROOTTITLE, required=False, type=str, dest="title") - parser.add_argument("--fancyfolders", help="Use fancy folders instead of default apache ones", action="store_true", default=False, required=False, dest="fancyfolders") - args = parser.parse_args() - - if not args.root.endswith("/"): - args.root += "/" - if not args.webroot.endswith("/"): - args.webroot += "/" - if not os.path.exists(os.path.join(args.root, ".previews")): - os.mkdir(os.path.join(args.root, ".previews")) - - if args.license: - _cclicense = Template(cclicense.licenseswitch(args.license)) - - if os.path.exists(os.path.join(args.root, ".lock")): - print("Another instance of this program is running.") - exit() - try: - Path(os.path.join(args.root, ".lock")).touch() - - if args.non_interactive: - print("Generating html files...") - listfolder(args.root, args.title) - - with Pool(os.cpu_count()) as p: - print("Generating thumbnails...") - p.map(thumbnail_convert, thumbnails) - else: - pbar = tqdm(desc="Traversing filesystem", unit=" folders", ascii=True, dynamic_ncols=True) - gettotal(args.root) - pbar.close() - - pbar = tqdm(total=total + 1, desc="Generating html files", unit=" files", ascii=True, dynamic_ncols=True) - listfolder(args.root, args.title) - pbar.close() - - with Pool(os.cpu_count()) as p: - for r in tqdm(p.imap_unordered(thumbnail_convert, thumbnails), total=len(thumbnails), desc="Generating thumbnails", unit=" files", ascii=True, dynamic_ncols=True): - pass - finally: - os.remove(os.path.join(args.root, ".lock")) - - -if __name__ == "__main__": - main()