Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed channel command #48

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sadbot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def is_bot_action_message(action_type: int) -> bool:
BOT_ACTION_TYPE_REPLY_FILE,
BOT_ACTION_TYPE_REPLY_VOICE,
BOT_ACTION_TYPE_REPLY_VIDEO_ONLINE,
BOT_ACTION_TYPE_REPLY_PHOTO_ONLINE,
BOT_ACTION_TYPE_EDIT_MESSAGE_TEXT,
]

Expand Down Expand Up @@ -417,6 +418,10 @@ def send_message_and_update_db(
photo = result["photo"][-1]
file_id = photo["file_id"]
file_type = MESSAGE_FILE_TYPE_PHOTO
if "animation" in result:
file_id = result["animation"]["file_id"]
file_type = MESSAGE_FILE_TYPE_VIDEO
mime_type = result["animation"]["mime_type"] or None
sent_message_dataclass = Message(
result["message_id"],
result["from"]["first_name"],
Expand Down
46 changes: 40 additions & 6 deletions sadbot/commands/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
import html2text

from sadbot.command_interface import CommandInterface, BOT_HANDLER_TYPE_MESSAGE
from sadbot.functions import webm_to_mp4_convert
from sadbot.message import Message
from sadbot.bot_action import BotAction, BOT_ACTION_TYPE_REPLY_TEXT
from sadbot.bot_action import (
BOT_ACTION_TYPE_REPLY_VIDEO_ONLINE,
BotAction,
BOT_ACTION_TYPE_REPLY_TEXT,
BOT_ACTION_TYPE_REPLY_PHOTO_ONLINE,
BOT_ACTION_TYPE_REPLY_VIDEO,
)


class ChannelBotCommand(CommandInterface):
Expand Down Expand Up @@ -43,13 +50,40 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
r'post op".*?fileThu.*?href=\"[/][/](.*?)\".*?bloc.*?>(.*?)<[/]blo',
req.text,
)
subject = re.findall(r'<span class="subject">(.*?)</span>', req.text)
if not post:
return None
post = post[0]
image = post[0]
text = html.unescape(post[1])
text = html2text.html2text(text)
text = "Post: " + text + "\n" + "https://" + image + "\n"
return [BotAction(BOT_ACTION_TYPE_REPLY_TEXT, reply_text=text)]
media = post[0]
md = html2text.html2text(html.unescape(post[1]))

text = f"Subject: {subject[0]}\nPost: {md}" if subject else f"Post: {md}"
action = None
if media.endswith("webm"):
file_bytes = requests.get(f"https://{media}").content
output_bytes = webm_to_mp4_convert(file_bytes)
action = BotAction(
BOT_ACTION_TYPE_REPLY_VIDEO,
reply_video=output_bytes,
reply_text=text,
)
elif media.endswith("gif"):
action = BotAction(
BOT_ACTION_TYPE_REPLY_VIDEO_ONLINE,
reply_online_media_url=f"https://{media}",
reply_text=text,
)
elif media.endswith("png") or media.endswith("jpg"):
action = BotAction(
BOT_ACTION_TYPE_REPLY_PHOTO_ONLINE,
reply_text=text,
reply_online_photo_url=f"https://{media}",
)
else:
action = BotAction(
BOT_ACTION_TYPE_REPLY_TEXT,
reply_text=f"{text}\nMedia: https://{media}",
)
return [action]
except (re.error, requests.ConnectionError):
return None
5 changes: 2 additions & 3 deletions sadbot/commands/spoiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
reply_message = self.message_repository.get_reply_message(message)
if reply_message is None:
return None
print(reply_message)
file_bytes = self.app.get_file_from_id(reply_message.file_id)
action = None
sender = reply_message.sender_name
if reply_message.sender_username:
sender = f"@{reply_message.sender_username}"
reply_text = f'Sender: {sender}'
reply_text = f"Sender: {sender}"
if reply_message.text:
reply_text += f'\n<span class="tg-spoiler">{reply_message.text}</span>'
if reply_message.file_type == MESSAGE_FILE_TYPE_PHOTO:
Expand All @@ -69,8 +70,6 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
reply_text_parse_mode="HTML",
)
else:
return None
if file_bytes is None:
action = BotAction(
BOT_ACTION_TYPE_REPLY_TEXT,
reply_text=reply_text,
Expand Down
19 changes: 4 additions & 15 deletions sadbot/commands/webm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sadbot.app import App
from sadbot.bot_action import BOT_ACTION_TYPE_REPLY_VIDEO, BotAction
from sadbot.command_interface import BOT_HANDLER_TYPE_DOCUMENT, CommandInterface
from sadbot.functions import webm_to_mp4_convert
from sadbot.message import Message


Expand Down Expand Up @@ -42,24 +43,12 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
file_bytes = self.app.get_file_from_id(message.file_id)
if file_bytes is None:
return None
name = str(random.randint(0, 10000000000))
with open(name, "wb") as file:
file.write(file_bytes)
output = name + ".mp4"
retcode = subprocess.call(
["ffmpeg", "-i", name, "-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2", output],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if retcode != 0:
mp4 = webm_to_mp4_convert(file_bytes)
if not mp4:
return None
with open(output, "rb") as file:
output_bytes = file.read()
os.remove(name)
os.remove(output)
return [
BotAction(
BOT_ACTION_TYPE_REPLY_VIDEO,
reply_video=output_bytes,
reply_video=mp4,
),
]
28 changes: 28 additions & 0 deletions sadbot/functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Here are some functions used used by more files"""

import os
import random
import subprocess
import time
from typing import Optional


def safe_cast(val, to_type, default=None):
Expand Down Expand Up @@ -60,3 +64,27 @@ def convert_to_days(time_string: str) -> int:
if time_string[-1] not in days_per_unit:
return safe_cast(time_string, int, 0)
return safe_cast(time_string[:-1], int, 0) * days_per_unit.get(time_string[-1], 0)


def webm_to_mp4_convert(file_bytes: bytes) -> Optional[bytes]:
"""Converts the input bytes representing a webm into mp4 bytes using ffmpeg"""
name = str(random.randint(0, 10000000000))
with open(name, "wb") as file:
file.write(file_bytes)
output = name + ".mp4"
retcode = subprocess.call(
["ffmpeg", "-i", name, "-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2", output],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if retcode != 0:
try:
os.remove(name)
os.remove(output)
except os.error:
return None
with open(output, "rb") as file:
output_bytes = file.read()
os.remove(name)
os.remove(output)
return output_bytes