WebSlider/slider.py

128 lines
3.1 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
from flask import (
Flask,
render_template,
send_from_directory,
redirect,
url_for,
request,
)
from random import choice
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
import click
from shutil import rmtree
2018-11-10 19:31:34 +00:00
from tempfile import gettempdir
import config
app = Flask(__name__)
2018-11-10 15:23:04 +00:00
imgdir = Path(config.imgdir).expanduser().resolve()
cache_resolution = config.resolution
2018-11-10 19:31:34 +00:00
cache_dir = (
Path(gettempdir()) / "webslider"
if config.cachedir == "auto"
else Path(config.cachedir)
) / ("%sx%s" % cache_resolution)
2018-10-29 19:35:45 +00:00
def find_images():
images = set()
for filetype in config.filetypes:
glob = "**/" + filetype if config.recursive else filetype
images.update(imgdir.glob(glob))
return list(images)
@app.route("/")
def random():
print(request.remote_addr, request.base_url)
2018-11-05 21:01:57 +00:00
return render_template("random.html", refresh=config.refresh)
2018-10-29 19:35:45 +00:00
@app.route("/random_image/")
def random_image():
print(request.remote_addr, request.base_url)
2018-11-10 17:33:27 +00:00
try:
last_modified_time, last_modified_file = max(
(f.stat().st_mtime, f) for f in find_images()
2018-11-10 17:33:27 +00:00
)
if time() - last_modified_time <= 60:
selected_image = last_modified_file.relative_to(imgdir)
else:
images = find_images()
2018-11-10 17:33:27 +00:00
selected_image = choice(images).relative_to(imgdir)
except ValueError:
return redirect(url_for("static", filename="clear.gif"))
2018-11-07 23:10:08 +00:00
return redirect(
url_for("image", filename=selected_image)
+ "?hash=%s" % get_cache_filename(selected_image)
)
@app.route("/img/<path:filename>")
def image(filename):
print(request.remote_addr, request.base_url)
cache_filename = create_cache_file(filename)
return send_from_directory(cache_dir, cache_filename)
def rm_cachedir():
2018-11-10 19:31:34 +00:00
if cache_dir.exists():
print("Removing cache dir", cache_dir)
rmtree(cache_dir)
2018-11-07 23:10:08 +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
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:23:04 +00:00
img = Image.open(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")
return cache_file
def get_cache_filename(filename):
2018-11-10 15:23:04 +00:00
original_file_path = imgdir / filename
return sha256(str(original_file_path.resolve()).encode("utf-8")).hexdigest()
def pre_cache_images():
for image_file in sorted(find_images()):
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-11-10 19:31:34 +00:00
app.run(host="0.0.0.0")
if __name__ == "__main__":
run_slider()