Added basic downloading ability
This commit is contained in:
39
logic.py
39
logic.py
@@ -3,6 +3,8 @@
|
||||
import asyncio
|
||||
import glob
|
||||
import html
|
||||
import os
|
||||
import shutil
|
||||
import traceback
|
||||
|
||||
from mab import MatrixBot
|
||||
@@ -11,10 +13,35 @@ from mab.filters.text import TextFilter, TextStartsWithFilter, TextEndsWithFilte
|
||||
|
||||
from nio import MatrixRoom, RoomMessageText
|
||||
|
||||
from downloader import Downloader
|
||||
|
||||
#
|
||||
# PRIVATE DATA
|
||||
#
|
||||
_bot: MatrixBot
|
||||
_tasks: set[asyncio.Task] = set()
|
||||
|
||||
#
|
||||
# PRIVATE
|
||||
#
|
||||
async def worker_download(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
# respond
|
||||
response = (
|
||||
f"<strong>Скачивание видео по "
|
||||
f"<a href=\"{html.escape(text.body)}\">вашей ссылке</a></strong>"
|
||||
)
|
||||
await _bot.send_text_to_room(room.room_id, response, True)
|
||||
# get formats
|
||||
d = Downloader(text.body)
|
||||
result = await d.download_video("temp")
|
||||
await _bot.send_text_to_room(room.room_id, "<strong>Скачано, загрузка в Matrix...</strong>", True)
|
||||
# upload
|
||||
try:
|
||||
await _bot.send_video_to_room(room.room_id, result, caption=f"Скачано из {text.body}")
|
||||
except:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
finally:
|
||||
os.unlink(result)
|
||||
|
||||
#
|
||||
# CALLBACKS
|
||||
@@ -33,9 +60,9 @@ async def command_http_https(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
"""The message starts with `http://` or `https://`"""
|
||||
if text.sender == _bot.get_client().user_id:
|
||||
return
|
||||
resp = "<strong>Сообщение начинается с <code>http://</code> или с <code>"
|
||||
resp += "https://</code></strong>. Ещё не реализовано."
|
||||
await _bot.send_text_to_room(room.room_id, resp, True)
|
||||
t = asyncio.create_task(worker_download(room, text))
|
||||
_tasks.add(t)
|
||||
t.add_done_callback(_tasks.discard)
|
||||
|
||||
async def command_test(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
"""The message ends with ` TesT` (case matters)"""
|
||||
@@ -83,14 +110,14 @@ async def command_test(room: MatrixRoom, text: RoomMessageText) -> None:
|
||||
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
|
||||
|
||||
#
|
||||
# PUBLIC
|
||||
#
|
||||
async def initialize(bot: MatrixBot) -> None:
|
||||
"""This function initializes the bot. It does not start it."""
|
||||
global _bot
|
||||
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))
|
||||
@@ -98,4 +125,4 @@ async def initialize(bot: MatrixBot) -> None:
|
||||
|
||||
async def deinitizalize(bot: MatrixBot) -> None:
|
||||
"""This function deinitializes the bot. It does not stop it."""
|
||||
pass
|
||||
shutil.rmtree("temp", ignore_errors=True)
|
||||
Reference in New Issue
Block a user