Обновление
This commit is contained in:
parent
59c685ba49
commit
bd989270bc
@ -3,7 +3,7 @@ from aiogram.types import Message, MessageEntity, PhotoSize, ReactionTypeEmoji
|
|||||||
from app.utils.send_to_notion import send_to_notion
|
from app.utils.send_to_notion import send_to_notion
|
||||||
|
|
||||||
|
|
||||||
async def parse_message(message: Message):
|
async def parse_message(message: Message, album=None):
|
||||||
await send_to_notion(message)
|
await send_to_notion(message, album)
|
||||||
|
|
||||||
await message.react([ReactionTypeEmoji(emoji="👌")])
|
await message.react([ReactionTypeEmoji(emoji="👌")])
|
||||||
|
@ -9,27 +9,25 @@ from aiogram.types import Message, MessageEntity, PhotoSize
|
|||||||
from app.settings import notion, bot, secrets
|
from app.settings import notion, bot, secrets
|
||||||
|
|
||||||
|
|
||||||
async def send_to_notion(message: Message):
|
async def send_to_notion(message: Message, album):
|
||||||
pattern = r"(https?://[^\s]+|t\.me/[^\s]+)"
|
|
||||||
image_url = None
|
|
||||||
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(
|
imgur_client = Imgur(
|
||||||
{"client_id": secrets.imgur_client_id.get_secret_value()}
|
{"client_id": secrets.imgur_client_id.get_secret_value()}
|
||||||
)
|
)
|
||||||
image = imgur_client.image_upload(
|
|
||||||
path.realpath(file_name), "Untitled", "My first image upload"
|
pattern = r"(https?://[^\s]+|t\.me/[^\s]+)"
|
||||||
)
|
image_url = None
|
||||||
image_url = image["response"]["data"]["link"]
|
photos = []
|
||||||
os.remove(file_name)
|
|
||||||
|
if message.caption:
|
||||||
|
links2 = re.findall(pattern, message.caption)
|
||||||
|
text: str = message.caption
|
||||||
|
links: MessageEntity = [
|
||||||
|
link.url for link in message.caption_entities if link.type == "text_link"
|
||||||
|
]
|
||||||
|
if album:
|
||||||
|
photos: PhotoSize = [photo.photo[0].file_id for photo in album]
|
||||||
|
elif message.photo:
|
||||||
|
photos: PhotoSize = [message.photo[0].file_id]
|
||||||
else:
|
else:
|
||||||
text: str = message.text
|
text: str = message.text
|
||||||
links2 = re.findall(pattern, message.text)
|
links2 = re.findall(pattern, message.text)
|
||||||
@ -43,7 +41,7 @@ async def send_to_notion(message: Message):
|
|||||||
links.extend(links2)
|
links.extend(links2)
|
||||||
links = set(links)
|
links = set(links)
|
||||||
properties = {
|
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}}]},
|
"Text": {"rich_text": [{"text": {"content": text}}]},
|
||||||
"Added at": {
|
"Added at": {
|
||||||
"date": {
|
"date": {
|
||||||
@ -58,7 +56,22 @@ async def send_to_notion(message: Message):
|
|||||||
properties[f"Link{i}"] = {"url": link}
|
properties[f"Link{i}"] = {"url": link}
|
||||||
cover = None
|
cover = None
|
||||||
if photos:
|
if photos:
|
||||||
properties["Image"] = {
|
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": [
|
"files": [
|
||||||
{
|
{
|
||||||
"name": "image.jpg",
|
"name": "image.jpg",
|
||||||
@ -67,7 +80,7 @@ async def send_to_notion(message: Message):
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
cover = {"type": "external", "external": {"url": image_url}}
|
|
||||||
icon = {"type": "emoji", "emoji": "🎉"}
|
icon = {"type": "emoji", "emoji": "🎉"}
|
||||||
parent = {
|
parent = {
|
||||||
"type": "database_id",
|
"type": "database_id",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user