Created
September 16, 2012 04:13
-
-
Save legoktm/3730970 to your computer and use it in GitHub Desktop.
extract.py
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/env python | |
""" | |
(C) 2012, Legoktm under the MIT License | |
""" | |
import os | |
import subprocess | |
root = os.getcwd() | |
def run(): | |
root = os.getcwd() | |
dirs = search_dirs(root) | |
for dir in dirs: | |
do_folder(dir) | |
def search_dirs(root): | |
if not os.path.isdir(root): | |
return [] | |
all = os.listdir(root) | |
work_on_these = list() | |
for dir in all: | |
full = os.path.join(root+'/'+dir) | |
if is_last_dir(full): | |
work_on_these.append(full) | |
else: | |
work_on_these.extend(search_dirs(full)) | |
return work_on_these | |
def is_last_dir(full): | |
if not os.path.isdir(full): | |
return | |
files = os.listdir(full) | |
last = False | |
for file in files: | |
if '.rar' in file: | |
last = True | |
break | |
return last | |
def find_rar_file(rars): | |
for rar in rars: | |
if 'part01.rar' in rar: | |
return rar | |
good = list() | |
for rar in rars: | |
if rar.strip().endswith('.rar'): | |
good.append(rar) | |
if len(good) == 1: | |
return good[0] | |
def prefix_zero(value): | |
if value < 10: | |
return '0%s' % value | |
return str(value) | |
def rar_tuple(): | |
l = ['.rar'] | |
count = 0 | |
while count < 100: | |
l.append('.r%s' % prefix_zero(count)) | |
count += 1 | |
return tuple(l) | |
def do_folder(dir): | |
os.chdir(dir) | |
rars = os.listdir(dir) | |
p1 = None | |
found = None | |
p1 = find_rar_file(rars) | |
for item in rars: | |
if item.endswith(('.avi','.mkv','.mp4')): | |
found = item | |
break | |
if not p1: | |
return | |
if not found: | |
commands = ['unrar','e',p1] | |
subprocess.call(commands) | |
if found: | |
for item in rars: | |
if item.endswith(rar_tuple()): | |
print 'Deleting '+item | |
os.unlink(item) | |
return | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment