Skip to content

Commit

Permalink
add method to save users avatars and try to paste it in image(get unl…
Browse files Browse the repository at this point in the history
…ucky)
  • Loading branch information
kawarag1 committed Dec 19, 2024
1 parent f4b520e commit f81608b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 6 deletions.
Binary file removed app/cfg/__pycache__/settings.cpython-312.pyc
Binary file not shown.
Binary file removed app/quote_module/__pycache__/quote.cpython-312.pyc
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion app/quote_module/models/quote_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@


class QuoteModel:
def __init__(self, quote: str, author: str):
def __init__(self, quote: str, author: str, avatar:str):
self.quote = quote
self.author = author
self.avatar = avatar

@property
def wrapped_quote(self, chunk_size=50):
Expand Down
8 changes: 4 additions & 4 deletions app/quote_module/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_avatar_circle(size, border_color='black', border_width=2):
def paste_avatar(image, avatar, position):
image.paste(avatar, position, avatar)

def generate_image(quote_text: str, author_name: str) -> Result[None]:
def generate_image(quote_text: str, author_name: str, avatar:str) -> Result[None]:
title_font = ImageFont.truetype("app/quote_module/media/Roboto-Medium.ttf", 24)
quote_font = ImageFont.truetype("app/quote_module/media/Roboto-Medium.ttf", 20)
author_font = ImageFont.truetype("app/quote_module/media/Roboto-Medium.ttf", 16)
Expand Down Expand Up @@ -72,17 +72,17 @@ def generate_image(quote_text: str, author_name: str) -> Result[None]:

author_position = (padding + avatar_size + padding, height - author_height - padding)
add_text(image, author_text, author_font, author_position)

avatar = create_avatar_circle(avatar_size)
avatar_position = (50, height - avatar_size - 50)
# paste_avatar(image, f"{author_name}_avatar.png", avatar_position)
paste_avatar(image, f"{author_name}_avatar.png", avatar_position)


image.save(f'temp_quotes/{author_name.replace("@", "")}.png')
image.close()
def generate_quote(quote: QuoteModel) -> Result[None]:
try:
generate_image(quote.wrapped_quote, quote.author)
generate_image(quote.wrapped_quote, quote.author, quote.avatar)
return success(None)
except Exception as e:
print(e)
Expand Down
Binary file not shown.
Binary file not shown.
19 changes: 18 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import os
import telebot
import requests
from telebot.types import Message
from telebot.types import UserProfilePhotos

Expand All @@ -20,8 +21,24 @@
def handle_all_messages(message: telebot.types.Message):

if message.forward_from or message.forward_from_chat:
user_id = message.forward_from.id
try:
photos = bot.get_user_profile_photos(user_id)
if photos.total_count > 0:
file_id = photos.photos[0][-1].file_id
file_info = bot.get_file(file_id)
file_path = file_info.file_path
file_url = f"https://api.telegram.org/file/bot{settings.TELEGRAM_TOKEN}/{file_path}"
response = requests.get(file_url)
if response.status_code == 200:
with open(f"{message.forward_from.full_name}_avatar.png", "wb") as file:
file.write(response.content)
else:
bot.send_message(message.chat.id, "Не удалось скачать аватар")
except Exception as e:
bot.reply_to(message, f"Ошибка!")
# avatar = get_user_avatar(message.forward_from.username, message.forward_from.id, bot)
image = generate_quote(QuoteModel(message.any_text, message.forward_from.username))
image = generate_quote(QuoteModel(message.any_text, message.forward_from.username, f"{message.forward_from.full_name}"))
if image.success:
with open(f"temp_quotes/{message.forward_from.username}.png", "rb") as photo:
bot.send_photo(message.chat.id, photo)
Expand Down

0 comments on commit f81608b

Please sign in to comment.