Skip to content

Commit

Permalink
Added support for splitted archives
Browse files Browse the repository at this point in the history
Still not tested tho
  • Loading branch information
Itz-fork committed Jun 28, 2022
1 parent 40b3bab commit 9e206c8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 18 deletions.
30 changes: 30 additions & 0 deletions unzipper/helpers_nexa/database/split_arc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2022 Itz-fork
# Don't kang this else your dad is gae

from . import unzipper_db

spl_db = unzipper_db["splitted_archive_users"]


async def add_split_arc_user(uid: int, fn: str, passw: str):
is_exist = await spl_db.find_one({"_id": uid})
if is_exist:
await spl_db.insert_one({"_id": uid, "file_name": fn, "password": passw})
else:
raise ValueError("Data already exists!")


async def get_split_arc_user(uid: int):
gsau = await spl_db.find_one({"_id": uid})
if gsau:
return True, gsau["file_name"]
else:
return False, None


async def del_split_arc_user(uid: int):
is_exist = await spl_db.find_one({"_id": uid})
if is_exist:
await spl_db.delete_one({"_id": uid})
else:
return
6 changes: 6 additions & 0 deletions unzipper/modules/bot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ class Messages:
**Folder link:** {}
"""

SPLITTED_FILE_TXT = """
**Splitted archive detected!**
`The bot detected this file as a splitted archive, please follow correct steps to continue the process!`
"""

AFTER_OK_DL_TXT = """
Expand Down
26 changes: 20 additions & 6 deletions unzipper/modules/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from unzipper.helpers_nexa.database.upload_mode import set_upload_mode
from unzipper.helpers_nexa.unzip_help import (TimeFormatter, humanbytes,
progress_for_pyrogram)
from unzipper.helpers_nexa.database.split_arc import add_split_arc_user

from .backup import CloudBackup
from .commands import https_url_regex
Expand Down Expand Up @@ -46,7 +47,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):

elif query.data == "upmodhelp":
await query.edit_message_text(text=Messages.UPMODE_HELP, reply_markup=Buttons.HELP_MENU_BTN)

elif query.data == "backuphelp":
await query.edit_message_text(text=Messages.BACKUP_HELP, reply_markup=Buttons.HELP_MENU_BTN)

Expand All @@ -55,7 +56,6 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):

elif query.data == "aboutcallback":
await query.edit_message_text(text=Messages.ABOUT_TXT, reply_markup=Buttons.ME_GOIN_HOME, disable_web_page_preview=True)

elif query.data.startswith("set_mode"):
user_id = query.from_user.id
mode = query.data.split("|")[1]
Expand All @@ -70,6 +70,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
splitted_data = query.data.split("|")

try:
arc_name = ""
if splitted_data[1] == "url":
url = r_message.text
# Double check
Expand All @@ -91,9 +92,9 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
# Send logs
await unzip_bot.send_message(chat_id=Config.LOGS_CHANNEL, text=Messages.LOG_TXT.format(user_id, url, u_file_size))
s_time = time()
archive = f"{download_path}/archive_from_{user_id}{os.path.splitext(url)[1]}"
arc_name = f"{download_path}/archive_from_{user_id}{os.path.splitext(url)[1]}"
await answer_query(query, f"**Trying to download!** \n\n**Url:** `{url}` \n\n`This may take a while, Go and grab a coffee ☕️!`", unzip_client=unzip_bot)
await download(url, archive)
await download(url, arc_name)
e_time = time()
else:
return await query.message.edit("**Sorry I can't download that URL 🥺!**")
Expand All @@ -107,17 +108,30 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
log_msg = await r_message.forward(chat_id=Config.LOGS_CHANNEL)
await log_msg.reply(Messages.LOG_TXT.format(user_id, r_message.document.file_name, humanbytes(r_message.document.file_size)))
s_time = time()
arc_name = f"{download_path}/archive_from_{user_id}{os.path.splitext(r_message.document.file_name)[1]}"
archive = await r_message.download(
file_name=f"{download_path}/archive_from_{user_id}{os.path.splitext(r_message.document.file_name)[1]}",
file_name=arc_name,
progress=progress_for_pyrogram, progress_args=(
"**Trying to Download!** \n", query.message, s_time)
)
e_time = time()
else:
await answer_query(query, "Can't Find Details! Please contact support group!", answer_only=True, unzip_client=unzip_bot)
return await answer_query(query, "Can't Find Details! Please contact support group!", answer_only=True, unzip_client=unzip_bot)

await answer_query(query, Messages.AFTER_OK_DL_TXT.format(TimeFormatter(round(e_time-s_time) * 1000)), unzip_client=unzip_bot)

# Checks if the archive is a splitted one
arc_ext = os.path.splitext(arc_name)[1]
if arc_ext.replace(".", "").isnumeric():
password = ""
if splitted_data[2] == "with_pass":
password = (await unzip_bot.ask(chat_id=query.message.chat.id, text="**Please send me the password 🔑:**")).text
await answer_query(query, Messages.SPLITTED_FILE_TXT)
narc = f"splitted_archive_from_{user_id}{arc_ext}"
os.rename(arc_name, narc)
await add_split_arc_user(user_id, narc, password)
return

if splitted_data[2] == "with_pass":
password = await unzip_bot.ask(chat_id=query.message.chat.id, text="**Please send me the password 🔑:**")
ext_s_time = time()
Expand Down
73 changes: 64 additions & 9 deletions unzipper/modules/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@
import psutil
import asyncio

from time import time
from config import Config
from pyrogram.types import Message
from pyrogram import Client, filters
from pyrogram.errors import FloodWait
from pyrogram.types import Message
from unzipper.helpers_nexa.database.users import (add_banned_user, # Banned Users db
check_user, get_users_list,
count_users, count_banned_users,
del_user, del_banned_user)
from unzipper.helpers_nexa.database.thumbnail import save_thumbnail, get_thumbnail, del_thumbnail
from unzipper.helpers_nexa.database.split_arc import (del_split_arc_user,
get_split_arc_user)
from unzipper.helpers_nexa.database.thumbnail import (del_thumbnail,
get_thumbnail,
save_thumbnail)
from unzipper.helpers_nexa.database.upload_mode import get_upload_mode
from unzipper.helpers_nexa.unzip_help import humanbytes
from unzipper.helpers_nexa.database.users import (check_user, add_banned_user,
count_banned_users,
count_users, del_banned_user,
del_user, get_users_list)
from unzipper.helpers_nexa.unzip_help import (TimeFormatter, humanbytes,
progress_for_pyrogram)
from .ext_script.ext_helper import extr_files, get_files, make_keyboard
from .bot_data import Buttons, Messages


# Regex for http/https urls
https_url_regex = ("((http|https)://)(www.)?" +
"[a-zA-Z0-9@:%._\\+~#?&//=]" +
"{2,256}\\.[a-z]" +
"{2,6}\\b([-a-zA-Z0-9@:%" +
"._\\+~#?&//=]*)")

# Function to check user status (is banned or not)


# Function to check user status (is banned or not)
@Client.on_message(filters.private)
async def _(_, message: Message):
await check_user(message)
Expand All @@ -47,11 +54,31 @@ async def clean_ma_files(_, message: Message):
@Client.on_message(filters.incoming & filters.private & filters.regex(https_url_regex) | filters.document)
async def extract_dis_archive(_, message: Message):
unzip_msg = await message.reply("`Processing ⚙️...`", reply_to_message_id=message.id)
if not message.document:
return await unzip_msg.edit("`Is this even an archive 🤨?")
# Due to https://t.me/Nexa_bots/38823
if not message.from_user:
return await unzip_msg.edit("`Ayo, you ain't a user 🤨?")
user_id = message.from_user.id
download_path = f"{Config.DOWNLOAD_LOCATION}/{user_id}"

# Splitted files
is_spl, lfn, ps = await get_split_arc_user(user_id)
if is_spl:
await unzip_msg.edit(f"`Since you sent me {lfn}, I have to do some file merge stuff!`")
arc_name = f"{os.path.splitext(lfn)[0]}{os.path.splitext(message.document.file_name)[1]}"
if os.path.isfile(arc_name):
return await unzip_msg.edit("`Dawg, I already have this file!`")
s_time = time()
await message.download(
file_name=arc_name,
progress=progress_for_pyrogram, progress_args=(
"**Trying to Download!** \n", unzip_msg, s_time)
)
e_time = time()
await unzip_msg.edit(Messages.AFTER_OK_DL_TXT.format(TimeFormatter(round(e_time-s_time) * 1000)))
return

if os.path.isdir(download_path):
return await unzip_msg.edit("`Already one process is going on, Don't spam you idiot 😑!` \n\nWanna Clear You Files from my server? Then just send **/clean** command!")
if message.text and (re.match(https_url_regex, message.text)):
Expand All @@ -62,6 +89,34 @@ async def extract_dis_archive(_, message: Message):
await unzip_msg.edit("`Hold up! What Should I Extract 😳?`")


@Client.on_message(filters.private & filters.command("done"))
async def extracted_dis_spl_archive(_, message: Message):
spl_umsg = await message.reply("`Processing ⚙️...`", reply_to_message_id=message.id)
# User checks
if not message.from_user:
return await spl_umsg.edit("`Ayo, you ain't a user 🤨?")
user_id = message.from_user.id
# Retrive data from database
is_spl, lfn, ps = await get_split_arc_user(user_id)
ext_path = f"{Config.DOWNLOAD_LOCATION}/{user_id}/extracted"
arc_path = os.path.dirname(lfn)
# Path checks
if not is_spl:
return await spl_umsg.edit("`Bruh, why are you sending this command 🤔?`")
if not os.path.exists(arc_path):
return await spl_umsg.edit("`Sorry, It looks like your files have been removed from the server 😔!`")
# Remove user record from the database
await del_split_arc_user(user_id)
# Extract the archive
s_time = time()
await extr_files(ext_path, arc_path, ps, True)
e_time = time()
await spl_umsg.edit(Messages.EXT_OK_TXT.format(TimeFormatter(round(e_time-s_time) * 1000)))
paths = await get_files(ext_path)
i_e_btns = await make_keyboard(paths, user_id, message.chat.id)
await spl_umsg.edit("`Select Files to Upload!`", reply_markup=i_e_btns)


# Backup stuff
@Client.on_message(filters.private & filters.command("backup"))
async def do_backup_files(_, message: Message):
Expand Down
7 changes: 4 additions & 3 deletions unzipper/modules/ext_script/ext_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def __run_cmds_unzipper(ar):


# Extract with 7z
async def _extract_with_7z_helper(path, archive_path, password=None):
async def _extract_with_7z_helper(path, archive_path, password=None, splitted=False):
if password:
command = f"7z x -o{path} -p{password} {archive_path} -y"
else:
command = f"7z x -o{path} {archive_path} -y"
command += " -tsplit" if splitted else ""
return await run_cmds_on_cr(__run_cmds_unzipper, cmd=command)


Expand All @@ -32,14 +33,14 @@ async def _extract_with_zstd(path, archive_path):


# Main function to extract files
async def extr_files(path, archive_path, password=None):
async def extr_files(path, archive_path, password=None, splitted=False):
file_path = os.path.splitext(archive_path)[1]
if file_path == ".zst":
os.mkdir(path)
ex = await _extract_with_zstd(path, archive_path)
return ex
else:
ex = await _extract_with_7z_helper(path, archive_path, password)
ex = await _extract_with_7z_helper(path, archive_path, password, splitted)
return ex


Expand Down

0 comments on commit 9e206c8

Please sign in to comment.