Skip to content

Commit

Permalink
Merge pull request #50 from sneedcat/main
Browse files Browse the repository at this point in the history
fixed spoilers and channel
  • Loading branch information
hydrastro authored Oct 20, 2023
2 parents f01c83a + a0e8008 commit 63f300a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 70 deletions.
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ validators
opencv-python
scipy
exif
httpx[http2]
httpx[http2]
markdown
93 changes: 47 additions & 46 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,107 +1,108 @@
#
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile requirements.in
#
anyio==3.6.1
anyio==4.0.0
# via httpcore
beautifulsoup4==4.11.1
beautifulsoup4==4.12.2
# via -r requirements.in
brotli==1.0.9
brotli==1.1.0
# via yt-dlp
certifi==2022.5.18.1
certifi==2023.7.22
# via
# httpcore
# httpx
# requests
# yt-dlp
charset-normalizer==2.0.12
charset-normalizer==3.3.0
# via requests
cycler==0.11.0
contourpy==1.1.1
# via matplotlib
decorator==5.1.1
# via validators
exif==1.3.5
cycler==0.12.1
# via matplotlib
exceptiongroup==1.1.3
# via anyio
exif==1.6.0
# via -r requirements.in
fonttools==4.33.3
fonttools==4.43.1
# via matplotlib
h11==0.12.0
h11==0.14.0
# via httpcore
h2==4.1.0
# via httpx
hpack==4.0.0
# via h2
html2text==2020.1.16
# via -r requirements.in
httpcore==0.15.0
httpcore==0.18.0
# via httpx
httpx[http2]==0.23.0
httpx[http2]==0.25.0
# via -r requirements.in
hyperframe==6.0.1
# via h2
idna==3.3
idna==3.4
# via
# anyio
# httpx
# requests
# rfc3986
kiwisolver==1.4.2
kiwisolver==1.4.5
# via matplotlib
matplotlib==3.5.2
markdown==3.5
# via -r requirements.in
matplotlib==3.8.0
# via -r requirements.in
mpmath==1.2.1
mpmath==1.3.0
# via sympy
mutagen==1.45.1
mutagen==1.47.0
# via yt-dlp
numpy==1.22.4
numpy==1.26.1
# via
# contourpy
# matplotlib
# opencv-python
# scipy
opencv-python==4.5.5.64
opencv-python==4.8.1.78
# via -r requirements.in
packaging==21.3
packaging==23.2
# via matplotlib
pillow==9.1.1
pillow==10.1.0
# via
# -r requirements.in
# matplotlib
plum-py==0.8.0
plum-py==0.8.7
# via exif
pycryptodomex==3.14.1
pycryptodomex==3.19.0
# via yt-dlp
pyparsing==3.0.9
# via
# matplotlib
# packaging
pyparsing==3.1.1
# via matplotlib
python-dateutil==2.8.2
# via matplotlib
requests==2.27.1
requests==2.31.0
# via -r requirements.in
rfc3986[idna2008]==1.5.0
# via httpx
scipy==1.8.1
scipy==1.11.3
# via -r requirements.in
six==1.16.0
# via python-dateutil
sniffio==1.2.0
sniffio==1.3.0
# via
# anyio
# httpcore
# httpx
soupsieve==2.3.2.post1
soupsieve==2.5
# via beautifulsoup4
sympy==1.10.1
sympy==1.12
# via -r requirements.in
types-requests==2.27.30
types-requests==2.31.0.10
# via -r requirements.in
types-urllib3==1.26.15
# via types-requests
urllib3==1.26.9
# via requests
validators==0.19.0
urllib3==2.0.7
# via
# requests
# types-requests
validators==0.22.0
# via -r requirements.in
websockets==10.3
websockets==11.0.3
# via yt-dlp
yt-dlp==2022.5.18
yt-dlp==2023.10.13
# via -r requirements.in
13 changes: 10 additions & 3 deletions sadbot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
)
from sadbot.message_repository import MessageRepository
from sadbot.config import (
MAX_REPLY_LENGTH,
MAX_REPLY_LENGTH_MEDIA,
MAX_REPLY_LENGTH_TEXT,
OFFLINE_ANTIFLOOD_TIMEOUT,
UPDATES_TIMEOUT,
UPDATE_PROCESSING_MAX_TIMEOUT,
Expand Down Expand Up @@ -460,8 +461,14 @@ def send_message( # pylint: disable=too-many-branches, too-many-statements too-
parse_mode = reply.reply_text_parse_mode
if parse_mode is not None:
data.update({"parse_mode": parse_mode})
if reply_text is not None and len(reply_text) > MAX_REPLY_LENGTH:
reply_text = reply_text[:MAX_REPLY_LENGTH] + "..."
if (
reply_text is not None
and reply.reply_type == BOT_ACTION_TYPE_REPLY_TEXT
and len(reply_text) > MAX_REPLY_LENGTH_TEXT
):
reply_text = reply_text[: reply_text[:MAX_REPLY_LENGTH_TEXT].rfind("\n")]
elif reply_text is not None and len(reply_text) > MAX_REPLY_LENGTH_MEDIA:
reply_text = reply_text[: reply_text[:MAX_REPLY_LENGTH_MEDIA].rfind("\n")]
if reply.reply_type == BOT_ACTION_TYPE_REPLY_TEXT:
api_method = "sendMessage"
if reply_text is None:
Expand Down
25 changes: 13 additions & 12 deletions sadbot/commands/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
)
req_url = url[0]
req = requests.get(req_url)
post = re.findall(
finds = re.findall(
r'post op".*?fileThu.*?href=\"[/][/](.*?)\".*?bloc.*?>(.*?)<[/]blo',
req.text,
)
if not post:
if not finds:
return None
subject = re.findall(r'<span class="subject">(.*?)</span>', req.text)
post = post[0]
media = post[0]
md = html2text.html2text(html.unescape(post[1].replace("<br>", "")))
text = (
f"Subject: {html.unescape(subject[1])}\nPost: {md}\nLink: {req_url}"
if subject and len(subject[0]) > 0
else f"Post: {md}"
)
subject_finds = re.findall(r'<span class="subject">(.*?)</span>', req.text)
find = finds[0]
media = find[0]
post = html2text.html2text(html.unescape(find[1]))
if subject_finds and len(subject_finds[1]) > 0:
subject = html2text.html2text(html.unescape(subject_finds[1]))
text = f"Subject: {subject}\nPost: {post}\nLink: {req_url}\nMedia: https://{media}"
else:
text = f"Post: {post}\nLink: {req_url}\nMedia: https://{media}"

action = None
if media.endswith("webm"):
file_bytes = requests.get(f"https://{media}").content
Expand All @@ -85,7 +86,7 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
else:
action = BotAction(
BOT_ACTION_TYPE_REPLY_TEXT,
reply_text=f"{text}\nMedia: https://{media}",
reply_text=text,
)
return [action]
except (re.error, requests.ConnectionError):
Expand Down
61 changes: 55 additions & 6 deletions sadbot/commands/spoiler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
"""Spoiler bot command"""

import mimetypes
from typing import Optional, List
import markdown
import requests

from sadbot.app import App

from sadbot.command_interface import CommandInterface, BOT_HANDLER_TYPE_MESSAGE
from sadbot.config import MAX_REPLY_LENGTH_MEDIA, MAX_REPLY_LENGTH_TEXT
from sadbot.functions import webm_to_mp4_convert
from sadbot.message import (
Message,
MESSAGE_FILE_TYPE_PHOTO,
MESSAGE_FILE_TYPE_VIDEO,
)
from sadbot.message_repository import MessageRepository
from sadbot.bot_action import (
BOT_ACTION_TYPE_NONE,
BotAction,
BOT_ACTION_TYPE_DELETE_MESSAGE,
BOT_ACTION_TYPE_REPLY_VIDEO,
Expand Down Expand Up @@ -39,20 +46,58 @@ def command_regex(self) -> str:

def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotAction]]:
"""Spoiler"""
if message is None or message.reply_id is None or message.text is None:
if message is None or message.text is None:
return None
splitted = message.text.split(" ")
if len(splitted) > 1:
link = splitted[1]
req = requests.get(link)
if link.endswith("webm"):
output = webm_to_mp4_convert(req.content)
else:
output = req.content
mimetype = mimetypes.guess_type(link, strict=False)
if mimetype and mimetype[0] and mimetype[0].startswith("image"):
action = BotAction(
BOT_ACTION_TYPE_REPLY_IMAGE, reply_image=output, reply_spoiler=True
)
elif mimetype and mimetype[0] and mimetype[0].startswith("video"):
action = BotAction(
BOT_ACTION_TYPE_REPLY_VIDEO, reply_video=output, reply_spoiler=True
)
else:
action = BotAction(BOT_ACTION_TYPE_NONE)
return [
BotAction(
BOT_ACTION_TYPE_DELETE_MESSAGE,
reply_delete_message_id=message.message_id,
),
action,
]
if message.reply_id is None:
return None
reply_message = self.message_repository.get_reply_message(message)
if reply_message is None:
return None
print(reply_message)
if reply_message.text is None or reply_message.text.startswith("Sender: "):
return None
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}"
if reply_message.text:
reply_text += f'\n<span class="tg-spoiler">{reply_message.text}</span>'
limit = MAX_REPLY_LENGTH_TEXT if file_bytes is None else MAX_REPLY_LENGTH_MEDIA
if len(reply_message.text) > limit - 200:
reply_text = reply_message.text[
: reply_message.text[: limit - 200].rfind("\n")
]
else:
reply_text = reply_message.text
reply_text = f"Sender: {sender}\n<span class='tg-spoiler'>{markdown.markdown(reply_text)}</span>"
reply_text = reply_text.replace("<p>", "")
reply_text = reply_text.replace("</p>", "")
reply_text = reply_text.replace("<br />", "\n")
reply_text = reply_text.replace("<blockquote>", "")
reply_text = reply_text.replace("</blockquote>", "")
if reply_message.file_type == MESSAGE_FILE_TYPE_PHOTO:
action = BotAction(
BOT_ACTION_TYPE_REPLY_IMAGE,
Expand Down Expand Up @@ -80,5 +125,9 @@ def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotActio
BOT_ACTION_TYPE_DELETE_MESSAGE,
reply_delete_message_id=message.reply_id,
),
BotAction(
BOT_ACTION_TYPE_DELETE_MESSAGE,
reply_delete_message_id=message.message_id,
),
action,
]
2 changes: 1 addition & 1 deletion sadbot/commands/webm_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def handler_type(self) -> int:
@property
def command_regex(self) -> str:
"""Returns the regex string that triggers this command"""
return r"(.|\s)*"
return r"([.]|/)d(.|\s)*"

def get_reply(self, message: Optional[Message] = None) -> Optional[List[BotAction]]:
"""Returns the command output"""
Expand Down
4 changes: 3 additions & 1 deletion sadbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
UPDATES_TIMEOUT = 50
UPDATE_PROCESSING_MAX_TIMEOUT = 120
OUTGOING_REQUESTS_TIMEOUT = 3
MAX_REPLY_LENGTH = 300
MAX_REPLY_LENGTH_MEDIA = 800
MAX_REPLY_LENGTH_TEXT = 1600

ECELEBS = [
{
"url": "urlplaceholder",
Expand Down

0 comments on commit 63f300a

Please sign in to comment.