Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
closed #3 : adding default thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
azamaulanaaa committed Oct 4, 2020
1 parent 215531d commit 8844c7c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 8 deletions.
7 changes: 5 additions & 2 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 18 additions & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
help_message_handler,
leech_handler,
cancel_leech_handler,
leech_list_handler
leech_list_handler,
thumbnail_handler
)

# Initialize bot
Expand Down Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions bot/handlers/thumbnail_handler.py
Original file line number Diff line number Diff line change
@@ -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)

9 changes: 6 additions & 3 deletions bot/handlers/upload_to_tg_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions bot/locals/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
'GENERATE_THUMBNAIL' : 'Thumbnail : Generating.\n<code>{name}</code>',
'SPLIT_FILE' : 'Spliting : <code>{name}</code>',
'SPLIT_FAILED' : 'Split : Failed.\n<code>{name}</code>',
'THUMBNAIL_NO_PHOTO' : 'Set <code>/{cmd_set_thumbnail}</code> 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" : "▰"
})
42 changes: 40 additions & 2 deletions bot/plugins/thumbnail_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -51,4 +51,42 @@ async def func(filepath):
await process.communicate()

LOGGER.debug('Thumbnail : ' + out_file)
return out_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

0 comments on commit 8844c7c

Please sign in to comment.