Updated to mab v0.3.0
This commit is contained in:
57
logic.py
57
logic.py
@@ -8,7 +8,7 @@ import shutil
|
||||
import traceback
|
||||
|
||||
from mab import MatrixBot
|
||||
from mab.types import VideoFileProperties
|
||||
from mab.types import RoomEventData
|
||||
from mab.filters.text import TextFilter, TextStartsWithFilter, TextEndsWithFilter
|
||||
|
||||
from nio import MatrixRoom, RoomMessageText
|
||||
@@ -30,47 +30,49 @@ async def worker_download(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
f"<strong>Скачивание видео по "
|
||||
f"<a href=\"{html.escape(text.body)}\">вашей ссылке</a></strong>"
|
||||
)
|
||||
await _bot.send_text_to_room(room.room_id, response, True)
|
||||
await _bot.send_text(room, response)
|
||||
# get formats
|
||||
d = Downloader(text.body)
|
||||
try:
|
||||
result = await d.download_video("temp")
|
||||
except:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
await _bot.send_text(room, traceback.format_exc())
|
||||
return
|
||||
await _bot.send_text_to_room(room.room_id, "<strong>Скачано, загрузка в Matrix...</strong>", True)
|
||||
await _bot.send_text(room, "<strong>Скачано, загрузка в Matrix...</strong>")
|
||||
# upload
|
||||
try:
|
||||
await _bot.send_video_to_room(room.room_id, result, caption=f"Скачано из {text.body}")
|
||||
await _bot.send_video(room, result, text=f"Скачано из {text.body}")
|
||||
except:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
await _bot.send_text(room.room_id, traceback.format_exc())
|
||||
finally:
|
||||
os.unlink(result)
|
||||
|
||||
#
|
||||
# CALLBACKS
|
||||
#
|
||||
async def command_help(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
async def command_help(e: RoomEventData) -> None:
|
||||
"""!help"""
|
||||
if text.sender == _bot.get_client().user_id:
|
||||
if e.event.sender == _bot.get_client().user_id:
|
||||
return
|
||||
resp = "<strong>2026-matrix-down</strong><br>"
|
||||
resp += "Этот бот используется для скачивания видео/аудио с хостингов, "
|
||||
resp += "поддерживаемых <code>yt-dlp</code>. Просто отправьте ссылку в эту "
|
||||
resp += "комнату, чтобы бот предложил варианты загрузки."
|
||||
await _bot.send_text_to_room(room.room_id, resp, True)
|
||||
await e.bot.send_text(e.room, resp)
|
||||
|
||||
async def command_http_https(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
async def command_http_https(e: RoomEventData) -> None:
|
||||
"""The message starts with `http://` or `https://`"""
|
||||
if text.sender == _bot.get_client().user_id:
|
||||
if e.event.sender == _bot.get_client().user_id:
|
||||
return
|
||||
t = asyncio.create_task(worker_download(room, text))
|
||||
if not isinstance(e.event, RoomMessageText):
|
||||
return
|
||||
t = asyncio.create_task(worker_download(e.room, e.event))
|
||||
_tasks.add(t)
|
||||
t.add_done_callback(_tasks.discard)
|
||||
|
||||
async def command_test(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
async def command_test(e: RoomEventData) -> None:
|
||||
"""The message ends with ` TesT` (case matters)"""
|
||||
if text.sender == _bot.get_client().user_id:
|
||||
if e.event.sender == _bot.get_client().user_id:
|
||||
return
|
||||
# glob patterns to use
|
||||
glob_img = ["*.png", "*.jpg", ".jpeg", "*.gif", "*.webp"]
|
||||
@@ -83,35 +85,35 @@ async def command_test(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
resp += "</code>, у которых следующие расширения:<br>"
|
||||
resp += f"<ul><li>Изображения: {", ".join([f"<code>{g}</code>" for g in glob_img])}</li></ul>"
|
||||
resp += f"<ul><li>Видео: {", ".join([f"<code>{g}</code>" for g in glob_vid])}</li></ul>"
|
||||
await _bot.send_text_to_room(room.room_id, resp, is_html=True)
|
||||
await _bot.send_text(e.room, resp)
|
||||
await asyncio.sleep(1)
|
||||
# send images
|
||||
for g in [f"test_media/{g}" for g in glob_img]:
|
||||
imgs = glob.glob(g)
|
||||
for file in imgs:
|
||||
try:
|
||||
await _bot.send_image_to_room(
|
||||
room.room_id,
|
||||
await _bot.send_image(
|
||||
e.room,
|
||||
file,
|
||||
caption=f"Картинка <code>{html.escape(file)}</code>",
|
||||
text=f"Картинка <code>{html.escape(file)}</code>",
|
||||
is_html=True
|
||||
)
|
||||
except:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
await _bot.send_text(e.room, traceback.format_exc())
|
||||
await asyncio.sleep(0.5)
|
||||
# send videos
|
||||
for g in [f"test_media/{g}" for g in glob_vid]:
|
||||
imgs = glob.glob(g)
|
||||
for file in imgs:
|
||||
try:
|
||||
await _bot.send_video_to_room(
|
||||
room.room_id,
|
||||
await _bot.send_video(
|
||||
e.room,
|
||||
file,
|
||||
caption=f"Видео <code>{html.escape(file)}</code>",
|
||||
text=f"Видео <code>{html.escape(file)}</code>",
|
||||
is_html=True
|
||||
)
|
||||
except:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
await _bot.send_text(e.room, traceback.format_exc())
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
#
|
||||
@@ -123,9 +125,12 @@ async def initialize(bot: MatrixBot) -> None:
|
||||
shutil.rmtree("temp", ignore_errors=True)
|
||||
os.makedirs("temp", exist_ok=True)
|
||||
_bot = bot
|
||||
bot.add_event_callback(command_http_https, TextStartsWithFilter(["https://", "http://"]))
|
||||
bot.add_event_callback(command_test, TextEndsWithFilter(" TesT", any_case=False))
|
||||
bot.add_event_callback(command_help, TextFilter())
|
||||
bot.add_callback(
|
||||
TextStartsWithFilter(["https://", "http://"]), command_http_https)
|
||||
bot.add_callback(
|
||||
TextEndsWithFilter(" TesT", any_case=False), command_test)
|
||||
bot.add_callback(
|
||||
TextFilter(), command_help)
|
||||
|
||||
async def deinitizalize(bot: MatrixBot) -> None:
|
||||
"""This function deinitializes the bot. It does not stop it."""
|
||||
|
||||
2
main.py
2
main.py
@@ -42,7 +42,7 @@ async def main() -> None:
|
||||
# setup the bot
|
||||
bot = MatrixBot(bot_config)
|
||||
await logic.initialize(bot)
|
||||
bot.start()
|
||||
await bot.start()
|
||||
|
||||
# wait for termination
|
||||
logging.info("You may use Ctrl+C to terminate the app")
|
||||
|
||||
@@ -16,7 +16,7 @@ hyperframe==6.1.0
|
||||
idna==3.19
|
||||
jsonschema==4.26.0
|
||||
jsonschema-specifications==2025.9.1
|
||||
mab @ git+https://git.tyukalov.su/nikita/mab@v0.2.0
|
||||
mab @ git+https://git.tyukalov.su/nikita/mab@v0.3.0
|
||||
matrix-nio==0.26.0
|
||||
multidict==6.7.1
|
||||
packaging==26.3
|
||||
|
||||
Reference in New Issue
Block a user