From 13dc09c5b390900dd270b40fbfdeff54bf0d6efe Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 8 Nov 2018 09:00:00 +0100 Subject: [PATCH] Configure locatoion of cache dir --- .gitignore | 1 + config.py | 1 + slider.py | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index c18dd8d..f9d5764 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__/ +.* diff --git a/config.py b/config.py index ced3d85..5e666a9 100644 --- a/config.py +++ b/config.py @@ -1,2 +1,3 @@ imgdir = "." refresh = 5 +cachedir = ".webslider" diff --git a/slider.py b/slider.py index 4fa7cff..9b43e77 100755 --- a/slider.py +++ b/slider.py @@ -18,15 +18,16 @@ def random(): @app.route("/random_image/") def random_image(): + img_glob = "*jpg" imgdir = Path(config.imgdir) last_modified_time, last_modified_file = max( - (f.stat().st_mtime, f) for f in imgdir.glob("*.jpg") + (f.stat().st_mtime, f) for f in imgdir.glob(img_glob) ) if time() - last_modified_time <= 60: selected_image = last_modified_file.relative_to(imgdir) else: - images = list(imgdir.glob("*.jpg")) + images = list(imgdir.glob(img_glob)) selected_image = choice(images).relative_to(imgdir) return redirect(url_for("image", filename=selected_image)) @@ -34,17 +35,18 @@ def random_image(): @app.route("/img/") def image(filename): - scaled_img_dir = Path(config.imgdir) / ".slider" / "fhd" + cache_resolution = (1920, 1080) + cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution) - if not scaled_img_dir.exists(): - scaled_img_dir.mkdir(parents=True) + if not cache_dir.exists(): + cache_dir.mkdir(parents=True) - if not (scaled_img_dir / filename).exists(): + if not (cache_dir / filename).exists(): img = Image.open(Path(config.imgdir) / filename) - img.thumbnail((1920, 1080)) - img.save(scaled_img_dir / filename) + img.thumbnail(cache_resolution) + img.save(cache_dir / filename) - return send_from_directory(scaled_img_dir, filename) + return send_from_directory(cache_dir, filename) if __name__ == "__main__":