From bd989270bc4efbf3dbed81ce207b1dad1a1de801 Mon Sep 17 00:00:00 2001 From: Ivan Ashikhmin Date: Sun, 28 Apr 2024 20:05:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/handlers/message.py | 4 +-- app/utils/send_to_notion.py | 61 ++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/app/handlers/message.py b/app/handlers/message.py index d518684..d587330 100644 --- a/app/handlers/message.py +++ b/app/handlers/message.py @@ -3,7 +3,7 @@ from aiogram.types import Message, MessageEntity, PhotoSize, ReactionTypeEmoji from app.utils.send_to_notion import send_to_notion -async def parse_message(message: Message): - await send_to_notion(message) +async def parse_message(message: Message, album=None): + await send_to_notion(message, album) await message.react([ReactionTypeEmoji(emoji="👌")]) diff --git a/app/utils/send_to_notion.py b/app/utils/send_to_notion.py index 0536739..801f2c3 100644 --- a/app/utils/send_to_notion.py +++ b/app/utils/send_to_notion.py @@ -9,27 +9,25 @@ from aiogram.types import Message, MessageEntity, PhotoSize from app.settings import notion, bot, secrets -async def send_to_notion(message: Message): +async def send_to_notion(message: Message, album): + imgur_client = Imgur( + {"client_id": secrets.imgur_client_id.get_secret_value()} + ) + pattern = r"(https?://[^\s]+|t\.me/[^\s]+)" image_url = None + photos = [] + if message.caption: links2 = re.findall(pattern, message.caption) text: str = message.caption - photos: PhotoSize = [photo.file_id for photo in message.photo] links: MessageEntity = [ link.url for link in message.caption_entities if link.type == "text_link" ] - if photos: - file_name = f"images/{photos[0]}.jpg" - await bot.download(message.photo[-1], destination=file_name) - imgur_client = Imgur( - {"client_id": secrets.imgur_client_id.get_secret_value()} - ) - image = imgur_client.image_upload( - path.realpath(file_name), "Untitled", "My first image upload" - ) - image_url = image["response"]["data"]["link"] - os.remove(file_name) + if album: + photos: PhotoSize = [photo.photo[0].file_id for photo in album] + elif message.photo: + photos: PhotoSize = [message.photo[0].file_id] else: text: str = message.text links2 = re.findall(pattern, message.text) @@ -43,7 +41,7 @@ async def send_to_notion(message: Message): links.extend(links2) links = set(links) properties = { - "Name": {"title": [{"text": {"content": text[: text.index("\n")]}}]}, + "Name": {"title": [{"text": {"content": text[: text.find("\n") or -1]}}]}, "Text": {"rich_text": [{"text": {"content": text}}]}, "Added at": { "date": { @@ -58,16 +56,31 @@ async def send_to_notion(message: Message): properties[f"Link{i}"] = {"url": link} cover = None if photos: - properties["Image"] = { - "files": [ - { - "name": "image.jpg", - "type": "external", - "external": {"url": image_url}, - } - ] - } - cover = {"type": "external", "external": {"url": image_url}} + for i, photo in enumerate(photos, start=1): + if i > 9: + break + + file_name = f"images/{photo}.jpg" + await bot.download(photo, destination=file_name) + image = imgur_client.image_upload( + path.realpath(file_name), "Untitled", "My first image upload" + ) + image_url = image["response"]["data"]["link"] + + os.remove(file_name) + if i == 1: + cover = {"type": "external", "external": {"url": image_url}} + + properties[f"Image{i}"] = { + "files": [ + { + "name": "image.jpg", + "type": "external", + "external": {"url": image_url}, + } + ] + } + icon = {"type": "emoji", "emoji": "🎉"} parent = { "type": "database_id",