Last active
April 13, 2020 11:48
-
-
Save keizie/6198425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os, re, logging | |
from glob import glob | |
from pipes import quote | |
from shutil import rmtree | |
SOURCES = ('/mnt/1.5T/,movie/','/mnt/1.5T/,tv/',) | |
LOGFILE = '/home/keizie/.brag/autounrar.log' | |
EXECUTE_RAR = 'unrar' | |
EXECUTE_ZIP = '7za' | |
LOGLEVEL = os.environ.get('LOGLEVEL', 'WARNING').upper() | |
logging.basicConfig(level=LOGLEVEL) | |
def get_basename(filename): | |
basename = os.path.splitext(filename)[0] | |
if basename.find('.part')>-1: | |
return os.path.splitext(basename)[0] | |
return basename | |
def is_multi(filename): | |
if filename.find('.part1.')>-1 or filename.find('.part01.')>-1 or filename.find('.part001.')>-1: | |
return 1 # True | |
elif filename.find('.rar')>-1 and os.path.exists(filename.replace('.rar','.r00')): | |
return 2 # True | |
elif filename.find('.zip')>-1 and os.path.exists(filename.replace('.zip','.z01')): | |
return 3 # True | |
return 0 # False | |
def is_complete(filename): | |
# TODO: check .sfv | |
basename = get_basename(filename) | |
if filename.find('.zip')>-1: | |
# As multipart zip set .zip extension for last file, | |
# if *.zip file has other .z01 siglings, it is complete | |
return True | |
elif filename.find('.part1.')>-1: | |
pattern = re.compile(re.escape(basename)+'\.part[1-9]\.rar$') | |
elif filename.find('.part01.')>-1: | |
#pattern = re.compile(re.escape(basename)+'\.part(?!01)\d{2}\.rar$') | |
pattern = re.compile(re.escape(basename)+'\.part\d{2}\.rar$') | |
elif filename.find('.part001.')>-1: | |
#pattern = re.compile(re.escape(basename)+'\.part(?!001)\d{3}\.rar$') | |
pattern = re.compile(re.escape(basename)+'\.part\d{3}\.rar$') | |
else: | |
pattern = re.compile(re.escape(basename)+'\.[rs][0-9][0-9]$') # s00 comes next to r99. | |
logging.info('base: %s' % basename) | |
multis = [f for f in os.listdir('.') if pattern.match(f)] | |
if len(multis) == 0: | |
return False | |
multis.sort() | |
lastfile = multis[-1] # largest number | |
if lastfile.find('.part')>-1: | |
numpos = os.path.splitext(lastfile)[0].rfind('.part')+5 | |
length = int(lastfile[numpos:-4]) | |
if filename.find('.exe')>-1: | |
length = length -1 # first file not in count | |
else: | |
length = int(lastfile[-2:])+1 # .rar file not in count but begins with 00 | |
if lastfile[-3]=='s': | |
length = length + 100 | |
is_full = (len(multis) == length) | |
is_diffsize = (os.path.getsize(filename) != os.path.getsize(lastfile)) | |
logging.info('file: %s' % filename) | |
logging.info('full: %s' % is_full) | |
logging.info('diff: %s' % is_diffsize) | |
return (is_full & is_diffsize) | |
def do_postproc(basename, dest): | |
logging.info('postprocessing...') | |
pattern = re.compile(re.escape(basename)+'(.*\.r.+|.*\.exe|.*\.z.+)') | |
multis = [f for f in os.listdir('.') if pattern.match(f)] | |
for f in multis: | |
os.remove(f) | |
single = [f for f in os.listdir(dest)] | |
if len(single) == 1: | |
os.rename(dest+single[0], './'+single[0]) | |
os.rmdir(dest) | |
def do_uncomp(filename): | |
basename = get_basename(filename) | |
dest = basename + os.sep | |
if filename.find('.rar')>-1: | |
command = '%s x %s %s' % (EXECUTE_RAR, quote(filename), quote(dest)) | |
logging.debug(command) | |
ret = os.system(command) | |
elif filename.find('.zip')>-1: | |
command = '%s x -o%s %s' % (EXECUTE_ZIP, quote(dest), quote(filename)) | |
logging.debug(command) | |
ret = os.system(command) | |
if ret == 0: | |
logging.info('succeed') | |
do_postproc(basename, dest) | |
else: | |
logging.warning('error: %s' % ret) | |
rmtree(dest) | |
if __name__ == '__main__': | |
from tendo import singleton | |
me = singleton.SingleInstance() | |
logging.basicConfig(filename=LOGFILE, level=logging.DEBUG) | |
for src in SOURCES: | |
os.chdir(src) | |
first_archives = [f for f in glob('*.[rez][axi][rep]') if is_multi(f)] | |
for f in first_archives: | |
if is_complete(f): | |
do_uncomp(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zip 파일도 멀티압축이 있어서, 7za 명령이 압축해제를 지원하는 걸 확인하고 zip 멀티압축 해제도 추가함