Skip to content

Commit

Permalink
Patch for #14
Browse files Browse the repository at this point in the history
- Sort file path list in ascending order
- Removed nested loop used in get_files function
  • Loading branch information
Itz-fork committed Jun 18, 2022
1 parent 88581bf commit 7205108
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions unzipper/modules/ext_script/ext_helper.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
# Copyright (c) 2021 Itz-fork
# Don't kang this else your dad is gae
import subprocess
import os
import subprocess

from pykeyboard import InlineKeyboard
from pyrogram.types import InlineKeyboardButton

## Run commands in shell

# Run commands in shell
async def __run_cmds_unzipper(command):
ext_cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
ext_cmd = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
ext_out = ext_cmd.stdout.read()[:-1].decode("utf-8")
return ext_out

## Extract with 7z

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

##Extract with zstd (for .zst files)

# Extract with zstd (for .zst files)
async def _extract_with_zstd(path, archive_path):
command = f"zstd -f --output-dir-flat {path} -d {archive_path}"
return await __run_cmds_unzipper(command)


# Main function to extract files
async def extr_files(path, archive_path, password=None):
file_path = os.path.splitext(archive_path)[1]
Expand All @@ -36,13 +41,13 @@ async def extr_files(path, archive_path, password=None):
ex = await _extract_with_7z_helper(path, archive_path, password)
return ex


# Get files in directory as a list
async def get_files(path):
path_list = []
for r, d, f in os.walk(path):
for file in f:
path_list.append(os.path.join(r, file))
return path_list
path_list = [val for sublist in [[os.path.join(
i[0], j) for j in i[2]] for i in os.walk(path)] for val in sublist]
return sorted(path_list)


# Make keyboard
async def make_keyboard(paths, user_id, chat_id):
Expand All @@ -57,7 +62,8 @@ async def make_keyboard(paths, user_id, chat_id):
)
for file in paths:
data.append(
InlineKeyboardButton(f"{num} - {os.path.basename(file)}".encode("utf-8").decode("utf-8"), f"ext_f|{user_id}|{chat_id}|{num}")
InlineKeyboardButton(f"{num} - {os.path.basename(file)}".encode(
"utf-8").decode("utf-8"), f"ext_f|{user_id}|{chat_id}|{num}")
)
num += 1
i_kbd.add(*data)
Expand All @@ -79,4 +85,4 @@ async def make_keyboard(paths, user_id, chat_id):
# i_kbd.append(
# [InlineKeyboardButton("Cancel ❌", callback_data="cancel_dis")]
# )
# return i_kbd
# return i_kbd

0 comments on commit 7205108

Please sign in to comment.