From 8844c7cde9fdb4689be5888cd737c361ab3ac34d Mon Sep 17 00:00:00 2001 From: Aza Maulana Date: Sun, 4 Oct 2020 23:03:25 +0700 Subject: [PATCH] closed #3 : adding default thumbnail --- bot/__init__.py | 7 +++-- bot/__main__.py | 19 ++++++++++++- bot/handlers/thumbnail_handler.py | 23 +++++++++++++++ bot/handlers/upload_to_tg_handler.py | 9 ++++-- bot/locals/default.py | 8 ++++++ bot/plugins/thumbnail_video.py | 42 ++++++++++++++++++++++++++-- 6 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 bot/handlers/thumbnail_handler.py diff --git a/bot/__init__.py b/bot/__init__.py index f0142502..e8a080c1 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -17,7 +17,8 @@ 'EDIT_SLEEP' : 3, 'UPLOAD_MAX_SIZE' : 2000 * 1024 * 1024, 'ARIA2_DIR' : 'downloads', - 'BAR_SIZE' : 10 + 'BAR_SIZE' : 10, + 'THUMBNAIL_NAME' : 'default_thumbnail.jpg' }) # GOAL: @@ -79,7 +80,9 @@ 'HELP' : 'help', 'LEECH' : 'leech', 'CANCEL_LEECH' : 'cancel', - 'LEECH_LIST' : 'list' + 'LEECH_LIST' : 'list', + 'SET_THUMBNAIL' : 'set_thumbnail', + 'RESET_THUMBNAIL' : 'reset_thumbnail' }) # GOAL: diff --git a/bot/__main__.py b/bot/__main__.py index 27e31baf..54cf23e4 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -8,7 +8,8 @@ help_message_handler, leech_handler, cancel_leech_handler, - leech_list_handler + leech_list_handler, + thumbnail_handler ) # Initialize bot @@ -81,6 +82,22 @@ ) +# register /set_thumbnail handler +app.add_handler( + MessageHandler( + thumbnail_handler.set, + filters=Filters.command(COMMAND.SET_THUMBNAIL) + ) +) + +# register /reset_thumbnail handler +app.add_handler( + MessageHandler( + thumbnail_handler.reset, + filters=Filters.command(COMMAND.RESET_THUMBNAIL) + ) +) + # cancel button handler app.add_handler( CallbackQueryHandler( diff --git a/bot/handlers/thumbnail_handler.py b/bot/handlers/thumbnail_handler.py new file mode 100644 index 00000000..3e6d349a --- /dev/null +++ b/bot/handlers/thumbnail_handler.py @@ -0,0 +1,23 @@ +from pyrogram import Client, Message +from os.path import join as os_path_join +from bot import COMMAND, LOCAL, CONFIG +from bot.plugins import thumbnail_video + +thumbnail_path = os_path_join(CONFIG.ROOT, CONFIG.WORKDIR, CONFIG.THUMBNAIL_NAME) + +async def set(client : Client, message: Message): + if not message.photo: + return await message.reply_text(LOCAL.THUMBNAIL_NO_PHOTO.format(cmd_set_thumbnail = COMMAND.SET_THUMBNAIL)) + reply = await message.reply_text(LOCAL.THUMBNAIL_DOWNLOADING) + await message.download( + file_name = thumbnail_path + ) + await reply.edit_text(LOCAL.THUMBNAIL_DOWNLOADED) + await thumbnail_video.set(thumbnail_path) + await reply.edit_text(LOCAL.THUMBNAIL_READY) + +async def reset(client : Client, message: Message): + reply = await message.reply_text(LOCAL.THUMBNAIL_DELETING) + await thumbnail_video.reset(thumbnail_path) + await reply.edit_text(LOCAL.THUMBNAIL_RESETED) + diff --git a/bot/handlers/upload_to_tg_handler.py b/bot/handlers/upload_to_tg_handler.py index 1dfb1e09..59558494 100644 --- a/bot/handlers/upload_to_tg_handler.py +++ b/bot/handlers/upload_to_tg_handler.py @@ -62,18 +62,21 @@ async def upload_fn(chat_id, file, **kwargs): name = file.name ) ) - thumbnail = await thumbnail_video.func(file.path) + thumbnail = os_path.join(CONFIG.ROOT, CONFIG.WORKDIR, CONFIG.THUMBNAIL_NAME) + use_default_thumbnail = os_path.exists(thumbnail) + if not use_default_thumbnail: + thumbnail = await thumbnail_video.func(file.path) await client.send_video( chat_id, file, supports_streaming=True, - thumb=str(thumbnail) or None, + thumb=str(thumbnail), height=height, width=width, duration=duration, **kwargs ) - if thumbnail: + if not use_default_thumbnail: os_remove(str(thumbnail)) else: upload_fn = client.send_document diff --git a/bot/locals/default.py b/bot/locals/default.py index 09ea06c4..c436f590 100644 --- a/bot/locals/default.py +++ b/bot/locals/default.py @@ -20,12 +20,20 @@ 'GENERATE_THUMBNAIL' : 'Thumbnail : Generating.\n{name}', 'SPLIT_FILE' : 'Spliting : {name}', 'SPLIT_FAILED' : 'Split : Failed.\n{name}', + 'THUMBNAIL_NO_PHOTO' : 'Set /{cmd_set_thumbnail} as your photo caption.', + 'THUMBNAIL_DOWNLOADING' : 'Downloading thumbnail.', + 'THUMBNAIL_DOWNLOADED' : 'Thumbnail download.', + 'THUMBNAIL_READY' : 'Thumbnail ready to use.', + 'THUMBNAIL_DELETING' : 'Deleting thumbnail', + 'THUMBNAIL_RESETED' : 'Thumbnail reseted', 'COMMAND_START' : 'start bot', 'COMMAND_PASSWORD' : 'enter password that required', 'COMMAND_HELP' : 'this message', 'COMMAND_LEECH' : 'leech link or magnet', 'COMMAND_CANCEL_LEECH' : 'cancel leeching', 'COMMAND_LEECH_LIST' : 'list on going leech', + 'COMMAND_SET_THUMBNAIL' : 'set custom video thumbail', + 'COMMAND_RESET_THUMBNAIL' : 'reset custom video thumbnail', 'BLOCK_EMPTY' : "▱", "BLOCK_FILLED" : "▰" }) \ No newline at end of file diff --git a/bot/plugins/thumbnail_video.py b/bot/plugins/thumbnail_video.py index 7c9978de..0eb647ef 100644 --- a/bot/plugins/thumbnail_video.py +++ b/bot/plugins/thumbnail_video.py @@ -8,7 +8,7 @@ # GOAL: # create video thumbnail maker handler class -from os import path as os_path +from os import path as os_path, rename as os_rename, remove as os_remove import asyncio from bot.plugins import ffprobe @@ -51,4 +51,42 @@ async def func(filepath): await process.communicate() LOGGER.debug('Thumbnail : ' + out_file) - return out_file \ No newline at end of file + return out_file + +async def set(filepath): + if not os_path.exists(filepath): + LOGGER.error('File not found : ' + filepath) + return False + + prepare_path = filepath + '.prep' + os_rename(filepath, prepare_path) + + cmd = [ + "ffmpeg", + "-hide_banner", + "-i", + prepare_path, + '-vf', + 'scale=320:-1', + '-y', + filepath + ] + LOGGER.debug(cmd) + + process = await asyncio.create_subprocess_exec( + *cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + await process.communicate() + os_remove(prepare_path) + LOGGER.debug('Set thumbnail : ' + filepath) + return True + +async def reset(filepath): + if not os_path.exists(filepath): + LOGGER.error('File not found : ' + filepath) + return True + + os_remove(filepath) + return True \ No newline at end of file