format code
This commit is contained in:
+2
-3
@@ -1,6 +1,5 @@
|
|||||||
def format_quote(text:str)->str:
|
def format_quote(text: str) -> str:
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
if not text.endswith((".","?","!")):
|
if not text.endswith((".", "?", "!")):
|
||||||
text += "."
|
text += "."
|
||||||
return text.upper()
|
return text.upper()
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ def draw_text_in_box(
|
|||||||
fill="white",
|
fill="white",
|
||||||
spacing=10,
|
spacing=10,
|
||||||
):
|
):
|
||||||
|
# another ai slop function
|
||||||
|
|
||||||
# another ai slop function
|
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
x1, y1, x2, y2 = box
|
x1, y1, x2, y2 = box
|
||||||
@@ -42,13 +42,11 @@ def draw_text_in_box(
|
|||||||
|
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
# Try font sizes from large to small
|
|
||||||
for font_size in range(max_font_size, min_font_size - 1, -1):
|
for font_size in range(max_font_size, min_font_size - 1, -1):
|
||||||
font = ImageFont.truetype(font_path, font_size)
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
|
|
||||||
lines = wrap_text(text, font)
|
lines = wrap_text(text, font)
|
||||||
|
|
||||||
# Calculate total text height
|
|
||||||
line_heights = []
|
line_heights = []
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@@ -62,7 +60,6 @@ def draw_text_in_box(
|
|||||||
else:
|
else:
|
||||||
raise ValueError("Text is too long even at minimum font size")
|
raise ValueError("Text is too long even at minimum font size")
|
||||||
|
|
||||||
# Draw each line centered
|
|
||||||
y = y1 + (box_height - text_height) / 2
|
y = y1 + (box_height - text_height) / 2
|
||||||
|
|
||||||
for line, line_height in zip(lines, line_heights):
|
for line, line_height in zip(lines, line_heights):
|
||||||
@@ -81,7 +78,7 @@ def draw_text_in_box(
|
|||||||
y += line_height + spacing
|
y += line_height + spacing
|
||||||
|
|
||||||
|
|
||||||
def generate_quote(text:str):
|
def generate_quote(text: str):
|
||||||
# basicaly just hard-coded values
|
# basicaly just hard-coded values
|
||||||
img = Image.open("assets/template.png").convert("RGB")
|
img = Image.open("assets/template.png").convert("RGB")
|
||||||
draw_text_in_box(
|
draw_text_in_box(
|
||||||
@@ -96,12 +93,11 @@ def generate_quote(text:str):
|
|||||||
)
|
)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("running test")
|
print("running test")
|
||||||
img = Image.open("assets/template.png").convert("RGB")
|
img = Image.open("assets/template.png").convert("RGB")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
draw_text_in_box(
|
draw_text_in_box(
|
||||||
img,
|
img,
|
||||||
"ЬБОПЛВОЛСМЧОЛБМСЧЮ.",
|
"ЬБОПЛВОЛСМЧОЛБМСЧЮ.",
|
||||||
@@ -115,4 +111,4 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
img.save("output.jpg")
|
img.save("output.jpg")
|
||||||
|
|
||||||
print("see output.jpg")
|
print("see output.jpg")
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher, F
|
from aiogram import Bot, Dispatcher, F
|
||||||
from aiogram.filters import Command
|
from aiogram.filters import Command
|
||||||
from aiogram.types import Message, InlineQuery, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent,BufferedInputFile, InputMediaPhoto, ChosenInlineResult
|
from aiogram.types import Message, InlineQuery, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent, BufferedInputFile, InputMediaPhoto, ChosenInlineResult
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from image import generate_quote
|
from image import generate_quote
|
||||||
from formater import format_quote
|
from formater import format_quote
|
||||||
|
|
||||||
dp = Dispatcher()
|
dp = Dispatcher()
|
||||||
bot = None
|
bot = None
|
||||||
|
|
||||||
|
|
||||||
works = {}
|
works = {}
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
global bot
|
global bot
|
||||||
cfg = configparser.ConfigParser()
|
cfg = configparser.ConfigParser()
|
||||||
@@ -25,18 +26,20 @@ async def main():
|
|||||||
token = cfg['bot']['BOT_TOKEN']
|
token = cfg['bot']['BOT_TOKEN']
|
||||||
if len(token) == 0:
|
if len(token) == 0:
|
||||||
raise "token seems empty"
|
raise "token seems empty"
|
||||||
bot = Bot(token=token)
|
bot = Bot(token=token)
|
||||||
|
|
||||||
print("Starting bot...")
|
print("Starting bot...")
|
||||||
try:
|
try:
|
||||||
await dp.start_polling(bot)
|
await dp.start_polling(bot)
|
||||||
finally:
|
finally:
|
||||||
print("Bot stopped")
|
print("Bot stopped")
|
||||||
|
|
||||||
|
|
||||||
@dp.message(F.text, Command("start"))
|
@dp.message(F.text, Command("start"))
|
||||||
async def start(message: Message):
|
async def start(message: Message):
|
||||||
await message.answer(f"usage:\n\n@{(await bot.get_me()).username} some cool quote")
|
await message.answer(f"usage:\n\n@{(await bot.get_me()).username} some cool quote")
|
||||||
|
|
||||||
|
|
||||||
@dp.inline_query()
|
@dp.inline_query()
|
||||||
async def inline_query(query: InlineQuery):
|
async def inline_query(query: InlineQuery):
|
||||||
text = query.query.strip()
|
text = query.query.strip()
|
||||||
@@ -109,4 +112,4 @@ async def chosen_inline_result(result: ChosenInlineResult):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user