imgin/imgin/__init__.py

81 lines
1.9 KiB
Python
Raw Permalink Normal View History

2021-10-03 00:56:33 +00:00
from gevent import monkey
from gevent import sleep
monkey.patch_all()
from threading import Thread
2021-10-03 22:26:25 +00:00
from os import remove, mkdir, path, stat
2021-10-03 00:56:33 +00:00
from shutil import rmtree
from uuid import uuid4
from bottle import route, run
from bottle import static_file
from bottle import SimpleTemplate
from .get import get
2022-01-06 05:17:08 +00:00
from .config import IMAGE_CACHE, template_dir, bind_ip, bind_port
2021-10-03 00:56:33 +00:00
2021-10-03 22:26:25 +00:00
def get_timestamp_of_file(file):
return stat(file).st_ctime
2021-10-03 00:56:33 +00:00
def album(id):
req = IMAGE_CACHE
2022-01-09 20:26:35 +00:00
title, metas = get("a/" + id, req)
2022-01-09 19:30:21 +00:00
found_list_file = IMAGE_CACHE + ("a/" + id).replace('/', '_')
2021-10-03 00:56:33 +00:00
2021-10-05 18:40:48 +00:00
with open(found_list_file, 'r') as f:
imgs = f.read().split(',')
for c, img in enumerate(imgs):
imgs[c] = IMAGE_CACHE + imgs[c]
2021-10-03 22:26:25 +00:00
# sort image order (file creation time)
imgs = sorted(imgs, key=get_timestamp_of_file)
2021-10-03 00:56:33 +00:00
for c, img in enumerate(imgs):
2022-01-09 20:26:35 +00:00
imgs[c] = (img.replace(IMAGE_CACHE, '/'), metas[c][0], metas[c][1])
2021-10-03 22:26:25 +00:00
2021-10-03 00:56:33 +00:00
with open(f'{template_dir}gallery.html', 'r') as img_view:
tpl = SimpleTemplate(img_view)
2022-01-09 19:52:28 +00:00
return tpl.render(imgs=imgs, title=title)
2021-10-03 00:56:33 +00:00
@route('/')
@route('')
def home():
return static_file("index.html", root=template_dir)
@route('/static/<file>')
def static(file=''):
return static_file(file, root=template_dir)
@route('/gallery/<id>')
def gallery(id=''):
return album(id)
@route('/a/<id>')
def gallery(id=''):
return album(id)
@route('/<img>')
def img(img=''):
if not img.endswith("jpeg") and not img.endswith("jpg") and not img.endswith("png"):
img = img + ".jpg"
2021-10-05 18:40:48 +00:00
img = img.replace('jpeg', 'jpg')
2021-10-03 00:56:33 +00:00
if not path.exists(IMAGE_CACHE + img):
get(img, IMAGE_CACHE)
return static_file(img, root=IMAGE_CACHE)
def start_server():
try:
rmtree(IMAGE_CACHE)
except FileNotFoundError:
pass
mkdir(IMAGE_CACHE)
2022-01-06 05:17:08 +00:00
run(server='gevent', host=bind_ip, port=bind_port)