From 1d809560d5adf5976fc49b502d29e17e8e884efe Mon Sep 17 00:00:00 2001
From: Hafitz Setya <71178188+breakdowns@users.noreply.github.com>
Date: Wed, 10 Mar 2021 08:46:46 +0700
Subject: [PATCH] v3.9 (#19)
- Added option to block mega folder support
---
app.json | 4 ++++
bot/__init__.py | 8 ++++++++
bot/helper/ext_utils/bot_utils.py | 12 ++++++------
.../mirror_utils/upload_utils/gdriveTools.py | 4 ++--
bot/helper/telegram_helper/message_utils.py | 16 ++++++++--------
bot/modules/mirror.py | 13 ++++++++-----
config_sample.env | 1 +
7 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/app.json b/app.json
index b468b62d362..19e9496ae3c 100644
--- a/app.json
+++ b/app.json
@@ -82,6 +82,10 @@
"description": "Your password for your mega.nz account.",
"required": false
},
+ "BLOCK_MEGA_FOLDER": {
+ "description": "If you want to remove mega.nz folder support, set it to True.",
+ "required": false
+ },
"BLOCK_MEGA_LINKS": {
"description": "If you want to remove mega.nz mirror support, set it to True.",
"required": false
diff --git a/bot/__init__.py b/bot/__init__.py
index 83d581b7d63..308776ee111 100644
--- a/bot/__init__.py
+++ b/bot/__init__.py
@@ -194,6 +194,14 @@ def getConfig(name: str):
USE_SERVICE_ACCOUNTS = False
except KeyError:
USE_SERVICE_ACCOUNTS = False
+try:
+ BLOCK_MEGA_FOLDER = getConfig('BLOCK_MEGA_FOLDER')
+ if BLOCK_MEGA_FOLDER.lower() == 'true':
+ BLOCK_MEGA_FOLDER = True
+ else:
+ BLOCK_MEGA_FOLDER = False
+except KeyError:
+ BLOCK_MEGA_FOLDER = False
try:
BLOCK_MEGA_LINKS = getConfig('BLOCK_MEGA_LINKS')
if BLOCK_MEGA_LINKS.lower() == 'true':
diff --git a/bot/helper/ext_utils/bot_utils.py b/bot/helper/ext_utils/bot_utils.py
index 085c12014da..0c8e7bf4ee6 100644
--- a/bot/helper/ext_utils/bot_utils.py
+++ b/bot/helper/ext_utils/bot_utils.py
@@ -93,19 +93,19 @@ def get_readable_message():
with download_dict_lock:
msg = ""
for download in list(download_dict.values()):
- msg += f"Filename : {download.name()}
"
- msg += f"\nStatus : {download.status()}"
+ msg += f"Filename: {download.name()}
"
+ msg += f"\nStatus: {download.status()}"
if download.status() != MirrorStatus.STATUS_ARCHIVING and download.status() != MirrorStatus.STATUS_EXTRACTING:
msg += f"\n{get_progress_bar_string(download)} {download.progress()}
"
if download.status() == MirrorStatus.STATUS_DOWNLOADING:
- msg += f"\nDownloaded : {get_readable_file_size(download.processed_bytes())} of {download.size()}"
+ msg += f"\nDownloaded: {get_readable_file_size(download.processed_bytes())} of {download.size()}"
else:
- msg += f"\nUploaded : {get_readable_file_size(download.processed_bytes())} of {download.size()}"
+ msg += f"\nUploaded: {get_readable_file_size(download.processed_bytes())} of {download.size()}"
msg += f"\nSpeed : {download.speed()}, \nETA: {download.eta()} "
# if hasattr(download, 'is_torrent'):
try:
- msg += f"\nInfo:- Seeders: {download.aria_download().num_seeders}" \
- f" & Peers : {download.aria_download().connections}"
+ msg += f"\nSeeders: {download.aria_download().num_seeders}" \
+ f" | Peers: {download.aria_download().connections}"
except:
pass
if download.status() == MirrorStatus.STATUS_DOWNLOADING:
diff --git a/bot/helper/mirror_utils/upload_utils/gdriveTools.py b/bot/helper/mirror_utils/upload_utils/gdriveTools.py
index 35fd6feeb83..d7d1ab5452a 100644
--- a/bot/helper/mirror_utils/upload_utils/gdriveTools.py
+++ b/bot/helper/mirror_utils/upload_utils/gdriveTools.py
@@ -317,7 +317,7 @@ def clone(self, link):
if meta.get("mimeType") == self.__G_DRIVE_DIR_MIME_TYPE:
dir_id = self.create_directory(meta.get('name'), parent_id)
result = self.cloneFolder(meta.get('name'), meta.get('name'), meta.get('id'), dir_id)
- msg += f'Filename : {meta.get("name")}
\nSize : {get_readable_file_size(self.transferred_size)}'
+ msg += f'Filename: {meta.get("name")}
\nSize: {get_readable_file_size(self.transferred_size)}'
durl = self.__G_DRIVE_DIR_BASE_DOWNLOAD_URL.format(dir_id)
buttons = button_build.ButtonMaker()
if SHORTENER is not None and SHORTENER_API is not None:
@@ -349,7 +349,7 @@ def clone(self, link):
else:
buttons.buildbutton("Drive Link", durl)
try:
- msg += f'\nSize : {get_readable_file_size(int(meta.get("size")))}
'
+ msg += f'\nSize: {get_readable_file_size(int(meta.get("size")))}
'
except TypeError:
pass
if INDEX_URL is not None:
diff --git a/bot/helper/telegram_helper/message_utils.py b/bot/helper/telegram_helper/message_utils.py
index 16671c5ca39..cd9dd1587a3 100644
--- a/bot/helper/telegram_helper/message_utils.py
+++ b/bot/helper/telegram_helper/message_utils.py
@@ -68,9 +68,9 @@ def delete_all_messages():
def update_all_messages():
msg = get_readable_message()
- msg += f"CPU: {psutil.cpu_percent()}%" \
- f" DISK: {psutil.disk_usage('/').percent}%" \
- f" RAM: {psutil.virtual_memory().percent}%"
+ msg += f"CPU: {psutil.cpu_percent()}%\n" \
+ f"RAM: {psutil.virtual_memory().percent}%\n" \
+ f"DISK: {psutil.disk_usage('/').percent}%\n"
with download_dict_lock:
dlspeed_bytes = 0
uldl_bytes = 0
@@ -88,7 +88,7 @@ def update_all_messages():
uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes)
- msg += f"\nDL: {dlspeed}ps 🔻| UL: {ulspeed}ps 🔺\n"
+ msg += f"\nDL: {dlspeed}ps | UL: {ulspeed}ps \n"
with status_reply_dict_lock:
for chat_id in list(status_reply_dict.keys()):
if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text:
@@ -104,9 +104,9 @@ def update_all_messages():
def sendStatusMessage(msg, bot):
progress = get_readable_message()
- progress += f"CPU: {psutil.cpu_percent()}%" \
- f" DISK: {psutil.disk_usage('/').percent}%" \
- f" RAM: {psutil.virtual_memory().percent}%"
+ progress += f"CPU: {psutil.cpu_percent()}%\n" \
+ f"RAM: {psutil.virtual_memory().percent}%\n" \
+ f"DISK: {psutil.disk_usage('/').percent}%\n"
with download_dict_lock:
dlspeed_bytes = 0
uldl_bytes = 0
@@ -124,7 +124,7 @@ def sendStatusMessage(msg, bot):
uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes)
- progress += f"\nDL:{dlspeed}ps 🔻| UL:{ulspeed}ps 🔺\n"
+ progress += f"\nDL: {dlspeed}ps | UL: {ulspeed}ps \n"
with status_reply_dict_lock:
if msg.message.chat.id in list(status_reply_dict.keys()):
try:
diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py
index 84547e9fc02..bbecd4fce78 100644
--- a/bot/modules/mirror.py
+++ b/bot/modules/mirror.py
@@ -2,10 +2,10 @@
from telegram.ext import CommandHandler, run_async
from telegram import InlineKeyboardMarkup
-from bot import Interval, INDEX_URL, BUTTON_THREE_NAME, BUTTON_THREE_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BLOCK_MEGA_LINKS
+from bot import Interval, INDEX_URL, BUTTON_THREE_NAME, BUTTON_THREE_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BLOCK_MEGA_LINKS, BLOCK_MEGA_FOLDER
from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock, SHORTENER, SHORTENER_API
from bot.helper.ext_utils import fs_utils, bot_utils
-from bot.helper.ext_utils.bot_utils import setInterval
+from bot.helper.ext_utils.bot_utils import setInterval, get_mega_link_type
from bot.helper.ext_utils.exceptions import DirectDownloadLinkException, NotSupportedExtractionArchive
from bot.helper.mirror_utils.download_utils.aria2_download import AriaDownloadHelper
from bot.helper.mirror_utils.download_utils.mega_downloader import MegaDownloadHelper
@@ -139,7 +139,7 @@ def onUploadProgress(self):
def onUploadComplete(self, link: str, size):
with download_dict_lock:
- msg = f'Filename : {download_dict[self.uid].name()}
\nSize : {size}
'
+ msg = f'Filename: {download_dict[self.uid].name()}
\nSize: {size}
'
buttons = button_build.ButtonMaker()
if SHORTENER is not None and SHORTENER_API is not None:
surl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, link)).text
@@ -167,7 +167,7 @@ def onUploadComplete(self, link: str, size):
else:
uname = f'{self.message.from_user.first_name}'
if uname is not None:
- msg += f'\n\ncc : {uname}'
+ msg += f'\n\ncc: {uname}'
try:
fs_utils.clean_download(download_dict[self.uid].path())
except FileNotFoundError:
@@ -238,7 +238,10 @@ def _mirror(bot, update, isTar=False, extract=False):
listener = MirrorListener(bot, update, isTar, tag, extract)
if bot_utils.is_mega_link(link):
- if BLOCK_MEGA_LINKS:
+ link_type = get_mega_link_type(link)
+ if link_type == "folder" and BLOCK_MEGA_FOLDER:
+ sendMessage("Mega folder are blocked!", bot, update)
+ elif BLOCK_MEGA_LINKS:
sendMessage("Mega links are blocked bcoz mega downloading is too much unstable and buggy. mega support will be added back after fix", bot, update)
else:
mega_dl = MegaDownloadHelper()
diff --git a/config_sample.env b/config_sample.env
index 31ac60835c3..1c6256055f9 100644
--- a/config_sample.env
+++ b/config_sample.env
@@ -19,6 +19,7 @@ UPTOBOX_TOKEN = ""
MEGA_API_KEY = ""
MEGA_EMAIL_ID = ""
MEGA_PASSWORD = ""
+BLOCK_MEGA_FOLDER = ""
BLOCK_MEGA_LINKS = ""
STOP_DUPLICATE_MIRROR = ""
SHORTENER = ""