Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active March 7, 2023 06:11
Show Gist options
  • Save Chubek/5e45ae19565980cb3d7dd6e452c9357a to your computer and use it in GitHub Desktop.
Save Chubek/5e45ae19565980cb3d7dd6e452c9357a to your computer and use it in GitHub Desktop.
Delphia Sccripts: scripts I have made for my brother
####################################################################################
# RAR file extractor by Chubak Bidpaa #
# Usage `python3 raract.py <folder with the files> [-part]` [-pass=<password>] #
# Released under GPLv3 License #
####################################################################################
import sys
import os
from pathlib import Path
if os.system("unrar > /dev/null 2>&1") != 0:
sys.command("sudo apt-get install unrar > /dev/null 2>&1")
def extract_rar_file(path_pass: tuple[Path, str]) -> None:
path, password = path_pass
out_dir = path.parent.joinpath(
Path("extracted_" + str(abs(hash(path)))[:4]))
out_dir.mkdir(exist_ok=True)
cmd = f"unrar e {path} {out_dir}> /dev/null 2>&1"
if password:
cmd += " -p" + password
res = os.system(cmd)
if res == 0:
print("\033[1m" + str(path) + "\033[0m extracted to \033[1m" +
str(out_dir) + "\033[0m")
else:
print("\033[1;31mError occured\033[0m with extracting " + str(path))
def list_rar_files(path: Path, part=False) -> list[Path]:
glob_str = "*.rar"
if part:
glob_str = "*.part1.rar"
globbed = path.glob(glob_str)
return list(globbed)
def error_out(message: str) -> None:
print("\033[1;31mError occurred!\033[0m")
print("\t", end="")
print(message)
exit(1)
def handle_args(args):
if 2 < len(args) < 1:
error_out(
"No folder passed! If folder name contains space, escape each spate with `\\`"
)
folder_path = args[0]
part = False
password = None
for arg in args:
if arg == "-part":
part = True
elif len(arg) >= 6:
if arg[:6] == "-pass=":
password = arg[6:]
folder = Path(folder_path)
if not folder.is_dir():
error_out(
"Passed path is not a folder or doesn't exist. If your path contains spaces, escape them with `\\`"
)
rarfiles = list_rar_files(folder, part=part)
if len(rarfiles) == 0:
error_out(
"No rar file found in folder! If you did not mean to, don't pass '-part'"
)
list(map(extract_rar_file, [(rf, password) for rf in rarfiles]))
if __name__ == "__main__":
handle_args(sys.argv[1:])
print("\033[1;33mOperation finished successfully\033[0m")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment