mirror of
https://github.com/greflm13/StaticGalleryBuilder.git
synced 2026-02-05 11:09:26 +00:00
rename
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Static Gallery Website Builder (SGWB)
|
# Static Gallery Website Builder (SGWB)
|
||||||
|
|
||||||
`static_gallery_website_builder.py` is a Python script designed to generate static HTML files for hosting images on a web server. It traverses a specified root directory, creates thumbnail previews for images, and generates corresponding HTML files to display the images and subfolders in a user-friendly format.
|
`builder.py` is a Python script designed to generate static HTML files for hosting images on a web server. It traverses a specified root directory, creates thumbnail previews for images, and generates corresponding HTML files to display the images and subfolders in a user-friendly format.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
|
|||||||
@@ -49,49 +49,103 @@ class Args:
|
|||||||
|
|
||||||
|
|
||||||
def parse_arguments() -> Args:
|
def parse_arguments() -> Args:
|
||||||
parser = argparse.ArgumentParser(description="Generate HTML files for a static image hosting website.", formatter_class=RichHelpFormatter)
|
parser = argparse.ArgumentParser(
|
||||||
parser.add_argument("-p", "--root-directory", help="Root directory containing the images.", required=True, type=str, dest="root_directory")
|
description="Generate HTML files for a static image hosting website.", formatter_class=RichHelpFormatter
|
||||||
parser.add_argument("-w", "--web-root-url", help="Base URL of the web root for the image hosting site.", required=True, type=str, dest="web_root_url")
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p", "--root-directory", help="Root directory containing the images.", required=True, type=str, dest="root_directory"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-w", "--web-root-url", help="Base URL of the web root for the image hosting site.", required=True, type=str, dest="web_root_url"
|
||||||
|
)
|
||||||
parser.add_argument("-t", "--site-title", help="Title of the image hosting site.", required=True, type=str, dest="site_title")
|
parser.add_argument("-t", "--site-title", help="Title of the image hosting site.", required=True, type=str, dest="site_title")
|
||||||
parser.add_argument("-r", "--regenerate-thumbnails", help="Regenerate thumbnails even if they already exist.", action="store_true", default=False, dest="regenerate_thumbnails")
|
parser.add_argument(
|
||||||
parser.add_argument("-n", "--non-interactive-mode", help="Run in non-interactive mode, disabling progress bars.", action="store_true", default=False, dest="non_interactive_mode")
|
"-r",
|
||||||
parser.add_argument("-l", "--license-type", help="Specify the license type for the images.", choices=["cc-zero", "cc-by", "cc-by-sa", "cc-by-nd", "cc-by-nc", "cc-by-nc-sa", "cc-by-nc-nd"], default=None, dest="license_type")
|
"--regenerate-thumbnails",
|
||||||
parser.add_argument("-a", "--author-name", help="Name of the author of the images.", default=DEFAULT_AUTHOR, type=str, dest="author_name")
|
help="Regenerate thumbnails even if they already exist.",
|
||||||
parser.add_argument("-e", "--file-extensions", help="File extensions to include (can be specified multiple times).", action="append", dest="file_extensions")
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="regenerate_thumbnails",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-n",
|
||||||
|
"--non-interactive-mode",
|
||||||
|
help="Run in non-interactive mode, disabling progress bars.",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="non_interactive_mode",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-l",
|
||||||
|
"--license-type",
|
||||||
|
help="Specify the license type for the images.",
|
||||||
|
choices=["cc-zero", "cc-by", "cc-by-sa", "cc-by-nd", "cc-by-nc", "cc-by-nc-sa", "cc-by-nc-nd"],
|
||||||
|
default=None,
|
||||||
|
dest="license_type",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-a", "--author-name", help="Name of the author of the images.", default=DEFAULT_AUTHOR, type=str, dest="author_name"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-e",
|
||||||
|
"--file-extensions",
|
||||||
|
help="File extensions to include (can be specified multiple times).",
|
||||||
|
action="append",
|
||||||
|
dest="file_extensions",
|
||||||
|
)
|
||||||
parser.add_argument("--theme-path", help="Path to the CSS theme file.", default=DEFAULT_THEME_PATH, type=str, dest="theme_path")
|
parser.add_argument("--theme-path", help="Path to the CSS theme file.", default=DEFAULT_THEME_PATH, type=str, dest="theme_path")
|
||||||
parser.add_argument("--use-fancy-folders", help="Enable fancy folder view instead of the default Apache directory listing.", action="store_true", default=False, dest="use_fancy_folders")
|
parser.add_argument(
|
||||||
parser.add_argument("--ignore-other-files", help="Ignore files that do not match the specified extensions.", action="store_true", default=False, dest="ignore_other_files")
|
"--use-fancy-folders",
|
||||||
parser.add_argument("--exclude-folder", help="Folders to exclude from processing (can be specified multiple times).", action="append", dest="exclude_folders")
|
help="Enable fancy folder view instead of the default Apache directory listing.",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="use_fancy_folders",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--ignore-other-files",
|
||||||
|
help="Ignore files that do not match the specified extensions.",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="ignore_other_files",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--exclude-folder",
|
||||||
|
help="Folders to exclude from processing (can be specified multiple times).",
|
||||||
|
action="append",
|
||||||
|
dest="exclude_folders",
|
||||||
|
)
|
||||||
parser.add_argument("--version", action="version", version=f"%(prog)s {VERSION}")
|
parser.add_argument("--version", action="version", version=f"%(prog)s {VERSION}")
|
||||||
|
|
||||||
parsed_args = parser.parse_args()
|
parsed_args = parser.parse_args()
|
||||||
args = Args()
|
_args = Args()
|
||||||
args.root_directory = parsed_args.root_directory
|
_args.root_directory = parsed_args.root_directory
|
||||||
args.web_root_url = parsed_args.web_root_url
|
_args.web_root_url = parsed_args.web_root_url
|
||||||
args.site_title = parsed_args.site_title
|
_args.site_title = parsed_args.site_title
|
||||||
args.regenerate_thumbnails = parsed_args.regenerate_thumbnails
|
_args.regenerate_thumbnails = parsed_args.regenerate_thumbnails
|
||||||
args.non_interactive_mode = parsed_args.non_interactive_mode
|
_args.non_interactive_mode = parsed_args.non_interactive_mode
|
||||||
args.use_fancy_folders = parsed_args.use_fancy_folders
|
_args.use_fancy_folders = parsed_args.use_fancy_folders
|
||||||
args.license_type = parsed_args.license_type
|
_args.license_type = parsed_args.license_type
|
||||||
args.author_name = parsed_args.author_name
|
_args.author_name = parsed_args.author_name
|
||||||
args.file_extensions = parsed_args.file_extensions
|
_args.file_extensions = parsed_args.file_extensions
|
||||||
args.theme_path = parsed_args.theme_path
|
_args.theme_path = parsed_args.theme_path
|
||||||
args.ignore_other_files = parsed_args.ignore_other_files
|
_args.ignore_other_files = parsed_args.ignore_other_files
|
||||||
args.exclude_folders = parsed_args.exclude_folders
|
_args.exclude_folders = parsed_args.exclude_folders
|
||||||
return args
|
return _args
|
||||||
|
|
||||||
|
|
||||||
def init_globals(args: Args) -> None:
|
def init_globals(_args: Args) -> Args:
|
||||||
global RAW_EXTENSIONS
|
global RAW_EXTENSIONS
|
||||||
if not args.file_extensions:
|
if not _args.file_extensions:
|
||||||
args.file_extensions = IMG_EXTENSIONS
|
_args.file_extensions = IMG_EXTENSIONS
|
||||||
if not args.exclude_folders:
|
if not _args.exclude_folders:
|
||||||
args.exclude_folders = NOT_LIST
|
_args.exclude_folders = NOT_LIST
|
||||||
args.root_directory = args.root_directory.rstrip("/") + "/"
|
_args.root_directory = _args.root_directory.rstrip("/") + "/"
|
||||||
args.web_root_url = args.web_root_url.rstrip("/") + "/"
|
_args.web_root_url = _args.web_root_url.rstrip("/") + "/"
|
||||||
|
|
||||||
RAW_EXTENSIONS = [ext.lower() for ext in RAW_EXTENSIONS] + [ext.upper() for ext in RAW_EXTENSIONS]
|
RAW_EXTENSIONS = [ext.lower() for ext in RAW_EXTENSIONS] + [ext.upper() for ext in RAW_EXTENSIONS]
|
||||||
|
|
||||||
|
return _args
|
||||||
|
|
||||||
|
|
||||||
def copy_static_files(args: Args) -> None:
|
def copy_static_files(args: Args) -> None:
|
||||||
shutil.copytree(STATIC_FILES_DIR, os.path.join(args.root_directory, ".static"), dirs_exist_ok=True)
|
shutil.copytree(STATIC_FILES_DIR, os.path.join(args.root_directory, ".static"), dirs_exist_ok=True)
|
||||||
@@ -108,13 +162,11 @@ def generate_thumbnail(arguments: Tuple[str, str]) -> None:
|
|||||||
img.save(path, "JPEG", quality=75, optimize=True)
|
img.save(path, "JPEG", quality=75, optimize=True)
|
||||||
except OSError:
|
except OSError:
|
||||||
print(f"Failed to generate thumbnail for {os.path.join(folder, item)}")
|
print(f"Failed to generate thumbnail for {os.path.join(folder, item)}")
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_total_folders(folder: str) -> None:
|
def get_total_folders(folder: str, _total: int = 0) -> int:
|
||||||
global total
|
|
||||||
|
|
||||||
total += 1
|
_total += 1
|
||||||
|
|
||||||
pbar.desc = f"Traversing filesystem - {folder}"
|
pbar.desc = f"Traversing filesystem - {folder}"
|
||||||
pbar.update(1)
|
pbar.update(1)
|
||||||
@@ -125,7 +177,8 @@ def get_total_folders(folder: str) -> None:
|
|||||||
if item not in EXCLUDES:
|
if item not in EXCLUDES:
|
||||||
if os.path.isdir(os.path.join(folder, item)):
|
if os.path.isdir(os.path.join(folder, item)):
|
||||||
if item not in args.exclude_folders:
|
if item not in args.exclude_folders:
|
||||||
get_total_folders(os.path.join(folder, item))
|
_total = get_total_folders(os.path.join(folder, item), _total)
|
||||||
|
return _total
|
||||||
|
|
||||||
|
|
||||||
def list_folder(folder: str, title: str) -> None:
|
def list_folder(folder: str, title: str) -> None:
|
||||||
@@ -175,7 +228,9 @@ def list_folder(folder: str, title: str) -> None:
|
|||||||
image_chunks = np.array_split(images, 8) if images else []
|
image_chunks = np.array_split(images, 8) if images else []
|
||||||
with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f:
|
with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f:
|
||||||
header = os.path.basename(folder) or title
|
header = os.path.basename(folder) or title
|
||||||
parent = None if not foldername else f"{args.web_root_url}{urllib.parse.quote(foldername.removesuffix(folder.split('/')[-1] + '/'))}"
|
parent = (
|
||||||
|
None if not foldername else f"{args.web_root_url}{urllib.parse.quote(foldername.removesuffix(folder.split('/')[-1] + '/'))}"
|
||||||
|
)
|
||||||
license_info: cclicense.License = (
|
license_info: cclicense.License = (
|
||||||
{
|
{
|
||||||
"project": args.site_title,
|
"project": args.site_title,
|
||||||
@@ -209,11 +264,10 @@ def list_folder(folder: str, title: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
global args, total, pbar, thumbnails
|
global args, pbar
|
||||||
total = 0
|
|
||||||
|
|
||||||
args = parse_arguments()
|
args = parse_arguments()
|
||||||
init_globals(args)
|
args = init_globals(args)
|
||||||
|
|
||||||
if os.path.exists(os.path.join(args.root_directory, ".lock")):
|
if os.path.exists(os.path.join(args.root_directory, ".lock")):
|
||||||
print("Another instance of this program is running.")
|
print("Another instance of this program is running.")
|
||||||
@@ -235,7 +289,7 @@ def main() -> None:
|
|||||||
pool.map(generate_thumbnail, thumbnails)
|
pool.map(generate_thumbnail, thumbnails)
|
||||||
else:
|
else:
|
||||||
pbar = tqdm(desc="Traversing filesystem", unit="folders", ascii=True, dynamic_ncols=True)
|
pbar = tqdm(desc="Traversing filesystem", unit="folders", ascii=True, dynamic_ncols=True)
|
||||||
get_total_folders(args.root_directory)
|
total = get_total_folders(args.root_directory)
|
||||||
pbar.desc = "Traversing filesystem"
|
pbar.desc = "Traversing filesystem"
|
||||||
pbar.update(0)
|
pbar.update(0)
|
||||||
pbar.close()
|
pbar.close()
|
||||||
@@ -247,7 +301,14 @@ def main() -> None:
|
|||||||
pbar.close()
|
pbar.close()
|
||||||
|
|
||||||
with Pool(os.cpu_count()) as pool:
|
with Pool(os.cpu_count()) as pool:
|
||||||
for _ in tqdm(pool.imap_unordered(generate_thumbnail, thumbnails), total=len(thumbnails), desc="Generating thumbnails", unit="files", ascii=True, dynamic_ncols=True):
|
for _ in tqdm(
|
||||||
|
pool.imap_unordered(generate_thumbnail, thumbnails),
|
||||||
|
total=len(thumbnails),
|
||||||
|
desc="Generating thumbnails",
|
||||||
|
unit="files",
|
||||||
|
ascii=True,
|
||||||
|
dynamic_ncols=True,
|
||||||
|
):
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
os.remove(os.path.join(args.root_directory, ".lock"))
|
os.remove(os.path.join(args.root_directory, ".lock"))
|
||||||
Reference in New Issue
Block a user