Skip to content

Commit

Permalink
Tar -> Zip
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulkhatri137 committed Oct 14, 2021
1 parent 84e39a2 commit 2ab0176
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ This project is heavily inspired from @out386 's telegram bot which is written i

```
mirror - Start Mirroring
tarmirror - Upload tar (zipped) file
zipmirror - Upload zip (zipped) file
unzipmirror - Extract files
clone - copy folder to drive
watch - mirror YT-DL support link
tarwatch - mirror youtube playlist link as tar
zipwatch - mirror youtube playlist link as zip
cancel - Cancel a task
cancelall - Cancel all tasks
del - Delete file from Drive
Expand All @@ -87,7 +87,7 @@ speedtest - Check Speed of the host (Not working currently!)
log - Bot Log [owner only]
usage - Check heroku dyno usage [owner only]
leech - Start leeching
tarleech - Leech as tar (zipped) file
zipleech - Leech as zip (zipped) file
unzipleech - Leech & Extract files
```

Expand Down
13 changes: 6 additions & 7 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ def bot_help(update, context):
/{BotCommands.UnzipMirrorCommand} [download_url][magnet_link] : starts mirroring and if downloaded file is any archive , extracts it to google drive
/{BotCommands.TarMirrorCommand} [download_url][magnet_link]: start mirroring and upload the archived (.tar) version of the download
/{BotCommands.ZipMirrorCommand} [download_url][magnet_link]: start mirroring and upload the archived (.zip) version of the download
/{BotCommands.LeechCommand} [download_url][magnet_link]: Start mirroring the link to google drive
/{BotCommands.LeechCommand} [download_url][magnet_link]: Start mirroring the link & upload to telegram
/{BotCommands.UnzipLeechCommand} [download_url][magnet_link] : starts mirroring and if downloaded file is any archive , extracts it to google drive
/{BotCommands.TarLeechCommand} [download_url][magnet_link]: start mirroring and upload the archived (.tar) version of the download
/{BotCommands.ZipLeechCommand} [download_url][magnet_link]: start mirroring and upload the archived (.zip) version of the download
/{BotCommands.LeechSetCommand}: Leech Settings
/{BotCommands.SetThumbCommand}: Reply photo to set it as Thumbnail
/{BotCommands.WatchCommand} [youtube-dl supported link]: Mirror through youtube-dl. Click /{BotCommands.WatchCommand} for more help.
/{BotCommands.TarWatchCommand} [youtube-dl supported link]: Mirror through youtube-dl and tar before uploading
/{BotCommands.ZipWatchCommand} [youtube-dl supported link]: Mirror through youtube-dl and zip before uploading
/{BotCommands.CancelMirror} : Reply to the message by which the download was initiated and that download will be cancelled
Expand All @@ -119,12 +119,11 @@ def bot_help(update, context):
botcmds = [
(f"{BotCommands.HelpCommand}", "Get detailed help"),
(f"{BotCommands.MirrorCommand}", "Start mirroring"),
(f"{BotCommands.TarMirrorCommand}", "Start mirroring and upload as .tar"),
(f"{BotCommands.ZipMirrorCommand}", "Start mirroring and upload as .zip"),
(f"{BotCommands.UnzipMirrorCommand}", "Extract files"),
(f"{BotCommands.CloneCommand}", "Copy file/folder from GDrive"),
(f"{BotCommands.deleteCommand}", "Delete file from GDrive [owner only]"),
(f"{BotCommands.WatchCommand}", "Mirror Youtube-dl support link"),
(f"{BotCommands.TarWatchCommand}", "Mirror Youtube playlist link as .tar"),
(f"{BotCommands.ZipWatchCommand}", "Mirror Youtube playlist link as .zip"),
(f"{BotCommands.CancelMirror}", "Cancel a task"),
(f"{BotCommands.CancelAllCommand}", "Cancel all tasks [owner only]"),
Expand All @@ -135,7 +134,7 @@ def bot_help(update, context):
(f"{BotCommands.LogCommand}", "Get the bot log [owner only]"),
(f"{BotCommands.LeechCommand}", "Start leeching"),
(f"{BotCommands.LeechSetCommand}", "Leech Settings"),
(f"{BotCommands.TarLeechCommand}", "Start leeching and upload as .tar"),
(f"{BotCommands.ZipLeechCommand}", "Start leeching and upload as .Zip"),
(f"{BotCommands.UnZipLeechCommand}", "Start leeching and upload as .zip"),
]

Expand Down
18 changes: 7 additions & 11 deletions bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
from bot import aria2, LOGGER, DOWNLOAD_DIR
import shutil
import os
import pathlib
import magic
import tarfile
from .exceptions import NotSupportedExtractionArchive
from PIL import Image
from hachoir.parser import createParser
from hachoir.metadata import extractMetadata


def clean_download(path: str):
if os.path.exists(path):
LOGGER.info(f"Cleaning download: {path}")
Expand Down Expand Up @@ -53,14 +50,13 @@ def get_path_size(path):
return total_size


def tar(org_path):
tar_path = org_path + ".tar"
path = pathlib.PurePath(org_path)
LOGGER.info(f'Tar: orig_path: {org_path}, tar_path: {tar_path}')
tar = tarfile.open(tar_path, "w")
tar.add(org_path, arcname=path.name)
tar.close()
return tar_path
def zip(name, path):
root_dir = os.path.dirname(path)
base_dir = os.path.basename(path.strip(os.sep))
zip_file = shutil.make_archive(name, "zip", root_dir, base_dir)
zip_path = shutil.move(zip_file, root_dir)
LOGGER.info(f"Zip: {zip_path}")
return zip_path


def get_base_name(orig_path: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus


class TarStatus(Status):

class ZipStatus(Status):
def __init__(self, name, path, size):
self.__name = name
self.__path = path
self.__size = size

# The progress of Tar function cannot be tracked. So we just return dummy values.
# The progress of Zip function cannot be tracked. So we just return dummy values.
# If this is possible in future,we should implement it

def progress(self):
Expand Down
6 changes: 3 additions & 3 deletions bot/helper/telegram_helper/bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def __init__(self):
self.StartCommand = 'start'
self.MirrorCommand = 'mirror'
self.UnzipMirrorCommand = 'unzipmirror'
self.TarMirrorCommand = 'tarmirror'
self.ZipMirrorCommand = 'zipmirror'
self.CancelMirror = 'cancel'
self.CancelAllCommand = 'cancelall'
self.ListCommand = 'list'
Expand All @@ -18,14 +18,14 @@ def __init__(self):
self.LogCommand = 'log'
self.CloneCommand = "clone"
self.WatchCommand = 'watch'
self.TarWatchCommand = 'tarwatch'
self.ZipWatchCommand = 'zipwatch'
self.deleteCommand = 'del'
self.UsageCommand = 'usage'
self.SpeedCommand = 'speedtest'
self.LeechSetCommand = 'leechset'
self.SetThumbCommand = 'setthumb'
self.LeechCommand = 'leech'
self.TarLeechCommand = 'tarleech'
self.ZipLeechCommand = 'zipleech'
self.UnzipLeechCommand = 'unzipleech'

BotCommands = _BotCommands()
32 changes: 16 additions & 16 deletions bot/modules/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from bot.helper.mirror_utils.download_utils.telegram_downloader import TelegramDownloadHelper
from bot.helper.mirror_utils.status_utils import listeners
from bot.helper.mirror_utils.status_utils.extract_status import ExtractStatus
from bot.helper.mirror_utils.status_utils.tar_status import TarStatus
from bot.helper.mirror_utils.status_utils.zip_status import ZipStatus
from bot.helper.mirror_utils.status_utils.upload_status import UploadStatus
from bot.helper.mirror_utils.status_utils.tg_upload_status import TgUploadStatus
from bot.helper.mirror_utils.upload_utils import gdriveTools, pyrogramEngine
Expand All @@ -33,9 +33,9 @@


class MirrorListener(listeners.MirrorListeners):
def __init__(self, bot, update, pswd, isTar=False, tag=None, extract=False, isLeech=False):
def __init__(self, bot, update, pswd, isZip=False, tag=None, extract=False, isLeech=False):
super().__init__(bot, update)
self.isTar = isTar
self.isZip = isZip
self.tag = tag
self.extract = extract
self.isLeech = isLeech
Expand Down Expand Up @@ -66,12 +66,12 @@ def onDownloadComplete(self):
if name is None: # when pyrogram's media.file_name is of NoneType
name = os.listdir(f'{DOWNLOAD_DIR}{self.uid}')[0]
m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
if self.isTar:
if self.isZip:
download.is_archiving = True
try:
with download_dict_lock:
download_dict[self.uid] = TarStatus(name, m_path, size)
path = fs_utils.tar(m_path)
download_dict[self.uid] = ZipStatus(name, m_path, size)
path = fs_utils.zip(name, m_path)
except FileNotFoundError:
LOGGER.info('File to archive not found!')
self.onUploadError('Internal error occurred!!')
Expand Down Expand Up @@ -260,7 +260,7 @@ def onUploadError(self, error):
update_all_messages()


def _mirror(bot, update, isTar=False, extract=False, isLeech=False):
def _mirror(bot, update, isZip=False, extract=False, isLeech=False):
mesg = update.message.text.split('\n')
message_args = mesg[0].split(' ')
name_args = mesg[0].split('|')
Expand Down Expand Up @@ -306,7 +306,7 @@ def _mirror(bot, update, isTar=False, extract=False, isLeech=False):
if not bot_utils.is_url(link) and not bot_utils.is_magnet(link) or len(link) == 0:
if file is not None:
if file.mime_type != "application/x-bittorrent":
listener = MirrorListener(bot, update, pswd, isTar, tag, extract, isLeech=isLeech)
listener = MirrorListener(bot, update, pswd, isZip, tag, extract, isLeech=isLeech)
tg_downloader = TelegramDownloadHelper(listener)
tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/', name)
sendStatusMessage(update, bot)
Expand All @@ -325,7 +325,7 @@ def _mirror(bot, update, isTar=False, extract=False, isLeech=False):
link = direct_link_generator(link)
except DirectDownloadLinkException as e:
LOGGER.info(f'{link}: {e}')
listener = MirrorListener(bot, update, pswd, isTar, tag, extract, isLeech)
listener = MirrorListener(bot, update, pswd, isZip, tag, extract, isLeech)

if bot_utils.is_mega_link(link) and MEGA_KEY is not None and not BLOCK_MEGA_LINKS:
mega_dl = MegaDownloader(listener)
Expand All @@ -334,7 +334,7 @@ def _mirror(bot, update, isTar=False, extract=False, isLeech=False):
elif bot_utils.is_mega_link(link) and BLOCK_MEGA_LINKS:
sendMessage("🚫Mega links are blocked. Dont try to mirror mega links.", bot, update)
elif bot_utils.is_gdrive_link(link):
if not isTar and not extract and not isLeech:
if not isZip and not extract and not isLeech:
sendMessage(f"Use /{BotCommands.CloneCommand} to clone Google Drive file/folder.", bot, update)
else:
ariaDlManager.add_download(link, f'{DOWNLOAD_DIR}{listener.uid}/', listener, name)
Expand All @@ -347,7 +347,7 @@ def mirror(update, context):
_mirror(context.bot, update)


def tar_mirror(update, context):
def zip_mirror(update, context):
_mirror(context.bot, update, True)


Expand All @@ -357,7 +357,7 @@ def unzip_mirror(update, context):
def leech(update, context):
_mirror(context.bot, update, isLeech=True)

def tar_leech(update, context):
def zip_leech(update, context):
_mirror(context.bot, update, True, isLeech=True)

def unzip_leech(update, context):
Expand All @@ -366,19 +366,19 @@ def unzip_leech(update, context):

mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
tar_mirror_handler = CommandHandler(BotCommands.TarMirrorCommand, tar_mirror,
zip_mirror_handler = CommandHandler(BotCommands.ZipMirrorCommand, zip_mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
unzip_mirror_handler = CommandHandler(BotCommands.UnzipMirrorCommand, unzip_mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
leech_handler = CommandHandler(BotCommands.LeechCommand, leech,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
tar_leech_handler = CommandHandler(BotCommands.TarLeechCommand, tar_leech,
zip_leech_handler = CommandHandler(BotCommands.ZipLeechCommand, zip_leech,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
unzip_leech_handler = CommandHandler(BotCommands.UnzipLeechCommand, unzip_leech,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
dispatcher.add_handler(mirror_handler)
dispatcher.add_handler(tar_mirror_handler)
dispatcher.add_handler(zip_mirror_handler)
dispatcher.add_handler(unzip_mirror_handler)
dispatcher.add_handler(leech_handler)
dispatcher.add_handler(tar_leech_handler)
dispatcher.add_handler(zip_leech_handler)
dispatcher.add_handler(unzip_leech_handler)
10 changes: 5 additions & 5 deletions bot/modules/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import threading


def _watch(bot: Bot, update, isTar=False):
def _watch(bot: Bot, update, isZip=False):
mssg = update.message.text
message_args = mssg.split(' ')
name_args = mssg.split('|')
Expand Down Expand Up @@ -46,15 +46,15 @@ def _watch(bot: Bot, update, isTar=False):
else:
tag = None
pswd = ""
listener = MirrorListener(bot, update, pswd, isTar, tag)
listener = MirrorListener(bot, update, pswd, isZip, tag)
ydl = YoutubeDLHelper(listener)
threading.Thread(target=ydl.add_download,args=(link, f'{DOWNLOAD_DIR}{listener.uid}', qual, name)).start()
sendStatusMessage(update, bot)
if len(Interval) == 0:
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))


def watchTar(update, context):
def watchZip(update, context):
_watch(context.bot, update, True)


Expand All @@ -64,7 +64,7 @@ def watch(update, context):

mirror_handler = CommandHandler(BotCommands.WatchCommand, watch,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
tar_mirror_handler = CommandHandler(BotCommands.TarWatchCommand, watchTar,
zip_mirror_handler = CommandHandler(BotCommands.ZipWatchCommand, watchZip,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
dispatcher.add_handler(mirror_handler)
dispatcher.add_handler(tar_mirror_handler)
dispatcher.add_handler(zip_mirror_handler)

0 comments on commit 2ab0176

Please sign in to comment.