2018-10-28 21:49:15 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2018-11-03 13:00:06 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory, redirect, url_for
|
2018-11-05 20:57:26 +00:00
|
|
|
from random import choice
|
2018-11-06 19:13:35 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
2018-11-07 23:10:08 +00:00
|
|
|
from time import time
|
2018-11-10 14:18:43 +00:00
|
|
|
from hashlib import sha256
|
2018-11-10 15:26:35 +00:00
|
|
|
import click
|
|
|
|
from shutil import rmtree
|
2018-10-28 21:49:15 +00:00
|
|
|
|
2018-11-03 12:21:26 +00:00
|
|
|
import config
|
|
|
|
|
2018-10-28 21:49:15 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2018-11-10 15:26:35 +00:00
|
|
|
cache_resolution = (1920, 1080)
|
|
|
|
cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution)
|
|
|
|
|
2018-10-29 19:35:45 +00:00
|
|
|
|
2018-10-28 21:49:15 +00:00
|
|
|
@app.route("/")
|
2018-11-05 20:57:26 +00:00
|
|
|
def random():
|
2018-11-05 21:01:57 +00:00
|
|
|
return render_template("random.html", refresh=config.refresh)
|
2018-10-29 19:35:45 +00:00
|
|
|
|
2018-10-28 21:49:15 +00:00
|
|
|
|
2018-11-05 20:57:26 +00:00
|
|
|
@app.route("/random_image/")
|
2018-11-03 13:00:06 +00:00
|
|
|
def random_image():
|
2018-11-08 08:00:00 +00:00
|
|
|
img_glob = "*jpg"
|
2018-11-07 22:43:29 +00:00
|
|
|
imgdir = Path(config.imgdir)
|
2018-11-07 23:10:08 +00:00
|
|
|
last_modified_time, last_modified_file = max(
|
2018-11-08 08:00:00 +00:00
|
|
|
(f.stat().st_mtime, f) for f in imgdir.glob(img_glob)
|
2018-11-07 23:10:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if time() - last_modified_time <= 60:
|
|
|
|
selected_image = last_modified_file.relative_to(imgdir)
|
|
|
|
else:
|
2018-11-08 08:00:00 +00:00
|
|
|
images = list(imgdir.glob(img_glob))
|
2018-11-07 23:10:08 +00:00
|
|
|
selected_image = choice(images).relative_to(imgdir)
|
|
|
|
|
2018-11-07 22:43:29 +00:00
|
|
|
return redirect(url_for("image", filename=selected_image))
|
2018-11-03 13:00:06 +00:00
|
|
|
|
|
|
|
|
2018-11-03 12:21:26 +00:00
|
|
|
@app.route("/img/<path:filename>")
|
|
|
|
def image(filename):
|
2018-11-10 15:26:35 +00:00
|
|
|
cache_filename = create_cache_file(filename)
|
|
|
|
|
|
|
|
return send_from_directory(cache_dir, cache_filename)
|
|
|
|
|
|
|
|
|
|
|
|
def rm_cachedir():
|
|
|
|
if Path(config.cachedir).exists():
|
|
|
|
print("Removing cache dir", config.cachedir)
|
|
|
|
rmtree(config.cachedir)
|
|
|
|
|
2018-11-07 23:10:08 +00:00
|
|
|
|
2018-11-10 15:26:35 +00:00
|
|
|
def create_cachedir():
|
2018-11-08 08:00:00 +00:00
|
|
|
if not cache_dir.exists():
|
2018-11-10 14:18:43 +00:00
|
|
|
print("Creating cache dir", cache_dir)
|
2018-11-08 08:00:00 +00:00
|
|
|
cache_dir.mkdir(parents=True)
|
2018-11-07 23:10:08 +00:00
|
|
|
|
2018-11-10 15:26:35 +00:00
|
|
|
|
|
|
|
def create_cache_file(filename):
|
|
|
|
create_cachedir()
|
|
|
|
cache_file = get_cache_filename(filename)
|
2018-11-10 14:18:43 +00:00
|
|
|
|
|
|
|
if not (cache_dir / cache_file).exists():
|
|
|
|
print("Creating cache file", filename)
|
2018-11-10 15:26:35 +00:00
|
|
|
img = Image.open(Path(config.imgdir) / filename)
|
2018-11-08 08:00:00 +00:00
|
|
|
img.thumbnail(cache_resolution)
|
2018-11-10 14:18:43 +00:00
|
|
|
img.save(cache_dir / cache_file, "JPEG")
|
2018-11-06 19:13:35 +00:00
|
|
|
|
2018-11-10 15:26:35 +00:00
|
|
|
return cache_file
|
2018-11-03 12:21:26 +00:00
|
|
|
|
|
|
|
|
2018-11-10 15:26:35 +00:00
|
|
|
def get_cache_filename(filename):
|
|
|
|
original_file_path = Path(config.imgdir) / filename
|
|
|
|
return sha256(str(original_file_path.resolve()).encode("utf-8")).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def pre_cache_images():
|
|
|
|
imgdir = Path(config.imgdir)
|
|
|
|
for image_file in sorted(imgdir.glob("*.jpg")):
|
|
|
|
create_cache_file(image_file.relative_to(imgdir))
|
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option("--build-cache", is_flag=True, default=False, help="pre-cache images")
|
|
|
|
@click.option(
|
|
|
|
"--clear-cache", is_flag=True, default=False, help="clear cache directory"
|
|
|
|
)
|
|
|
|
def run_slider(build_cache, clear_cache):
|
|
|
|
if clear_cache:
|
|
|
|
rm_cachedir()
|
|
|
|
if build_cache:
|
|
|
|
pre_cache_images()
|
|
|
|
|
2018-10-28 21:49:15 +00:00
|
|
|
app.run()
|
2018-11-10 15:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run_slider()
|