This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boop.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. import re
  5. from jinja2 import Environment, PackageLoader, select_autoescape
  6. from configuration import THEME
  7. PICTURES_DIR_NAME = "photos"
  8. OUTPUT_DIR_NAME = "output"
  9. def list_galleries_in(path):
  10. gallery_dirs = [f for f in os.scandir(path) if f.is_dir()]
  11. for gallery_dir in gallery_dirs:
  12. photos = list(list_photos_in(gallery_dir))
  13. if len(photos) == 0:
  14. continue
  15. gallery = {
  16. "name": gallery_dir.name,
  17. "path": gallery_dir.path,
  18. "url": gallery_dir.name,
  19. "num_photos": len(photos),
  20. "photos": photos,
  21. "cover_photo": photos[0],
  22. }
  23. yield gallery
  24. def list_photos_in(gallery_dir):
  25. photo_files = [
  26. f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I)
  27. ]
  28. for photo_file in photo_files:
  29. url = os.path.join(gallery_dir.name, photo_file.name)
  30. photo = {"name": photo_file.name, "url": url}
  31. yield photo
  32. def generate_output_dir():
  33. output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
  34. shutil.rmtree(output_path)
  35. if not os.path.isdir(output_path):
  36. os.mkdir(output_path)
  37. return output_path
  38. def generate_style(output_path):
  39. style_path = os.path.join(os.curdir, THEME, "style")
  40. style_output_path = os.path.join(output_path, "style")
  41. shutil.copytree(style_path, style_output_path)
  42. def generate_index(output_path, galleries):
  43. index_path = os.path.join(output_path, "index.html")
  44. theme_path = os.path.join(os.curdir, THEME)
  45. jinja_env = Environment(
  46. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  47. )
  48. index_template = jinja_env.get_template("index.html.j2")
  49. with open(index_path, "w") as index_file:
  50. index_file.write(index_template.render(galleries=galleries))
  51. def generate_gallery(output_path, gallery):
  52. generate_gallery_index(output_path, gallery)
  53. generate_gallery_dir(output_path, gallery)
  54. def generate_gallery_index(output_path, gallery):
  55. gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html")
  56. theme_path = os.path.join(os.curdir, THEME)
  57. jinja_env = Environment(
  58. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  59. )
  60. gallery_template = jinja_env.get_template("gallery.html.j2")
  61. with open(gallery_index_path, "w") as gallery_file:
  62. gallery_file.write(gallery_template.render(gallery=gallery))
  63. def generate_gallery_dir(output_path, gallery):
  64. gallery_output_path = os.path.join(output_path, gallery["name"])
  65. if os.path.isdir(gallery_output_path):
  66. os.mkdir(gallery_output_path)
  67. shutil.copytree(gallery["path"], gallery_output_path)
  68. def main():
  69. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  70. galleries = list(list_galleries_in(pictures_folder))
  71. if len(galleries) == 0:
  72. return
  73. output_path = generate_output_dir()
  74. generate_style(output_path)
  75. generate_index(output_path, galleries)
  76. for gallery in galleries:
  77. generate_gallery(output_path, gallery)
  78. if __name__ == "__main__":
  79. main()