Skip to content

Commit

Permalink
update.py improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarning committed Aug 4, 2024
1 parent d019769 commit b61b61e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import pathlib
import readline
import hashlib
import signal
Expand Down Expand Up @@ -170,13 +171,22 @@ def handle_image(i, n, prev, db, image):

def add_previews(db):
def find_images_paths(name):
image_exts = (".png", ".svg", ".pdf")
images = []
for entry in glob.glob("images/{}/*".format(name), recursive=True):
if not os.path.isfile(entry):
continue
if entry.endswith(".pdf") or entry.endswith(".png") or entry.endswith(".jpg") or entry.endswith(".svg"):
if entry.endswith(image_exts):
images.append(entry)
return images

# sort by extensions
def indexOf(ext):
for i, e in enumerate(image_exts):
if e == ext:
return i
return len(image_exts)

return sorted(images, key=lambda image: indexOf(pathlib.Path(image).suffix))

for name in db:
if not os.path.isfile("images/{}/preview.webp".format(name)):
Expand All @@ -185,6 +195,7 @@ def find_images_paths(name):

done = False
for path in image_paths:
print("Try with '{}'".format(path))
rc = os.system("convert -resize 300 '{}' 'images/{}/preview.webp'".format(path, name))
if rc == 0:
done = True
Expand Down Expand Up @@ -251,10 +262,10 @@ def get_image_set():
if not image.startswith("images/"):
filename = os.path.basename(image)
base = os.path.splitext(filename)[0].lower()
dst = "images/{}".format(base)
if not os.path.isdir(dst):
os.makedirs(dst)
shutil.copyfile(image, "{}/{}".format(dst, filename))
dst_folder = "images/{}".format(base)
if not os.path.isdir(dst_folder):
os.makedirs(dst_folder)
shutil.copyfile(image, "{}/{}".format(dst_folder, filename))
images.append(base)
elif os.path.isdir(image):
images.append(os.path.basename(image))
Expand All @@ -265,6 +276,7 @@ def get_image_set():
images = list(get_image_set() - set(db_images))

images.sort()
images = list(set(images)) # make list distinct

if check_duplicate_images():
print("Please remove duplicate files first!")
Expand Down

0 comments on commit b61b61e

Please sign in to comment.