From 7397e9fc78c42f49abe4e5acda42ac26698d3be2 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Fri, 19 Jun 2020 00:29:58 -0600 Subject: add word wrapping and improve source links --- assets/templates/classic/info.json | 4 ++++ assets/templates/garfield/info.json | 4 ++++ assets/templates/otamatone/info.json | 4 ++++ bot.py | 44 ++++++++++++++++++++++++++++++------ templates/index.html | 9 ++++---- 5 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 assets/templates/classic/info.json create mode 100644 assets/templates/garfield/info.json create mode 100644 assets/templates/otamatone/info.json diff --git a/assets/templates/classic/info.json b/assets/templates/classic/info.json new file mode 100644 index 0000000..4372f8f --- /dev/null +++ b/assets/templates/classic/info.json @@ -0,0 +1,4 @@ +{ + "name": "Classic", + "source": "https://youtu.be/LDU_Txk06tM" +} diff --git a/assets/templates/garfield/info.json b/assets/templates/garfield/info.json new file mode 100644 index 0000000..0ec371d --- /dev/null +++ b/assets/templates/garfield/info.json @@ -0,0 +1,4 @@ +{ + "name": "Garfield (original by @jondancesto)", + "source": "https://twitter.com/jondancesto/status/1199796307218427904" +} diff --git a/assets/templates/otamatone/info.json b/assets/templates/otamatone/info.json new file mode 100644 index 0000000..40c0a39 --- /dev/null +++ b/assets/templates/otamatone/info.json @@ -0,0 +1,4 @@ +{ + "name": "Otamatone (original by TheRealSullyG)", + "source": "https://youtu.be/VI5I3MuKJlo" +} diff --git a/bot.py b/bot.py index d9392ff..e7b794f 100644 --- a/bot.py +++ b/bot.py @@ -8,13 +8,14 @@ import subprocess import tempfile from queue import Queue from threading import Thread +import json import telegram from telegram.ext import Dispatcher, MessageHandler, CommandHandler, InlineQueryHandler, Filters, CallbackContext from dotenv import load_dotenv, find_dotenv from PIL import Image, ImageDraw, ImageFont import libhoney -from flask import Flask, render_template, request, make_response +from flask import Flask, render_template, request app = Flask(__name__) load_dotenv(find_dotenv()) @@ -33,7 +34,7 @@ class Style: """Crab rave style""" id: str name: str - desc: str + source: str image: Path video: Path @@ -43,17 +44,45 @@ def get_styles(): result = [] for folder in templates.iterdir(): s_id = folder.name - name = s_id - desc = s_id + with list(folder.glob('*.json'))[0].open('r') as meta: + metadata = json.load(meta) + name = metadata['name'] + source = metadata['source'] image = list(folder.glob('*.png'))[0] video = list(folder.glob('*.mp4'))[0] - result.append(Style(s_id, name, desc, image, video)) + result.append(Style(s_id, name, source, image, video)) return result STYLES = get_styles() +def word_wrap(text: str, draw: ImageDraw.ImageDraw, width: int) -> str: + text_width, text_height = draw.multiline_textsize(text, font=font) + if text_width > width: + lines = text.splitlines() + wrapped_text = [] + for line in lines: + line_width, _ = draw.textsize(line, font=font) + if line_width > width: + new_line = '' + last_line = new_line + for word in line.split(): + if len(new_line) > 0: + new_line += ' ' + new_line += word + line_width, _ = draw.textsize(new_line, font=font) + if line_width > width: + wrapped_text.append(last_line) + new_line = word + last_line = new_line + if len(last_line) > 0: + wrapped_text.append(last_line) + else: + wrapped_text.append(line) + return '\n'.join(wrapped_text) + + def render_text(text: str, base: Image): if base.mode == 'RGB': white = (255, 255, 255) @@ -65,11 +94,12 @@ def render_text(text: str, base: Image): raise ValueError('Base image {}, not RGB/RGBA'.format(base.mode)) draw = ImageDraw.Draw(base) + text = word_wrap(text, draw, base.width) text_width, text_height = draw.multiline_textsize(text, font=font) center_x = base.width // 2 center_y = base.height // 2 draw.multiline_text((center_x - text_width / 2, center_y - text_height / 2), text, font=font, - fill=white, stroke_width=1, stroke_fill=black) + fill=white, stroke_width=1, stroke_fill=black, align='center') def make_image(text: str, style_id: str): @@ -151,7 +181,7 @@ def message(update: telegram.Update, context: CallbackContext): @app.route('/') def index(): - return render_template('index.html') + return render_template('index.html', styles=STYLES) @app.route('/render') diff --git a/templates/index.html b/templates/index.html index fa7bdd6..26673d6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,11 +11,10 @@

Style: - - (original by TheRealSullyG) - (original by @jondancesto) + {% for style in styles %} + + (source) + {% endfor %}

-- cgit v1.2.3