~boringcactus/crabravebot

7397e9fc78c42f49abe4e5acda42ac26698d3be2 — Melody Horn 3 years ago 1d7b7ed
add word wrapping and improve source links
A assets/templates/classic/info.json => assets/templates/classic/info.json +4 -0
@@ 0,0 1,4 @@
{
  "name": "Classic",
  "source": "https://youtu.be/LDU_Txk06tM"
}

A assets/templates/garfield/info.json => assets/templates/garfield/info.json +4 -0
@@ 0,0 1,4 @@
{
  "name": "Garfield (original by @jondancesto)",
  "source": "https://twitter.com/jondancesto/status/1199796307218427904"
}

A assets/templates/otamatone/info.json => assets/templates/otamatone/info.json +4 -0
@@ 0,0 1,4 @@
{
  "name": "Otamatone (original by TheRealSullyG)",
  "source": "https://youtu.be/VI5I3MuKJlo"
}

M bot.py => bot.py +37 -7
@@ 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')

M templates/index.html => templates/index.html +4 -5
@@ 11,11 11,10 @@
        <input type="hidden" name="ext" value="mp4">
        <p>
            Style:
            <input type="radio" id="classic" name="style" value="classic" checked><label for="classic">Classic</label>
            <input type="radio" id="otamatone" name="style" value="otamatone"><label for="otamatone">Otamatone</label> (<a
                href="https://youtu.be/VI5I3MuKJlo">original</a> by TheRealSullyG)
            <input type="radio" id="garfield" name="style" value="garfield"><label for="garfield">Garfield</label> (<a
                href="https://twitter.com/jondancesto/status/1199796307218427904">original</a> by @jondancesto)
            {% for style in styles %}
            <input type="radio" id="{{ style.id }}" name="style" value="{{ style.id }}" {% if style.id == 'classic' %}checked{% endif %}>
            <label for="{{ style.id }}">{{ style.name }}</label> (<a href="{{ style.source }}">source</a>)
            {% endfor %}
        </p>
        <input type="submit" value="Overlay!">
    </form>