Monday, April 05, 2010

SimpleCode

very useful if one intends to share code

http://www.simplebits.com/cgi-bin/simplecode.pl?mode=process

Just for kicks

Quite frequently i found myself struggling with extracting files that i download from iptorrents.com . The download data is organized in multiple rar files which i hate to extract manually. Here is how it is typically arrange for TV series.

/blah/tv_showX_seasonY/

In folder ./
tv_showX_seasonY episodes are in multiple dirs of the form
tv_showX_seasonY_E1 to /tv_showX_seasonY_E24

In the episode folder, there are rar files typically starting with extension *.r00 .

With intentions of having fun this script was written which taken a path as argument [SeasonY path] and extracts all episodes under it that have rar files arranged in *.r00 format.

import getopt, sys, os, fnmatch, subprocess

def usage():
usage = """
-h --help Prints this
-p --path (PATH to the fir which needs to be unrar'ed)
-v verbose
"""
print usage

def unrar():
if os.path.exists(__path) == False:
print "Path doesn't exist: %s" % __path
sys.exit(1)
for dirs in os.listdir(__path):
if os.path.isdir(os.path.join(__path, dirs)) == True:
for files in os.listdir(os.path.join(__path, dirs)):
if fnmatch.fnmatch(files, '*.r00'):
myarg = os.path.join(__path, dirs)+ "/" +files
print myarg
try:
retcode = subprocess.Popen(["unrar", "x", myarg, __path])
if retcode <>>sys.stderr, "Child was terminated by signal", -retcode
else:
print >>sys.stderr, "Child returned", retcode
except OSError, e:
print >>sys.stderr, "Execution failed:", e


def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hp:v", ["help", "path="])
except getopt.GetoptError:
print str(err)
usage()
sys.exit(2)
global __path
verbose = False
for options, arg in opts:
if options == "-v":
verbose = True
elif options == "-h":
usage()
sys.exit()
elif options in ("-p", "--path"):
__path = arg
else:
assert False, "unhandled option"

__path = ""

if __name__ == "__main__":
main()
unrar()

This spawns multiple processes each unraring each one of the r00 [disclaimer it could slow down you system but then given the fact that your are reading this time is not of much importance to you]. Invoke it giving the season's dir path

python this_files_name.py path_to_season_folder
It will result in all avi files extracted to
path_to_season_folder. You can now delete [or keep] the rars

Its a diff thing that i could it do this way too :P

for loop in `find . -name *.r00`; do unrar e $loop; done

Like i said, just for kicks