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