Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Dec 11, 2023
1 parent a7e8643 commit 54d1b96
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 87 deletions.
22 changes: 11 additions & 11 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,43 @@


class TaskConfig:
def __init__(self, message):
self.message = message
def __init__(self):
self.mid = self.message.id
self.user = self.message.from_user or self.message.sender_chat
self.user_id = self.user.id
self.user_dict = user_data.get(self.user_id, {})
self.sameDir = {}
self.bulk = []
self.dir = f"{DOWNLOAD_DIR}{self.mid}"
self.link = ""
self.upDest = ""
self.rcFlags = ""
self.options = ""
self.tag = ""
self.name = ""
self.session = ""
self.newDir = ""
self.multiTag = 0
self.splitSize = 0
self.maxSplitSize = 0
self.multi = 0
self.isLeech = False
self.isQbit = False
self.isClone = False
self.isYtDlp = False
self.equalSplits = False
self.userTransmission = False
self.isClone = False
self.isQbit = False
self.isLeech = False
self.extract = False
self.compress = False
self.select = False
self.seed = False
self.compress = False
self.extract = False
self.join = False
self.isYtDlp = False
self.privateLink = False
self.stopDuplicate = False
self.sampleVideo = False
self.screenShots = False
self.as_doc = False
self.suproc = None
self.client = None
self.thumb = None
self.extension_filter = []
self.isSuperChat = self.message.chat.type.name in ["SUPERGROUP", "CHANNEL"]

def getTokenPath(self, dest):
Expand Down Expand Up @@ -137,6 +132,11 @@ async def isTokenExists(self, path, status):
raise ValueError(f"SAccounts or token.pickle: {token_path} not Exists!")

async def beforeStart(self):
self.extension_filter = (
self.user_dict.get("excluded_extensions") or GLOBAL_EXTENSION_FILTER
if "excluded_extensions" not in self.user_dict
else ["aria2", "!qB"]
)
if not self.isYtDlp:
if self.link not in ["rcl", "gdl"]:
await self.isTokenExists(self.link, "dl")
Expand Down
7 changes: 3 additions & 4 deletions bot/helper/ext_utils/files_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from os import walk, path as ospath
from aiofiles.os import remove as aioremove, path as aiopath, listdir, rmdir, makedirs
from aioshutil import rmtree as aiormtree
from shutil import rmtree
from magic import Magic
from re import split as re_split, I, search as re_search
from subprocess import run as srun
from sys import exit as sexit

from .exceptions import NotSupportedExtractionArchive
from bot import aria2, LOGGER, DOWNLOAD_DIR, get_client, GLOBAL_EXTENSION_FILTER
from bot import aria2, LOGGER, DOWNLOAD_DIR, get_client
from bot.helper.ext_utils.bot_utils import sync_to_async, async_to_sync, cmd_exec

ARCH_EXT = [
Expand Down Expand Up @@ -138,13 +137,13 @@ async def get_path_size(path):
return total_size


async def count_files_and_folders(path):
async def count_files_and_folders(path, extension_filter):
total_files = 0
total_folders = 0
for _, dirs, files in await sync_to_async(walk, path):
total_files += len(files)
for f in files:
if f.endswith(tuple(GLOBAL_EXTENSION_FILTER)):
if f.endswith(tuple(extension_filter)):
total_files -= 1
total_folders += len(dirs)
return total_folders, total_files
Expand Down
4 changes: 2 additions & 2 deletions bot/helper/listeners/task_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@


class TaskListener(TaskConfig):
def __init__(self, message):
super().__init__(message)
def __init__(self):
super().__init__()

async def clean(self):
try:
Expand Down
16 changes: 7 additions & 9 deletions bot/helper/mirror_utils/gdrive_utils/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
RetryError,
)

from bot import GLOBAL_EXTENSION_FILTER
from bot.helper.ext_utils.bot_utils import async_to_sync
from bot.helper.mirror_utils.gdrive_utils.helper import GoogleDriveHelper

Expand All @@ -19,8 +18,9 @@

class gdClone(GoogleDriveHelper):
def __init__(self, listener):
super().__init__(listener)
self.listener = listener
self._start_time = time()
super().__init__()
self.is_cloning = True
self.user_setting()

Expand Down Expand Up @@ -109,19 +109,17 @@ def _cloneFolder(self, folder_name, folder_id, dest_id):
files = self.getFilesByFolderId(folder_id)
if len(files) == 0:
return dest_id
if self.listener.user_dict.get("excluded_extensions", False):
extension_filter = self.listener.user_dict["excluded_extensions"]
elif "excluded_extensions" not in self.listener.user_dict:
extension_filter = GLOBAL_EXTENSION_FILTER
else:
extension_filter = ["aria2", "!qB"]
for file in files:
if file.get("mimeType") == self.G_DRIVE_DIR_MIME_TYPE:
self.total_folders += 1
file_path = ospath.join(folder_name, file.get("name"))
current_dir_id = self.create_directory(file.get("name"), dest_id)
self._cloneFolder(file_path, file.get("id"), current_dir_id)
elif not file.get("name").lower().endswith(tuple(extension_filter)):
elif (
not file.get("name")
.lower()
.endswith(tuple(self.listener.extension_filter))
):
self.total_files += 1
self._copyFile(file.get("id"), dest_id)
self.proc_bytes += int(file.get("size", 0))
Expand Down
12 changes: 3 additions & 9 deletions bot/helper/mirror_utils/gdrive_utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
RetryError,
)

from bot import GLOBAL_EXTENSION_FILTER
from bot.helper.ext_utils.bot_utils import setInterval
from bot.helper.ext_utils.bot_utils import async_to_sync
from bot.helper.mirror_utils.gdrive_utils.helper import GoogleDriveHelper
Expand All @@ -21,9 +20,10 @@

class gdDownload(GoogleDriveHelper):
def __init__(self, listener, path):
super().__init__(listener)
self.listener = listener
self._updater = None
self._path = path
super().__init__()
self.is_downloading = True

def download(self):
Expand Down Expand Up @@ -70,12 +70,6 @@ def _download_folder(self, folder_id, path, folder_name):
result = self.getFilesByFolderId(folder_id)
if len(result) == 0:
return
if self.listener.user_dict.get("excluded_extensions", False):
extension_filter = self.listener.user_dict["excluded_extensions"]
elif "excluded_extensions" not in self.listener.user_dict:
extension_filter = GLOBAL_EXTENSION_FILTER
else:
extension_filter = ["aria2", "!qB"]
result = sorted(result, key=lambda k: k["name"])
for item in result:
file_id = item["id"]
Expand All @@ -90,7 +84,7 @@ def _download_folder(self, folder_id, path, folder_name):
self._download_folder(file_id, path, filename)
elif not ospath.isfile(
f"{path}{filename}"
) and not filename.lower().endswith(tuple(extension_filter)):
) and not filename.lower().endswith(tuple(self.listener.extension_filter)):
self._download_file(file_id, path, filename, mime_type)
if self.is_cancelled:
break
Expand Down
3 changes: 1 addition & 2 deletions bot/helper/mirror_utils/gdrive_utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class GoogleDriveHelper:
def __init__(self, listener=None):
def __init__(self):
self._OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]
self.token_path = "token.pickle"
self.G_DRIVE_DIR_MIME_TYPE = "application/vnd.google-apps.folder"
Expand All @@ -37,7 +37,6 @@ def __init__(self, listener=None):
self.sa_count = 1
self.sa_number = 100
self.alt_auth = False
self.listener = listener
self.service = None
self.total_files = 0
self.total_folders = 0
Expand Down
4 changes: 2 additions & 2 deletions bot/helper/mirror_utils/gdrive_utils/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,14 @@ async def id_updates(_, query, obj):

class gdriveList(GoogleDriveHelper):
def __init__(self, listener):
super().__init__(listener)
self.listener = listener
self._token_user = False
self._token_owner = False
self._sa_owner = False
self._reply_to = None
self._time = time()
self._timeout = 240
self.drives = []
self.is_cancelled = False
self.query_proc = False
self.item_type = "folders"
self.event = Event()
Expand All @@ -132,6 +131,7 @@ def __init__(self, listener):
self.items_list = []
self.iter_start = 0
self.page_step = 1
super().__init__()

@new_thread
async def _event_handler(self):
Expand Down
26 changes: 10 additions & 16 deletions bot/helper/mirror_utils/gdrive_utils/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
RetryError,
)

from bot import config_dict, GLOBAL_EXTENSION_FILTER
from bot import config_dict
from bot.helper.ext_utils.files_utils import get_mime_type
from bot.helper.ext_utils.bot_utils import async_to_sync, setInterval
from bot.helper.mirror_utils.gdrive_utils.helper import GoogleDriveHelper
Expand All @@ -20,10 +20,11 @@

class gdUpload(GoogleDriveHelper):
def __init__(self, listener, path):
super().__init__(listener)
self.listener = listener
self._updater = None
self._path = path
self._is_errored = False
super().__init__()
self.is_uploading = True

def user_setting(self):
Expand All @@ -44,14 +45,8 @@ def upload(self, size):
LOGGER.info(f"Uploading: {self._path}")
self._updater = setInterval(self.update_interval, self.progress)
try:
if self.listener.user_dict.get("excluded_extensions", False):
extension_filter = self.listener.user_dict["excluded_extensions"]
elif "excluded_extensions" not in self.listener.user_dict:
extension_filter = GLOBAL_EXTENSION_FILTER
else:
extension_filter = ["aria2", "!qB"]
if ospath.isfile(self._path):
if self._path.lower().endswith(tuple(extension_filter)):
if self._path.lower().endswith(tuple(self.listener.extension_filter)):
raise Exception(
"This file extension is excluded by extension filter!"
)
Expand All @@ -71,9 +66,10 @@ def upload(self, size):
else:
mime_type = "Folder"
dir_id = self.create_directory(
ospath.basename(ospath.abspath(self.listener.name)), self.listener.upDest
ospath.basename(ospath.abspath(self.listener.name)),
self.listener.upDest,
)
result = self._upload_dir(self._path, dir_id, extension_filter)
result = self._upload_dir(self._path, dir_id)
if result is None:
raise Exception("Upload has been manually cancelled!")
link = self.G_DRIVE_DIR_BASE_DOWNLOAD_URL.format(dir_id)
Expand Down Expand Up @@ -108,7 +104,7 @@ def upload(self, size):
dir_id=self.getIdFromUrl(link),
)

def _upload_dir(self, input_directory, dest_id, extension_filter):
def _upload_dir(self, input_directory, dest_id):
list_dirs = listdir(input_directory)
if len(list_dirs) == 0:
return dest_id
Expand All @@ -117,11 +113,9 @@ def _upload_dir(self, input_directory, dest_id, extension_filter):
current_file_name = ospath.join(input_directory, item)
if ospath.isdir(current_file_name):
current_dir_id = self.create_directory(item, dest_id)
new_id = self._upload_dir(
current_file_name, current_dir_id, extension_filter
)
new_id = self._upload_dir(current_file_name, current_dir_id)
self.total_folders += 1
elif not item.lower().endswith(tuple(extension_filter)):
elif not item.lower().endswith(tuple(self.listener.extension_filter)):
mime_type = get_mime_type(current_file_name)
file_name = current_file_name.split("/")[-1]
self._upload_file(current_file_name, file_name, mime_type, dest_id)
Expand Down
20 changes: 6 additions & 14 deletions bot/helper/mirror_utils/rclone_utils/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from random import randrange
from logging import getLogger

from bot import config_dict, GLOBAL_EXTENSION_FILTER
from bot import config_dict
from bot.helper.ext_utils.bot_utils import cmd_exec, sync_to_async
from bot.helper.ext_utils.files_utils import get_mime_type, count_files_and_folders

Expand All @@ -31,8 +31,6 @@ def __init__(self, listener):
self._sa_count = 1
self._sa_index = 0
self._sa_number = 0
self.extension_filter = ["aria2", "!qB"]
self.user_settings()

@property
def transferred_size(self):
Expand All @@ -54,14 +52,6 @@ def eta(self):
def size(self):
return self._size

def user_settings(self):
if self._listener.user_dict.get("excluded_extensions", False):
self.extension_filter = self._listener.user_dict["excluded_extensions"]
elif "excluded_extensions" not in self._listener.user_dict:
self.extension_filter = GLOBAL_EXTENSION_FILTER
else:
self.extension_filter = ["aria2", "!qB"]

async def _progress(self):
while not (self._proc is None or self._is_cancelled):
try:
Expand Down Expand Up @@ -278,10 +268,12 @@ async def upload(self, path, size):

if await aiopath.isdir(path):
mime_type = "Folder"
folders, files = await count_files_and_folders(path)
folders, files = await count_files_and_folders(
path, self._listener.extension_filter
)
rc_path += f"/{self._listener.name}" if rc_path else self._listener.name
else:
if path.lower().endswith(tuple(self.extension_filter)):
if path.lower().endswith(tuple(self._listener.extension_filter)):
await self._listener.onUploadError(
"This file extension is excluded by extension filter!"
)
Expand Down Expand Up @@ -427,7 +419,7 @@ async def clone(self, config_path, src_remote, src_path, mime_type):
return None, None

def _getUpdatedCommand(self, config_path, source, destination, method):
ext = "*.{" + ",".join(self.extension_filter) + "}"
ext = "*.{" + ",".join(self._listener.extension_filter) + "}"
cmd = f'rclone {method} --fast-list --config {config_path} -P "{source}" "{destination}" --exclude "{ext}"'
cmd += " --ignore-case --low-level-retries 1 -M --log-file rlog.txt --log-level DEBUG"
if rcflags := self._listener.rcFlags or config_dict["RCLONE_FLAGS"]:
Expand Down
10 changes: 2 additions & 8 deletions bot/helper/mirror_utils/telegram_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from natsort import natsorted
from aioshutil import copy

from bot import config_dict, GLOBAL_EXTENSION_FILTER, user
from bot import config_dict, user
from bot.helper.ext_utils.files_utils import clean_unwanted, is_archive, get_base_name
from bot.helper.ext_utils.bot_utils import sync_to_async
from bot.helper.ext_utils.media_utils import (
Expand Down Expand Up @@ -223,18 +223,12 @@ async def upload(self, o_files, m_size, size):
res = await self._msg_to_reply()
if not res:
return
if self._listener.user_dict.get("excluded_extensions", False):
extension_filter = self._listener.user_dict["excluded_extensions"]
elif "excluded_extensions" not in self._listener.user_dict:
extension_filter = GLOBAL_EXTENSION_FILTER
else:
extension_filter = ["aria2", "!qB"]
for dirpath, _, files in sorted(await sync_to_async(walk, self._path)):
if dirpath.endswith("/yt-dlp-thumb"):
continue
for file_ in natsorted(files):
self._up_path = ospath.join(dirpath, file_)
if file_.lower().endswith(tuple(extension_filter)):
if file_.lower().endswith(tuple(self._listener.extension_filter)):
if not self._listener.seed or self._listener.newDir:
await aioremove(self._up_path)
continue
Expand Down
Loading

0 comments on commit 54d1b96

Please sign in to comment.