Skip to content

Commit

Permalink
Improve Leech
Browse files Browse the repository at this point in the history
Signed-off-by: anas <e.anastayyar@gmail.com>
  • Loading branch information
anasty17 committed Sep 24, 2021
1 parent c4035d4 commit 845a737
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
28 changes: 15 additions & 13 deletions bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,37 @@ def get_mime_type(file_path):
mime_type = mime_type or "text/plain"
return mime_type

def take_ss(video_file, duration):
def take_ss(video_file):
des_dir = 'Thumbnails'
if not os.path.exists(des_dir):
os.mkdir(des_dir)
des_dir = os.path.join(des_dir, f"{time.time()}.jpg")
metadata = extractMetadata(createParser(video_file))
if metadata.has("duration"):
duration = metadata.get('duration').seconds
else:
duration = 5
duration = int(duration) / 2
subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-ss", str(duration),
"-i", video_file, "-vframes", "1", des_dir])
if not os.path.lexists(des_dir):
return None, 0
return None

Image.open(des_dir).convert("RGB").save(des_dir)
img = Image.open(des_dir)
w, h = img.size
img.resize((320, h))
img.resize((480, 320))
img.save(des_dir, "JPEG")
return des_dir, 320
return des_dir

def split(path, size, split_size, start_time=0, i=1):
out_dir = os.path.dirname(path)
base_name = os.path.basename(path)
if base_name.upper().endswith(VIDEO_SUFFIXES):
base_name, extension = os.path.splitext(path)
def split(path, size, file, dirpath, split_size, start_time=0, i=1):
if file.upper().endswith(VIDEO_SUFFIXES):
base_name, extension = os.path.splitext(file)
metadata = extractMetadata(createParser(path))
total_duration = metadata.get('duration').seconds - 8
split_size = split_size - 2000000
while start_time < total_duration:
parted_name = "{}.part{}{}".format(str(base_name), str(i).zfill(3), str(extension))
out_path = os.path.join(out_dir, parted_name)
out_path = os.path.join(dirpath, parted_name)
subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-i",
path, "-ss", str(start_time), "-fs", str(split_size),
"-strict", "-2", "-c", "copy", out_path])
Expand All @@ -193,11 +195,11 @@ def split(path, size, split_size, start_time=0, i=1):
dif = out_size - TG_SPLIT_SIZE
split_size = TG_SPLIT_SIZE - dif
os.remove(out_path)
return split(path, size, split_size, start_time, i)
return split(path, size, file, dirpath, split_size, start_time, i)
metadata = extractMetadata(createParser(out_path))
start_time = start_time + metadata.get('duration').seconds - 5
i = i + 1
else:
out_path = os.path.join(out_dir, base_name + ".")
out_path = os.path.join(dirpath, file + ".")
subprocess.run(["split", "--numeric-suffixes=1", "--suffix-length=3", f"--bytes={split_size}", path, out_path])

26 changes: 17 additions & 9 deletions bot/helper/mirror_utils/upload_utils/pyrogramEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
LOGGER = logging.getLogger(__name__)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

VIDEO_SUFFIXES = ("M4V", "MP4", "MOV", "FLV", "WMV", "3GP", "MPG", "WEBM", "MKV", "AVI")
VIDEO_SUFFIXES = ("MKV", "MP4", "MOV", "WMV", "3GP", "MPG", "WEBM", "AVI", "FLV", "M4V")
AUDIO_SUFFIXES = ("MP3", "M4A", "M4B", "FLAC", "WAV", "AIF", "OGG", "AAC", "DTS", "MID", "AMR", "MKA")
IMAGE_SUFFIXES = ("JPG", "JPX", "PNG", "GIF", "WEBP", "CR2", "TIF", "BMP", "JXR", "PSD", "ICO", "HEIC")

Expand Down Expand Up @@ -48,43 +48,46 @@ def upload(self):
if self.is_cancelled:
return
up_path = os.path.join(dirpath, file)
self.upload_file(up_path, file)
self.upload_file(up_path, file, dirpath)
if self.is_cancelled:
return
msgs_dict[file] = self.sent_msg.message_id
os.remove(up_path)
self.last_uploaded = 0
LOGGER.info(f"Leech Done: {self.name}")
self.__listener.onUploadComplete(self.name, None, msgs_dict, None, None)

def upload_file(self, up_path, file):
def upload_file(self, up_path, file, dirpath):
cap_mono = f"<code>{file}</code>"
notMedia = False
thumb = self.thumb
try:
if not self.as_doc:
duration = 0
if file.upper().endswith(VIDEO_SUFFIXES):
width = 0
height = 0
metadata = extractMetadata(createParser(up_path))
if metadata.has("duration"):
duration = metadata.get("duration").seconds
if thumb is None:
thumb, width = take_ss(up_path, duration)
thumb = take_ss(up_path)
if self.is_cancelled:
return
if not file.upper().endswith(("MKV", "MP4")):
file = os.path.splitext(file)[0] + '.mp4'
new_path = os.path.join(dirpath, file)
os.rename(up_path, new_path)
up_path = new_path
self.sent_msg = self.sent_msg.reply_video(video=up_path,
quote=True,
caption=cap_mono,
parse_mode="html",
duration=duration,
width=width,
height=height,
width=480,
height=320,
thumb=thumb,
supports_streaming=True,
disable_notification=True,
progress=self.upload_progress)
os.remove(up_path)
if self.thumb is None and thumb is not None and os.path.lexists(thumb):
os.remove(thumb)
elif file.upper().endswith(AUDIO_SUFFIXES):
Expand All @@ -103,23 +106,28 @@ def upload_file(self, up_path, file):
thumb=thumb,
disable_notification=True,
progress=self.upload_progress)
os.remove(up_path)
elif file.upper().endswith(IMAGE_SUFFIXES):
self.sent_msg = self.sent_msg.reply_photo(photo=up_path,
quote=True,
caption=cap_mono,
parse_mode="html",
disable_notification=True,
progress=self.upload_progress)
os.remove(up_path)
else:
notMedia = True
if self.as_doc or notMedia:
if file.upper().endswith(VIDEO_SUFFIXES) and thumb is None:
thumb = take_ss(up_path)
self.sent_msg = self.sent_msg.reply_document(document=up_path,
quote=True,
thumb=thumb,
caption=cap_mono,
parse_mode="html",
disable_notification=True,
progress=self.upload_progress)
os.remove(up_path)
except FloodWait as f:
LOGGER.info(f)
time.sleep(f.x)
Expand Down
7 changes: 3 additions & 4 deletions bot/modules/leech_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def leechSet(update, context):
msg += "DOCUMENT"
else:
msg += "MEDIA"
msg += "\nCustom Thmubnail "
msg += "\nCustom Thumbnail "
msg += "exists" if os.path.exists(path) else "not exists"
buttons = button_build.ButtonMaker()
buttons.sbutton("As Document", f"doc {user_id}")
Expand Down Expand Up @@ -93,9 +93,8 @@ def setThumb(update, context):
des_dir = os.path.join(path, str(user_id) + ".jpg")
# Image.open(photo_dir).convert("RGB").save(photo_dir)
img = Image.open(photo_dir)
w, h = img.size
img.thumbnail((320, h))
# img.resize((320, h))
img.thumbnail((480, 320))
# img.resize((480, 320))
img.save(des_dir, "JPEG")
os.remove(photo_dir)
sendMessage(f"Custom thumbnail saved for {user_id} user.", context.bot, update)
Expand Down
9 changes: 4 additions & 5 deletions bot/modules/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, \
BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK, aria2, \
dispatcher, DOWNLOAD_DIR, download_dict, download_dict_lock, SHORTENER, SHORTENER_API, \
TAR_UNZIP_LIMIT, TG_SPLIT_SIZE, OWNER_ID
TAR_UNZIP_LIMIT, TG_SPLIT_SIZE
from bot.helper.ext_utils import fs_utils, bot_utils
from bot.helper.ext_utils.shortenurl import short_url
from bot.helper.ext_utils.exceptions import DirectDownloadLinkException, NotSupportedExtractionArchive
Expand Down Expand Up @@ -150,7 +150,7 @@ def onDownloadComplete(self):
with download_dict_lock:
download_dict[self.uid] = SplitStatus(up_name, up_path, size)
LOGGER.info(f"Splitting: {up_name}")
fs_utils.split(f_path, f_size, TG_SPLIT_SIZE)
fs_utils.split(f_path, f_size, file, dirpath, TG_SPLIT_SIZE)
os.remove(f_path)
LOGGER.info(f"Leech Name: {up_name}")
tg = pyrogramEngine.TgUploader(up_name, self)
Expand Down Expand Up @@ -202,14 +202,13 @@ def onUploadComplete(self, link: str, size, files, folders, typ):
uname = f"@{self.message.from_user.username}"
else:
uname = f'<a href="tg://user?id={self.message.from_user.id}">{self.message.from_user.first_name}</a>'
chat_id = str(self.message.chat.id)
count = len(files)
if OWNER_ID == int(chat_id) and count != 1:
if self.message.chat.type == 'private':
msg = f'<b>Name:</b> <code>{link}</code>\n'
msg += f'<b>Total Files:</b> {count}'
sendMessage(msg, self.bot, self.update)
else:
chat_id = chat_id[4:]
chat_id = str(self.message.chat.id)[4:]
msg = f"<b>Name:</b> <a href='https://t.me/c/{chat_id}/{self.uid}'>{link}</a>\n"
msg += f'<b>Total Files:</b> {count}\n'
msg += f'cc: {uname}\n\n'
Expand Down

0 comments on commit 845a737

Please sign in to comment.