diff --git a/README.md b/README.md index 2393325..457c5ce 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ - **License Information:** Optionally include license information in the HTML files. - **Custom Author and Title:** Allows specifying a custom author and title for the HTML files. - **CSS Theme Support:** Allows specifying a custom CSS theme file for the HTML files. +- **Folder Exclusion:** Ability to exclude specific folders from processing. +- **Selective File Extensions:** Ability to specify which file extensions to include in the gallery. +- **Ignore Other Files:** Option to ignore files other than those specified by the included extensions. ## Requirements @@ -34,47 +37,56 @@ pip install numpy tqdm Jinja2 The script supports several command-line options to customize its behavior. Below is the list of available options: ```sh -./generate_html.py [-h] [-p ROOT] [-w WEBROOT] [-i ICON] [-r] [-n] [--fancyfolders] [-l LICENSE] [-a AUTHOR] [-t TITLE] [--theme THEME] +./generate_html.py [-h] -p ROOT -w WEBROOT -t TITLE [-i ICON] [-r] [-n] [--fancyfolders] [-l LICENSE] [-a AUTHOR] [-e EXTENSION] [--theme THEME] [--ignore-other-files] [--exclude EXCLUDE] ``` ### Options - `-h, --help`: Show the help message and exit. -- `-p ROOT, --root ROOT`: Specify the root folder where the images are stored. Default is `/data/pictures/`. -- `-w WEBROOT, --webroot WEBROOT`: Specify the web root URL where the images will be accessible. Default is `https://pictures.example.com/`. +- `-p ROOT, --root ROOT`: Specify the root folder where the images are stored. This option is required. +- `-w WEBROOT, --webroot WEBROOT`: Specify the web root URL where the images will be accessible. This option is required. +- `-t TITLE, --title TITLE`: Specify the title for the root directory HTML file. This option is required. - `-i ICON, --foldericon ICON`: Specify the URL for the folder icon. Default is `https://www.svgrepo.com/show/400249/folder.svg`. - `-r, --regenerate`: Regenerate thumbnails even if they already exist. - `-n, --non-interactive`: Disable interactive mode, which is useful for automated workflows. - `--fancyfolders`: Use fancy folders instead of the default Apache directory listing. - `-l LICENSE, --license LICENSE`: Specify a license for the content. Options are `cc-zero`, `cc-by`, `cc-by-sa`, `cc-by-nd`, `cc-by-nc`, `cc-by-nc-sa`, and `cc-by-nc-nd`. -- `-a AUTHOR, --author AUTHOR`: Specify the author of the content. -- `-t TITLE, --title TITLE`: Specify the title for the root directory HTML file. +- `-a AUTHOR, --author AUTHOR`: Specify the author of the content. Default is "Author". +- `-e EXTENSION, --extension EXTENSION`: Specify file extensions to include. This option can be used multiple times. - `--theme THEME`: Specify the path to a custom CSS theme file. Default is `themes/default.css`. +- `--ignore-other-files`: Ignore files other than those specified by the included extensions. +- `--exclude EXCLUDE`: Exclude folders from processing. Only provide the basename of the folders you want to exclude. This option can be used multiple times. ### Example To generate HTML files and thumbnails for a directory `/data/pictures` and host them on `https://pictures.example.com`, run: ```sh -./generate_html.py -p /data/pictures -w https://pictures.example.com +./generate_html.py -p /data/pictures -w https://pictures.example.com -t "My Photo Gallery" ``` To regenerate thumbnails and run in non-interactive mode: ```sh -./generate_html.py -p /data/pictures -w https://pictures.example.com -r -n +./generate_html.py -p /data/pictures -w https://pictures.example.com -t "My Photo Gallery" -r -n ``` To include a license, author, and custom title: ```sh -./generate_html.py -p /data/pictures -w https://pictures.example.com -l cc-by -a "John Doe" -t "My Photo Gallery" +./generate_html.py -p /data/pictures -w https://pictures.example.com -t "My Photo Gallery" -l cc-by -a "John Doe" ``` To specify a custom CSS theme: ```sh -./generate_html.py -p /data/pictures -w https://pictures.example.com --theme custom_theme.css +./generate_html.py -p /data/pictures -w https://pictures.example.com -t "My Photo Gallery" --theme custom_theme.css +``` + +To exclude specific folders and specify file extensions: + +```sh +./generate_html.py -p /data/pictures -w https://pictures.example.com -t "My Photo Gallery" --exclude Archives --exclude Temp -e .jpg -e .jpeg -e .png ``` ## Notes @@ -82,7 +94,8 @@ To specify a custom CSS theme: - The root and webroot paths must point to the same folder, one on the filesystem and one on the webserver. Use absolute paths. - Ensure that ImageMagick is installed and accessible in your system for thumbnail generation. - The script generates the preview thumbnails in a `.thumbnails` subdirectory within the root folder. +- The `.lock` file prevents multiple instances of the script from running simultaneously. Make sure to remove it if the script terminates unexpectedly. ## License -This project is licensed under the AGPL-3.0 License. See the [LICENSE](LICENSE) file for details. +This project is licensed under the AGPL-3.0 License. See the [LICENSE](LICENSE) file for details. \ No newline at end of file diff --git a/generate_html.old.py b/generate_html.old.py old mode 100755 new mode 100644 diff --git a/generate_html.py b/generate_html.py index 1a5c8e9..6cb66b3 100755 --- a/generate_html.py +++ b/generate_html.py @@ -13,15 +13,13 @@ import cclicense environment = Environment(loader=FileSystemLoader(os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates/"))) -_ROOT = "/data/pictures/" -_WEBROOT = "https://pictures.example.com/" _FOLDERICON = "https://www.svgrepo.com/show/400249/folder.svg" -_ROOTTITLE = "Pictures" _STATICFILES = os.path.join(os.path.abspath(os.path.dirname(__file__)), "files") _FAVICON = ".static/favicon.ico" _STYLE = ".static/global.css" _THEME = os.path.join(os.path.abspath(os.path.dirname(__file__)), "themes", "default.css") _AUTHOR = "Author" +VERSION = "1.3" # fmt: off 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"] imgext = [".jpg", ".jpeg"] @@ -69,7 +67,7 @@ def listfolder(folder: str, title: str): if os.path.isdir(os.path.join(folder, item)): subfolder = {"url": f"{args.webroot}{baseurl}{urllib.parse.quote(item)}", "name": item} subfolders.extend([subfolder]) - if item not in notlist: + if item not in args.exclude: listfolder(os.path.join(folder, item), os.path.join(folder, item).removeprefix(args.root)) else: extsplit = os.path.splitext(item) @@ -77,7 +75,7 @@ def listfolder(folder: str, title: str): pbar.desc = f"Generating html files - {folder}" pbar.update(0) contains_files = True - if extsplit[1].lower() in imgext: + if extsplit[1].lower() in args.extensions: image = { "url": f"{args.webroot}{baseurl}{urllib.parse.quote(item)}", "thumbnail": f"{args.webroot}.thumbnails/{baseurl}{urllib.parse.quote(extsplit[0])}.jpg", @@ -96,7 +94,7 @@ def listfolder(folder: str, title: str): 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): + if len(images) > 0 or (args.fancyfolders and not contains_files) or (args.fancyfolders and args.ignoreotherfiles): imagechunks = [] if len(images) > 0: for chunk in np.array_split(images, 8): @@ -159,7 +157,7 @@ def gettotal(folder): total += 1 if not args.non_interactive: pbar.update(1) - if item not in notlist: + if item not in args.exclude: gettotal(os.path.join(folder, item)) @@ -174,19 +172,27 @@ def main(): # fmt: off # 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("-p", "--root", help="Root folder", required=True, type=str, dest="root") + parser.add_argument("-w", "--webroot", help="Webroot url", required=True, type=str, dest="webroot") + parser.add_argument("-t", "--title", help="Title", required=True, type=str, dest="title") 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("-e", "--extension", help="Extensions to include (multiple --extension are allowed)", required=False, action="append", dest="extensions") parser.add_argument("--theme", help="Path to CSS theme file", default=_THEME, required=False, type=str, dest="theme") parser.add_argument("--fancyfolders", help="Use fancy folders instead of default apache ones", action="store_true", default=False, required=False, dest="fancyfolders") + parser.add_argument("--ignore-other-files", help="Ignore other files than the ones specified with the specified extensions -e", action="store_true", default=False, required=False, dest="ignoreotherfiles") + parser.add_argument("--exclude", help="Exclude folders, only provide the basename of the folders you want to exclude (multiple --exclude are allowed)", required=False, action="append", dest="exclude") + parser.add_argument('--version', action='version', version=f'%(prog)s {VERSION}') args = parser.parse_args() # fmt: on + if args.extensions == []: + args.extensions = imgext + if args.exclude == []: + args.exclude = notlist if not args.root.endswith("/"): args.root += "/" if not args.webroot.endswith("/"):