Skip to content

Commit

Permalink
Media Group for all files
Browse files Browse the repository at this point in the history
- other minor fixes

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Jan 1, 2023
1 parent 3308369 commit b6feb5f
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 236 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In each single file there is a major change from base code, it's almost totaly d
- 4GB file upload with premium account
- Upload all files to specific superGroup/channel.
- Leech Split size and equal split size settings for each user
- Ability to upload documents and videos parts in media group. Setting for each user
- Ability to leech files in media group. Setting for each user
### Google
- Stop duplicates for all tasks except yt-dlp tasks
- Download from Google Drive
Expand Down Expand Up @@ -182,7 +182,7 @@ Fill up rest of the fields. Meaning of each field is discussed below. **NOTE**:
- `LEECH_SPLIT_SIZE`: Size of split in bytes. Default is `2GB`. Default is `4GB` if your account is premium. `Int`
- `AS_DOCUMENT`: Default type of Telegram file upload. Default is `False` mean as media. `Bool`
- `EQUAL_SPLITS`: Split files larger than **LEECH_SPLIT_SIZE** into equal parts size (Not working with zip cmd). Default is `False`. `Bool`
- `MEDIA_GROUP`: View Uploaded parts of videos or documents in media group. Default is `False`. `Bool`
- `MEDIA_GROUP`: View Uploaded files in media group. Default is `False`. `Bool`.**NOTE**: Some files will end without any reply, it's hard to manage. Maybe in future i will fix it.
- `LEECH_FILENAME_PREFIX`: Add custom word to leeched file name. `Str`
- `DUMP_CHAT`: Chat ID. Upload files to specific chat. `str`. **NOTE**: Only available for superGroup/channel. Add `-100` before channel/superGroup id. In short don't add bot id or your id!
- `USER_SESSION_STRING`: To download/upload from your telegram account. If you own premium account. To generate session string use this command `python3 generate_string_session.py` after mounting repo folder for sure. `Str`. **NOTE**: You can't use bot with private message. Use it with superGroup.
Expand Down
21 changes: 13 additions & 8 deletions bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ def split_file(path, size, file_, dirpath, split_size, listener, start_time=0, i
if not ospath.exists(dirpath):
mkdir(dirpath)
user_id = listener.message.from_user.id
user_dict = user_data.get(user_id, False)
leech_split_size = user_dict and user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
user_dict = user_data.get(user_id, {})
leech_split_size = user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
parts = ceil(size/leech_split_size)
if (user_dict and user_dict.get('equal_splits') or config_dict['EQUAL_SPLITS']) and not inLoop:
if (user_dict.get('equal_splits') or config_dict['EQUAL_SPLITS']) and not inLoop:
split_size = ceil(size/parts) + 1000
if get_media_streams(path)[0]:
duration = get_media_info(path)[0]
Expand Down Expand Up @@ -234,33 +234,38 @@ def get_media_streams(path):

is_video = False
is_audio = False
is_image = False

mime_type = get_mime_type(path)
if mime_type.startswith('audio'):
is_audio = True
return is_video, is_audio
return is_video, is_audio, is_image

if mime_type.startswith('image'):
is_image = True
return is_video, is_audio, is_image

if path.endswith('.bin') or not mime_type.startswith('video') and not mime_type.endswith('octet-stream'):
return is_video, is_audio
return is_video, is_audio, is_image

try:
result = check_output(["ffprobe", "-hide_banner", "-loglevel", "error", "-print_format",
"json", "-show_streams", path]).decode('utf-8')
except Exception as e:
if not mime_type.endswith('octet-stream'):
LOGGER.error(f'{e}. Mostly file not found!')
return is_video, is_audio
return is_video, is_audio, is_image

fields = eval(result).get('streams')
if fields is None:
LOGGER.error(f"get_media_streams: {result}")
return is_video, is_audio
return is_video, is_audio, is_image

for stream in fields:
if stream.get('codec_type') == 'video':
is_video = True
elif stream.get('codec_type') == 'audio':
is_audio = True

return is_video, is_audio
return is_video, is_audio, is_image

278 changes: 140 additions & 138 deletions bot/helper/mirror_utils/upload_utils/pyrogramEngine.py

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions bot/helper/telegram_helper/message_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from time import sleep, time
from telegram.error import RetryAfter
from pyrogram.errors import FloodWait
from os import remove
from io import BytesIO

from bot import config_dict, LOGGER, status_reply_dict, status_reply_dict_lock, Interval, bot, rss_session
from bot.helper.ext_utils.bot_utils import get_readable_message, setInterval
Expand Down Expand Up @@ -72,17 +72,15 @@ def deleteMessage(bot, message):

def sendLogFile(bot, message):
with open('log.txt', 'rb') as f:
bot.sendDocument(document=f, filename=f.name,
reply_to_message_id=message.message_id,
chat_id=message.chat_id)
bot.sendDocument(document=f, filename=f.name, reply_to_message_id=message.message_id,
chat_id=message.chat_id)

def sendFile(bot, message, name, caption=""):
def sendFile(bot, message, txt, fileName, caption=""):
try:
with open(name, 'rb') as f:
bot.sendDocument(document=f, filename=f.name, reply_to_message_id=message.message_id,
caption=caption, chat_id=message.chat_id)
remove(name)
return
with BytesIO(str.encode(txt)) as document:
document.name = fileName
return bot.sendDocument(document=document, reply_to_message_id=message.message_id,
caption=caption, chat_id=message.chat_id)
except RetryAfter as r:
LOGGER.warning(str(r))
sleep(r.retry_after * 1.5)
Expand Down
6 changes: 2 additions & 4 deletions bot/modules/bot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,8 @@ def edit_bot_settings(update, context):
value = config_dict[data[2]]
if len(str(value)) > 200:
query.answer()
filename = f"{data[2]}.txt"
with open(filename, 'w', encoding='utf-8') as f:
f.write(f'{value}')
sendFile(context.bot, message, filename)
fileName = f"{data[2]}.txt"
sendFile(context.bot, message, value, fileName)
return
elif value == '':
value = None
Expand Down
34 changes: 23 additions & 11 deletions bot/modules/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@


def _clone(message, bot):
if not config_dict['GDRIVE_ID']:
sendMessage('GDRIVE_ID not Provided!', bot, message)
return
args = message.text.split()
reply_to = message.reply_to_message
link = ''
Expand All @@ -34,26 +37,35 @@ def _clone(message, bot):
tag = f"@{reply_to.from_user.username}"
else:
tag = reply_to.from_user.mention_html(reply_to.from_user.first_name)

def __run_multi():
if multi > 1:
sleep(4)
nextmsg = type('nextmsg', (object, ), {'chat_id': message.chat_id,
'message_id': message.reply_to_message.message_id + 1})
cmsg = message.text.split()
cmsg[1] = f"{multi - 1}"
nextmsg = sendMessage(" ".join(cmsg), bot, nextmsg)
nextmsg.from_user.id = message.from_user.id
sleep(4)
Thread(target=_clone, args=(nextmsg, bot)).start()

if is_gdrive_link(link):
gd = GoogleDriveHelper()
res, size, name, files = gd.helper(link)
if res != "":
return sendMessage(res, bot, message)
sendMessage(res, bot, message)
__run_multi()
return
if config_dict['STOP_DUPLICATE']:
LOGGER.info('Checking File/Folder if already in Drive...')
smsg, button = gd.drive_list(name, True, True)
if smsg:
msg = "File/Folder is already available in Drive.\nHere are the search results:"
return sendMessage(msg, bot, message, button)
if multi > 1:
sleep(4)
nextmsg = type('nextmsg', (object, ), {'chat_id': message.chat_id, 'message_id': message.reply_to_message.message_id + 1})
cmsg = message.text.split()
cmsg[1] = f"{multi - 1}"
nextmsg = sendMessage(" ".join(cmsg), bot, nextmsg)
nextmsg.from_user.id = message.from_user.id
sleep(4)
Thread(target=_clone, args=(nextmsg, bot)).start()
sendMessage(msg, bot, message, button)
__run_multi()
return
__run_multi()
if files <= 20:
msg = sendMessage(f"Cloning: <code>{link}</code>", bot, message)
result, button = gd.clone(link)
Expand Down
6 changes: 3 additions & 3 deletions bot/modules/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def onDownloadComplete(self):
if self.uid in non_queued_dl:
non_queued_dl.remove(self.uid)
start_from_queued()
user_dict = user_data.get(self.message.from_user.id, False)
user_dict = user_data.get(self.message.from_user.id, {})
if self.isZip:
if self.seed and self.isLeech:
self.newDir = f"{self.dir}10000"
Expand All @@ -78,7 +78,7 @@ def onDownloadComplete(self):
path = f"{m_path}.zip"
with download_dict_lock:
download_dict[self.uid] = ZipStatus(name, size, gid, self)
LEECH_SPLIT_SIZE = user_dict and user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
LEECH_SPLIT_SIZE = user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
if self.pswd is not None:
if self.isLeech and int(size) > LEECH_SPLIT_SIZE:
LOGGER.info(f'Zip: orig_path: {m_path}, zip_path: {path}.0*')
Expand Down Expand Up @@ -167,7 +167,7 @@ def onDownloadComplete(self):
o_files = []
if not self.isZip:
checked = False
LEECH_SPLIT_SIZE = user_dict and user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
LEECH_SPLIT_SIZE = user_dict.get('split_size') or config_dict['LEECH_SPLIT_SIZE']
for dirpath, subdir, files in walk(up_dir, topdown=False):
for file_ in files:
f_path = ospath.join(dirpath, file_)
Expand Down
56 changes: 31 additions & 25 deletions bot/modules/mirror_leech.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@


def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeech=False):
if not isLeech and not config_dict['GDRIVE_ID']:
sendMessage('GDRIVE_ID not Provided!', bot, message)
return
mesg = message.text.split('\n')
message_args = mesg[0].split(maxsplit=1)
index = 1
Expand Down Expand Up @@ -61,6 +64,18 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
if link.startswith(("|", "pswd:")):
link = ''

def __run_multi():
if multi > 1:
sleep(4)
nextmsg = type('nextmsg', (object, ), {'chat_id': message.chat_id,
'message_id': message.reply_to_message.message_id + 1})
msg = message.text.split(maxsplit=mi+1)
msg[mi] = f"{multi - 1}"
nextmsg = sendMessage(" ".join(msg), bot, nextmsg)
nextmsg.from_user.id = message.from_user.id
sleep(4)
Thread(target=_mirror_leech, args=(bot, nextmsg, isZip, extract, isQbit, isLeech)).start()

name = mesg[0].split('|', maxsplit=1)
if len(name) > 1:
if 'pswd:' in name[0]:
Expand Down Expand Up @@ -100,15 +115,7 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
elif not isQbit and file_.mime_type != "application/x-bittorrent":
listener = MirrorLeechListener(bot, message, isZip, extract, isQbit, isLeech, pswd, tag)
Thread(target=TelegramDownloadHelper(listener).add_download, args=(message, f'{DOWNLOAD_DIR}{listener.uid}/', name)).start()
if multi > 1:
sleep(4)
nextmsg = type('nextmsg', (object, ), {'chat_id': message.chat_id, 'message_id': message.reply_to_message.message_id + 1})
msg = message.text.split(maxsplit=mi+1)
msg[mi] = f"{multi - 1}"
nextmsg = sendMessage(" ".join(msg), bot, nextmsg)
nextmsg.from_user.id = message.from_user.id
sleep(4)
Thread(target=_mirror_leech, args=(bot, nextmsg, isZip, extract, isQbit, isLeech)).start()
__run_multi()
return
else:
link = file_.get_file().file_path
Expand Down Expand Up @@ -144,7 +151,8 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
3. You can add those options <b>d, s and multi</b> randomly. Ex: <code>/cmd</code> d:1:20 s 10 <b>or</b> <code>/cmd</code> s 10 d:0.5:100
4. Commands that start with <b>qb</b> are ONLY for torrents.
'''
return sendMessage(help_msg, bot, message)
sendMessage(help_msg, bot, message)
return

LOGGER.info(link)

Expand All @@ -158,7 +166,9 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
except DirectDownloadLinkException as e:
LOGGER.info(str(e))
if str(e).startswith('ERROR:'):
return sendMessage(str(e), bot, message)
sendMessage(str(e), bot, message)
__run_multi()
return
elif isQbit and not is_magnet(link):
if link.endswith('.torrent') or "https://api.telegram.org/file/" in link:
content_type = None
Expand All @@ -173,17 +183,23 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
t.write(resp.content)
link = str(file_name)
else:
return sendMessage(f"{tag} ERROR: link got HTTP response: {resp.status_code}", bot, message)
sendMessage(f"{tag} ERROR: link got HTTP response: {resp.status_code}", bot, message)
__run_multi()
return
except Exception as e:
error = str(e).replace('<', ' ').replace('>', ' ')
if error.startswith('No connection adapters were found for'):
link = error.split("'")[1]
else:
LOGGER.error(str(e))
return sendMessage(tag + " " + error, bot, message)
sendMessage(tag + " " + error, bot, message)
__run_multi()
return
else:
msg = "Qb commands for torrents only. if you are trying to dowload torrent then report."
return sendMessage(msg, bot, message)
sendMessage(msg, bot, message)
__run_multi()
return

listener = MirrorLeechListener(bot, message, isZip, extract, isQbit, isLeech, pswd, tag, select, seed)

Expand Down Expand Up @@ -213,17 +229,7 @@ def _mirror_leech(bot, message, isZip=False, extract=False, isQbit=False, isLeec
auth = ''
Thread(target=add_aria2c_download, args=(link, f'{DOWNLOAD_DIR}{listener.uid}', listener, name,
auth, ratio, seed_time)).start()

if multi > 1:
sleep(4)
nextmsg = type('nextmsg', (object, ), {'chat_id': message.chat_id, 'message_id': message.reply_to_message.message_id + 1})
msg = message.text.split(maxsplit=mi+1)
msg[mi] = f"{multi - 1}"
nextmsg = sendMessage(" ".join(msg), bot, nextmsg)
nextmsg.from_user.id = message.from_user.id
sleep(4)
Thread(target=_mirror_leech, args=(bot, nextmsg, isZip, extract, isQbit, isLeech)).start()

__run_multi()

def mirror(update, context):
_mirror_leech(context.bot, update.message)
Expand Down
Loading

0 comments on commit b6feb5f

Please sign in to comment.