Skip to content

Commit

Permalink
Smarter generation of everything
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapJoris committed Jun 12, 2022
1 parent 378a856 commit f5f4b09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/photog
Expand Up @@ -11,4 +11,4 @@ if __name__ == "__main__":
static_dir = os.path.join(os.path.dirname(photog.__file__), "static")
copytree(static_dir, os.path.join(root[0], "static"), dirs_exist_ok=True)
else:
photog.create_website(".")
sys.exit(photog.create_website("."))
37 changes: 30 additions & 7 deletions photog/__init__.py
Expand Up @@ -26,6 +26,10 @@ def create_website(root="."):
Walk the directory tree, rename images, and generate indexes.
"""

# Somewhat unfair, but it's nice to say
# `if photog; then aws s3 sync` in Bash
exit_status = 1

for (dir, dirs, files) in os.walk(root):
if dir.startswith("./."):
continue
Expand All @@ -36,9 +40,27 @@ def create_website(root="."):
if not any([file.lower().endswith(".jpg") for file in files]):
continue

os.makedirs(os.path.join(dir, "thumbnails"), exist_ok=True)
photos = rename_images(dir)
generate_index(dir, photos)
if not os.path.exists(os.path.join(dir, "index.html")):
exit_status = 0
process_directory(dir)
else:
for image in glob("*.jpg", root_dir=dir):
basename = image.split(".", maxsplit=1)[0]
if os.path.getmtime(
os.path.join(dir, "thumbnails", basename + " (large)" + ".jpg")
) < os.path.getmtime(os.path.join(dir, image)):
exit_status = 0
process_directory(dir)
break

return exit_status


def process_directory(dir):
print(f"Processing {dir}...")
os.makedirs(os.path.join(dir, "thumbnails"), exist_ok=True)
photos = rename_images(dir)
generate_index(dir, photos)


def rename_images(dir):
Expand Down Expand Up @@ -115,11 +137,14 @@ def generate_index(dir, photos):
exif = im.info["exif"]

try:
update_file = os.path.getmtime(os.path.join(dir, large_thumbnail)) < os.path.getmtime(path)
update_file = os.path.getmtime(
os.path.join(dir, large_thumbnail)
) < os.path.getmtime(path)
except:
update_file = True
if update_file:
# Generate S, M and L thumbnails
print(f"Generating thumbnails for {path}...")
im.thumbnail((L, 99999))
im.save(os.path.join(dir, large_thumbnail), quality=95, exif=exif)
im.thumbnail((M, 99999))
Expand Down Expand Up @@ -149,10 +174,8 @@ def generate_index(dir, photos):
}
)

print(path)

if options.get("zip", True):
print(zippath)
print("(Re)creating all.zip...")
zipfile.close()

index = T.render(
Expand Down

0 comments on commit f5f4b09

Please sign in to comment.