mirror of
https://github.com/greflm13/StaticGalleryBuilder.git
synced 2026-02-05 02:59:27 +00:00
going home to fix...
This commit is contained in:
213
builder.py
213
builder.py
@@ -4,7 +4,7 @@ import argparse
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
import shutil
|
import shutil
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import time
|
import json
|
||||||
from multiprocessing import Pool
|
from multiprocessing import Pool
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
@@ -140,106 +140,119 @@ def get_total_folders(folder: str, _total: int = 0) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def list_folder(folder: str, title: str) -> None:
|
def list_folder(folder: str, title: str) -> None:
|
||||||
items = os.listdir(folder)
|
sizelist: Dict[Dict[str, int], Dict[str, int]] = {}
|
||||||
items.sort()
|
with open(os.path.join(folder, "sizelist.json"), "w+", encoding="utf-8") as sizelistfile:
|
||||||
images: List[Dict[str, Any]] = []
|
sizelist = json.loads(sizelistfile.read())
|
||||||
subfolders: List[Dict[str, str]] = []
|
items = os.listdir(folder)
|
||||||
foldername = folder.removeprefix(args.root_directory)
|
items.sort()
|
||||||
foldername = f"{foldername}/" if foldername else ""
|
images: List[Dict[str, Any]] = []
|
||||||
baseurl = urllib.parse.quote(foldername)
|
subfolders: List[Dict[str, str]] = []
|
||||||
if not os.path.exists(os.path.join(args.root_directory, ".thumbnails", foldername)):
|
foldername = folder.removeprefix(args.root_directory)
|
||||||
os.mkdir(os.path.join(args.root_directory, ".thumbnails", foldername))
|
foldername = f"{foldername}/" if foldername else ""
|
||||||
contains_files = False
|
baseurl = urllib.parse.quote(foldername)
|
||||||
if not args.non_interactive_mode:
|
if not os.path.exists(os.path.join(args.root_directory, ".thumbnails", foldername)):
|
||||||
imgpbar = tqdm(total=len(items), desc=f"Getting image info - {folder}", unit="files", ascii=True, dynamic_ncols=True)
|
os.mkdir(os.path.join(args.root_directory, ".thumbnails", foldername))
|
||||||
for item in items:
|
contains_files = False
|
||||||
if item not in EXCLUDES:
|
|
||||||
if os.path.isdir(os.path.join(folder, item)):
|
|
||||||
subfolder = {"url": f"{args.web_root_url}{baseurl}{urllib.parse.quote(item)}", "name": item}
|
|
||||||
subfolders.append(subfolder)
|
|
||||||
if item not in args.exclude_folders:
|
|
||||||
skip = False
|
|
||||||
for exclude in args.exclude_folders:
|
|
||||||
if fnmatch.fnmatchcase(os.path.join(folder, item), exclude):
|
|
||||||
skip = True
|
|
||||||
if not skip:
|
|
||||||
list_folder(os.path.join(folder, item), os.path.join(folder, item).removeprefix(args.root_directory))
|
|
||||||
else:
|
|
||||||
extsplit = os.path.splitext(item)
|
|
||||||
contains_files = True
|
|
||||||
if extsplit[1].lower() in args.file_extensions:
|
|
||||||
with Image.open(os.path.join(folder, item)) as img:
|
|
||||||
width, height = img.size
|
|
||||||
image = {
|
|
||||||
"url": f"{args.web_root_url}{baseurl}{urllib.parse.quote(item)}",
|
|
||||||
"thumbnail": f"{args.web_root_url}.thumbnails/{baseurl}{urllib.parse.quote(extsplit[0])}.jpg",
|
|
||||||
"name": item,
|
|
||||||
"width": width,
|
|
||||||
"height": height,
|
|
||||||
}
|
|
||||||
if not os.path.exists(os.path.join(args.root_directory, ".thumbnails", foldername, item)):
|
|
||||||
thumbnails.append((folder, item))
|
|
||||||
for raw in RAW_EXTENSIONS:
|
|
||||||
if os.path.exists(os.path.join(folder, extsplit[0] + raw)):
|
|
||||||
url = urllib.parse.quote(extsplit[0]) + raw
|
|
||||||
if raw in (".tif", ".tiff"):
|
|
||||||
image["tiff"] = f"{args.web_root_url}{baseurl}{url}"
|
|
||||||
else:
|
|
||||||
image["raw"] = f"{args.web_root_url}{baseurl}{url}"
|
|
||||||
images.append(image)
|
|
||||||
if item == "info":
|
|
||||||
with open(os.path.join(folder, item), encoding="utf-8") as f:
|
|
||||||
_info = f.read()
|
|
||||||
info[urllib.parse.quote(folder)] = _info
|
|
||||||
if not args.non_interactive_mode:
|
if not args.non_interactive_mode:
|
||||||
imgpbar.update(1)
|
imgpbar = tqdm(total=len(items), desc=f"Getting image info - {folder}", unit="files", ascii=True, dynamic_ncols=True)
|
||||||
pbar.update(0)
|
for item in items:
|
||||||
if not contains_files and not args.use_fancy_folders:
|
if item not in EXCLUDES:
|
||||||
return
|
if os.path.isdir(os.path.join(folder, item)):
|
||||||
if images or (args.use_fancy_folders and not contains_files) or (args.use_fancy_folders and args.ignore_other_files):
|
subfolder = {"url": f"{args.web_root_url}{baseurl}{urllib.parse.quote(item)}", "name": item}
|
||||||
image_chunks = np.array_split(images, 8) if images else []
|
subfolders.append(subfolder)
|
||||||
with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f:
|
if item not in args.exclude_folders:
|
||||||
_info: List[str] = None
|
skip = False
|
||||||
header = os.path.basename(folder) or title
|
for exclude in args.exclude_folders:
|
||||||
parent = None if not foldername else f"{args.web_root_url}{urllib.parse.quote(foldername.removesuffix(folder.split('/')[-1] + '/'))}"
|
if fnmatch.fnmatchcase(os.path.join(folder, item), exclude):
|
||||||
license_info: cclicense.License = (
|
skip = True
|
||||||
{
|
if not skip:
|
||||||
"project": args.site_title,
|
list_folder(os.path.join(folder, item), os.path.join(folder, item).removeprefix(args.root_directory))
|
||||||
"author": args.author_name,
|
else:
|
||||||
"type": cclicense.licensenameswitch(args.license_type),
|
extsplit = os.path.splitext(item)
|
||||||
"url": cclicense.licenseurlswitch(args.license_type),
|
contains_files = True
|
||||||
"pics": cclicense.licensepicswitch(args.license_type),
|
if extsplit[1].lower() in args.file_extensions:
|
||||||
}
|
if not sizelist.get(item):
|
||||||
if args.license_type
|
print("fuck")
|
||||||
else None
|
with Image.open(os.path.join(folder, item)) as img:
|
||||||
)
|
width, height = img.size
|
||||||
if urllib.parse.quote(folder) in info:
|
sizelist[item] = {"width": width, "height": height}
|
||||||
_info = []
|
print(sizelist)
|
||||||
_infolst = info[urllib.parse.quote(folder)].split("\n")
|
|
||||||
for i in _infolst:
|
image = {
|
||||||
if len(i) > 1:
|
"url": f"{args.web_root_url}{baseurl}{urllib.parse.quote(item)}",
|
||||||
_info.append(i)
|
"thumbnail": f"{args.web_root_url}.thumbnails/{baseurl}{urllib.parse.quote(extsplit[0])}.jpg",
|
||||||
html = env.get_template("index.html.j2")
|
"name": item,
|
||||||
content = html.render(
|
"width": sizelist[item]["width"],
|
||||||
title=title,
|
"height": sizelist[item]["height"],
|
||||||
favicon=f"{args.web_root_url}{FAVICON_PATH}",
|
}
|
||||||
stylesheet=f"{args.web_root_url}{GLOBAL_CSS_PATH}",
|
if not os.path.exists(os.path.join(args.root_directory, ".thumbnails", foldername, item)):
|
||||||
theme=f"{args.web_root_url}.static/theme.css",
|
thumbnails.append((folder, item))
|
||||||
root=args.web_root_url,
|
for raw in RAW_EXTENSIONS:
|
||||||
parent=parent,
|
if os.path.exists(os.path.join(folder, extsplit[0] + raw)):
|
||||||
header=header,
|
url = urllib.parse.quote(extsplit[0]) + raw
|
||||||
license=license_info,
|
if raw in (".tif", ".tiff"):
|
||||||
subdirectories=subfolders,
|
image["tiff"] = f"{args.web_root_url}{baseurl}{url}"
|
||||||
images=image_chunks,
|
else:
|
||||||
info=_info,
|
image["raw"] = f"{args.web_root_url}{baseurl}{url}"
|
||||||
allimages=images,
|
images.append(image)
|
||||||
)
|
if item == "info":
|
||||||
f.write(content)
|
with open(os.path.join(folder, item), encoding="utf-8") as f:
|
||||||
else:
|
_info = f.read()
|
||||||
if os.path.exists(os.path.join(folder, "index.html")):
|
info[urllib.parse.quote(folder)] = _info
|
||||||
os.remove(os.path.join(folder, "index.html"))
|
if not args.non_interactive_mode:
|
||||||
if not args.non_interactive_mode:
|
imgpbar.update(1)
|
||||||
pbar.update(1)
|
pbar.update(0)
|
||||||
|
sizelistfile.write(json.dumps(sizelist, indent=4))
|
||||||
|
if not contains_files and not args.use_fancy_folders:
|
||||||
|
return
|
||||||
|
if images or (args.use_fancy_folders and not contains_files) or (args.use_fancy_folders and args.ignore_other_files):
|
||||||
|
image_chunks = np.array_split(images, 8) if images else []
|
||||||
|
with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f:
|
||||||
|
_info: List[str] = None
|
||||||
|
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] + '/'))}"
|
||||||
|
)
|
||||||
|
license_info: cclicense.License = (
|
||||||
|
{
|
||||||
|
"project": args.site_title,
|
||||||
|
"author": args.author_name,
|
||||||
|
"type": cclicense.licensenameswitch(args.license_type),
|
||||||
|
"url": cclicense.licenseurlswitch(args.license_type),
|
||||||
|
"pics": cclicense.licensepicswitch(args.license_type),
|
||||||
|
}
|
||||||
|
if args.license_type
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
if urllib.parse.quote(folder) in info:
|
||||||
|
_info = []
|
||||||
|
_infolst = info[urllib.parse.quote(folder)].split("\n")
|
||||||
|
for i in _infolst:
|
||||||
|
if len(i) > 1:
|
||||||
|
_info.append(i)
|
||||||
|
html = env.get_template("index.html.j2")
|
||||||
|
content = html.render(
|
||||||
|
title=title,
|
||||||
|
favicon=f"{args.web_root_url}{FAVICON_PATH}",
|
||||||
|
stylesheet=f"{args.web_root_url}{GLOBAL_CSS_PATH}",
|
||||||
|
theme=f"{args.web_root_url}.static/theme.css",
|
||||||
|
root=args.web_root_url,
|
||||||
|
parent=parent,
|
||||||
|
header=header,
|
||||||
|
license=license_info,
|
||||||
|
subdirectories=subfolders,
|
||||||
|
images=image_chunks,
|
||||||
|
info=_info,
|
||||||
|
allimages=images,
|
||||||
|
)
|
||||||
|
f.write(content)
|
||||||
|
else:
|
||||||
|
if os.path.exists(os.path.join(folder, "index.html")):
|
||||||
|
os.remove(os.path.join(folder, "index.html"))
|
||||||
|
if not args.non_interactive_mode:
|
||||||
|
pbar.update(1)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
|||||||
BIN
test/example/example copy.jpg
Normal file
BIN
test/example/example copy.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.8 MiB |
10
test/example/sizelist.json
Normal file
10
test/example/sizelist.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"example copy.jpg": {
|
||||||
|
"width": 7030,
|
||||||
|
"height": 4688
|
||||||
|
},
|
||||||
|
"example.jpg": {
|
||||||
|
"width": 7030,
|
||||||
|
"height": 4688
|
||||||
|
}
|
||||||
|
}
|
||||||
0
test/sizelist.json
Normal file
0
test/sizelist.json
Normal file
Reference in New Issue
Block a user