Files
web-framework-resources/plugins/thumbnails/plugin.py
T

143 lines
6.6 KiB
Python

"""AnimeSchedule lookup plus bounded, authenticated local poster caching."""
import hashlib
import json
import os
from pathlib import Path
import tempfile
from urllib.parse import urlencode, urljoin, urlsplit
import requests
from flask import Blueprint, render_template, request, flash, redirect, url_for, send_file, abort
from web_framework.auth import require_permission
from web_framework.plugins import Plugin, Setting, get_plugins
from web_framework.plugin_runtime import Service, QUEUE_SETTING, text, http_bytes, run_one
from .title_matching import normalize_identity_title_key
bp = Blueprint('plugin_thumbnails', __name__, template_folder='templates')
API = 'https://animeschedule.net/api/v3/anime'
IMAGES = 'https://img.animeschedule.net/production/assets/public/img/'
class Thumbnails(Service):
plugin_id = 'thumbnails'
@staticmethod
def key(title):
return hashlib.sha256(text(title, 'Title', 200).casefold().encode()).hexdigest()
def request(self, title, image_url=''):
self.check()
title = text(title, 'Title', 200)
key = self.key(title)
existing = self.state.items('thumbnails')
cached = next((item for item in existing if item['id'] == key), None)
if cached and (self.directory() / (key + '.image')).exists():
return cached.get('job')
job = self.enqueue('Thumbnail: ' + title[:140], {'title': title, 'image_url': image_url})
return job
def get(self, title):
self.check()
key = self.key(title)
if (self.directory() / (key + '.image')).is_file():
return {'key': key, 'url': url_for('plugin_thumbnails.image', key=key)}
return None
def find_image(self, title):
payload, _ = http_bytes(API + '?' + urlencode({'q': title, 'st': 'alphabetic'}), limit=2 * 1024 * 1024)
try:
entries = json.loads(payload)
except ValueError:
raise ValueError('AnimeSchedule returned invalid metadata.') from None
if isinstance(entries, dict):
entries = entries.get('anime') or entries.get('data') or entries.get('results') or []
wanted = normalize_identity_title_key(title)
for item in entries if isinstance(entries, list) else []:
if not isinstance(item, dict):
continue
names = [item.get('title'), item.get('english'), item.get('romaji'), item.get('native')]
if any(normalize_identity_title_key(value) == wanted for value in names if value):
route = item.get('imageVersionRoute')
if route:
return urljoin(IMAGES, route)
raise ValueError('No matching anime thumbnail was found. Try its exact metadata title.')
def process(self, payload, progress):
title = text(payload.get('title'), 'Title', 200)
settings = self.settings()
progress(0, 2, 'Looking up thumbnail metadata.')
url = payload.get('image_url', '') if settings['source'] == 'provider-url' else self.find_image(title)
parsed = urlsplit(text(url, 'Image URL', 2048))
allowed = {value.strip().lower() for value in settings['hosts'].split(',') if value.strip()}
if parsed.scheme != 'https' or parsed.hostname not in allowed or parsed.username or parsed.password or parsed.port not in (None, 443):
raise ValueError('Thumbnail host is not allowed. Configure trusted HTTPS image hosts in plugin settings.')
try:
with requests.get(url, stream=True, timeout=(5, 30), allow_redirects=False) as response:
if response.status_code != 200:
raise ValueError('Thumbnail server rejected the request or redirected it.')
data = bytearray()
for chunk in response.iter_content(65536):
data.extend(chunk)
if len(data) > 8 * 1024 * 1024:
raise ValueError('Thumbnail exceeds 8 MiB.')
except requests.RequestException:
raise ValueError('Thumbnail download failed.') from None
data = bytes(data)
mime = ('image/png' if data.startswith(b'\x89PNG\r\n\x1a\n') else 'image/jpeg' if data.startswith(b'\xff\xd8\xff') else
'image/gif' if data.startswith((b'GIF87a', b'GIF89a')) else 'image/webp' if data.startswith(b'RIFF') and data[8:12] == b'WEBP' else '')
if not mime:
raise ValueError('Thumbnail is not a supported PNG, JPEG, GIF, or WebP image.')
progress(1, 2, 'Saving thumbnail cache.')
key = self.key(title)
fd, temporary = tempfile.mkstemp(dir=self.directory())
try:
with os.fdopen(fd, 'wb') as output:
output.write(data)
self.check()
os.replace(temporary, self.directory() / (key + '.image'))
finally:
Path(temporary).unlink(missing_ok=True)
self.state.put_item('thumbnails', {'title': title, 'mime': mime}, key)
progress(2, 2, 'Thumbnail cached.')
def service():
return get_plugins().service('thumbnails', 'thumbnails')
@bp.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
return request_image()
return render_template('thumbnails/index.html', title='Thumbnails', images=service().state.items('thumbnails'))
@require_permission('content.write')
def request_image():
try:
job = service().request(request.form.get('title', ''), request.form.get('image_url', ''))
flash('Thumbnail requested. Check the Queue for progress.' if job else 'Thumbnail is already cached.', 'success')
except ValueError as exc:
flash(str(exc), 'error')
return redirect(url_for('plugin_thumbnails.index'))
@bp.get('/images/<key>')
def image(key):
if len(key) != 64 or any(value not in '0123456789abcdef' for value in key):
abort(404)
entry = next((item for item in service().state.items('thumbnails') if item['id'] == key), None)
if not entry:
abort(404)
path = service().directory() / (key + '.image')
if not path.is_file():
abort(404)
return send_file(path, mimetype=entry['mime'])
plugin = Plugin('thumbnails', 'Thumbnails', description='Anime poster lookup and an authenticated persistent image cache.', blueprint=bp,
endpoint='plugin_thumbnails.index', services={'thumbnails': Thumbnails}, workers={'download': lambda: run_one(service())}, settings_schema={
'queue': QUEUE_SETTING,
'source': Setting('Thumbnail source', kind='choice', choices=('animeschedule', 'provider-url'), default='animeschedule'),
'hosts': Setting('Allowed image hosts (comma separated)', default='img.animeschedule.net,s4.anilist.co'),
})