Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit ea47d6c

Browse files
committed
Added video support #12
Only links to save space/avoid limits
1 parent bd986ef commit ea47d6c

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

forwarding_bot/vk/_blueprint/attachment_handler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from forwarding_bot.config import data_config
77
from forwarding_bot.settings import PARSE_MODE
8+
from .message_helper import MessageHelper
89

910
logger = logging.getLogger(__name__)
1011

@@ -26,6 +27,11 @@ async def handle_attachment(
2627
caption=message_text,
2728
photo=source.url,
2829
parse_mode=PARSE_MODE)
30+
elif msg_attachment.type == "video":
31+
response = await bot.send_message(chat_id=data_config.destination_id,
32+
text=message_text +
33+
MessageHelper.get_video_str(msg_attachment.video),
34+
parse_mode=PARSE_MODE)
2935
elif msg_attachment.type == "audio_message":
3036
response = await bot.send_voice(chat_id=data_config.destination_id,
3137
caption=message_text,

forwarding_bot/vk/_blueprint/blueprint.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import logging
22
from asyncio import sleep
3+
from typing import NoReturn
34

45
from aiogram import Bot
56
from vkbottle.user import Blueprint, Message
6-
from typing import NoReturn
7+
78
from forwarding_bot.config import data_config
89
from forwarding_bot.settings import PARSE_MODE
910
from . import attachment_handler, nested_handler
@@ -16,7 +17,16 @@
1617

1718
@bot_bp.on.chat_message()
1819
async def handler(message: Message) -> NoReturn:
19-
"""Default handler that handles every message"""
20+
"""
21+
Default handler that handles every message
22+
---
23+
Attachment types:
24+
1. Audio Message/Photo
25+
2. Video (sent as a link because of TG API limits and also to save TG space)
26+
3. Sticker (cannot be parsed, so instead just becomes "*sticker*")
27+
Handling:
28+
- If there are no *valid* attachments, then only text will be sent
29+
"""
2030
logger.info("New message")
2131
logger.debug(str(message))
2232
# VK attachment limit workaround
@@ -51,9 +61,11 @@ async def handler(message: Message) -> NoReturn:
5161
await nested_handler.handle_nested(bot=bot, message=message, tree_getter=nested_handler.get_fwd_tree)
5262
elif not message.attachments:
5363
logger.info("No valid attachments or forwarded messages, sending text")
64+
5465
await bot.send_message(chat_id=data_config.destination_id,
5566
text=formatted_message,
5667
parse_mode=PARSE_MODE)
5768

5869
await bot.close()
5970
await sleep(0.1)
71+
return

forwarding_bot/vk/_blueprint/message_helper.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from asyncio import sleep
22
from datetime import datetime
3-
from typing import List, Optional
3+
from typing import List, Optional, Union
44

55
from vkbottle.api import API
6-
from vkbottle.types.objects.messages import MessageAttachment
6+
from vkbottle.types.objects.messages import MessageAttachment, ForeignMessage
77
from vkbottle.types.objects.users import UserXtrCounters
8+
from vkbottle.types.objects.video import Video
89
from vkbottle.user import Message
910

1011
from forwarding_bot.settings import MINSK_TZ
@@ -13,7 +14,7 @@
1314
class MessageHelper:
1415

1516
@classmethod
16-
def get_date(cls, message: Message) -> str:
17+
def get_date(cls, message: Union[Message, ForeignMessage]) -> str:
1718
dt = datetime.fromtimestamp(message.date)
1819
now = datetime.now(tz=MINSK_TZ)
1920
if now.date() != dt.date():
@@ -25,7 +26,7 @@ def get_name(user: UserXtrCounters) -> str:
2526
return f"{user.last_name} {user.first_name}"
2627

2728
@classmethod
28-
def get_header(cls, user: UserXtrCounters, message: Message) -> str:
29+
def get_header(cls, user: UserXtrCounters, message: Union[Message, ForeignMessage]) -> str:
2930
date = cls.get_date(message)
3031
if date:
3132
return f"<em>{cls.get_name(user)}, {date}</em>"
@@ -36,11 +37,11 @@ def get_text(text: str) -> str:
3637
return f"<strong>{text[:1000]}</strong>"
3738

3839
@staticmethod
39-
def get_valid_attachments(message: Message) -> List[MessageAttachment]:
40+
def get_valid_attachments(message: Union[Message, ForeignMessage]) -> List[MessageAttachment]:
4041
return [
4142
attach
4243
for attach in message.attachments
43-
if attach.type in ("photo", "doc", "sticker", "audio_message")
44+
if attach.type in ("photo", "video", "doc", "sticker", "audio_message")
4445
]
4546

4647
@staticmethod
@@ -52,8 +53,14 @@ def filter_media(attachments: List[MessageAttachment]) -> List[MessageAttachment
5253
]
5354

5455
@staticmethod
55-
async def get_sender(token: Optional[str], message: Message) -> UserXtrCounters:
56+
async def get_sender(token: Optional[str], message: Union[Message, ForeignMessage]) -> UserXtrCounters:
5657
api = API(token)
5758
user_list = await api.users.get(user_ids=str(message.from_id))
5859
await sleep(0.1)
5960
return user_list[0]
61+
62+
@staticmethod
63+
def get_video_str(video: Video) -> str:
64+
url = f"vk.com/video{video.owner_id}_{video.id}" \
65+
f"?access_key={video.access_key or ''}"
66+
return f"(Видео {url})"

forwarding_bot/vk/_blueprint/nested_handler.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ def parse_text(message_: ParsedMessage, attachment_storage: List[Tuple[str, Mess
112112
}
113113

114114
for attach_ in message_.attachments:
115-
name = "{name}_{id}".format(name=attach_names[str(attach_.type)],
116-
id=len(attachment_storage))
117-
attachment_storage.append((name, attach_))
118-
text += '{indent}<u>{{{name}}}</u>\n'.format(indent=" " * message_.indent,
119-
name=name)
115+
if attach_.type == "video":
116+
text += '{indent}<u>{link}</u>\n'.format(indent=" " * message_.indent,
117+
link=MessageHelper.get_video_str(attach_.video))
118+
else:
119+
name = "{name}_{id}".format(name=attach_names[str(attach_.type)],
120+
id=len(attachment_storage))
121+
attachment_storage.append((name, attach_))
122+
text += '{indent}<u>{{{name}}}</u>\n'.format(indent=" " * message_.indent,
123+
name=name)
120124

121125
return text
122126

0 commit comments

Comments
 (0)