#!/usr/bin/python # decrap.py # Title: CUE sheet and m3u de-crapifier # Author: Ryan Stonecipher-Fisher # email: Ryan.Stonecipher.Fisher@gmail.com # Input: Album directory containing one .cue and one .m3u (and optionally one .log) file # Output: Thorough, ready-to-use album description # Version # 0.1 - testing - 05/19/06 # import modules for functions import sys # handles passed arguments import os # determines if files exist import dircache # used for directory listing import re # used for regular expressions import math # used for floor() rounding function # read input if len(sys.argv) != 2: # ensures that only one argument was passed print "decrap: missing directory argument" # explain argument error print "Try 'decrap --help' for more information." # suggest usage help sys.exit() # quit program else: album_dir = sys.argv[1] # get the directory from the command # explain usage if --help is passed if album_dir == '--help': # check that passed value is --help print "Usage: decrap DIRECTORY" # print usage print "Prints useful output about DIRECTORY", # explain function print "for upload form on OiNK's." # explain function (continued) print "Directories must contain .cue and .m3u files." # show requirements print "Report bugs to ." # give contact sys.exit() # quit program # check for a valid directory if os.path.exists(album_dir): # check that the directory exists dir_files = dircache.listdir(album_dir+'/') # load the directory contents else: # if the directory doesn't exist print album_dir + " is not a valid directory." # explain error print "Try 'decrap --help' for more information." # suggest usage sys.exit() # quit program # find cue, m3u, and log file(s) - we're gonna assume there's just one of each cueways = ".cue|.Cue.|.cUe|.cuE|.CUe|.CuE|.cUE|.CUE" # every way to put .cue in the filesystem m3uways = ".m3u|.M3u|.m3U|.M3U" # every way to put .m3u in the filesystem logways = ".log|.Log|.lOg|.loG|.LOg|.LoG|.lOG|.LOG" # every way to put .log in the filesystem cuefile = [foo for foo in dir_files if re.search(cueways, foo)] # list of cue files m3ufile = [foo for foo in dir_files if re.search(m3uways, foo)] # list of m3u files logfile = [foo for foo in dir_files if re.search(logways, foo)] # find .log # cuesheet - reading if len(cuefile) == 0: # if no .cue was found print "FREAKOUT ERROR - NO CUE:" # show error print "decrap only works for directories with cue sheets." # suggest reason sys.exit() # quit program else: # if a .cue was found cue = open(album_dir + cuefile[0], 'r') # open the .cue file for reading rawcue = cue.read().decode('cp1252').encode('UTF8') # read the .cue file cue.close() # close the .cue file cuelines = rawcue.split("\r\n") # split the .cue file into lines # ignore cuesheet data tracks datalines = [foo for foo in cuelines if re.search("MODEx", foo)] # find data tracks if datalines != []: # if data tracks were found datastart = cuelines.index(datalines[0]) # get the index of the first data track trimmedcuelines = cuelines[0:(datastart-1)] # trim all subsequent tracks cuelines = trimmedcuelines # replace the original list # m3u - reading if len(m3ufile) == 0: # if no .m3u was found print "FREAKOUT ERROR - NO M3U:" # show error print "decrap only works for directories with m3u playlists." # suggest reason sys.exit() # quit program else: # if a .m3u was found m3u = open(album_dir + m3ufile[0], 'r') # open the .m3u file for reading rawm3u = m3u.read().decode('cp1252').encode('UTF8') # read the .m3u file m3u.close() # close the .m3u file m3ulines = rawm3u.split("\n") # split the .m3u file into lines # cuesheet - year cuedateline = [foo for foo in cuelines if re.search("REM DATE", foo)] # find lines with year if len(cuedateline)==0: # if the logs has no year year = '*' # red flag the year else: # if a date exists year = cuedateline[0].split(" ")[2] # extract the year # cuesheet - album title titles = [foo for foo in cuelines if re.search("TITLE", foo)] # find lines with titles a_title = titles[0].split("TITLE ")[1].split('"')[1] # extract album title # cuesheet - album artist artists = [foo for foo in cuelines if re.search("PERFORMER ", foo)] # find lines with performers a_artist = artists[0].split("PERFORMER ")[1].split('"')[1] # extract album artist # cuesheet - track titles t_title=[] # declare list for track titles x=1 # initialize counter while x minutes string with leading zero else: # if there are no hours minutes = str(minute) # -> minutes string if (second < 10) and ((hour != 0) or (minute != 0)): # -> if there are hours or minutes seconds = '0' + str(second) # ->-> seconds string with leading zero else: # if there are seconds only seconds = str(second) # -> shortest... album... ever... # output print '\n[pre]Artist: ' + a_artist + '\nAlbum: ' + a_title # preformatting, album artist and title if year != '*': # if a year was found print 'Year: ' + year # show album year # time output if hours != '0': # only display hours if present print 'Length: ' + hours + ':' + minutes + ':' + seconds + '[/pre]\n' # hours:minutes:seconds else: # if hours are not present if minutes != '0': # ->if minutes are present print 'Length: ' + minutes + ':' + seconds + '[/pre]\n' # ->->minutes:seconds else: # ->if minutes aren't present print 'Length: ' + seconds + '[/pre]\n' # ->->shortest... album... ever # track listing x = 0 # reset counter while xcue and m3u are extra files else: # if a log was found extra_files = 3 # ->cue and m3u and log are extra files # see if the directory has mp3 or APE files then assume FLAC if chosen types arent present # this could be expanded for other formats under the assumption that only one format was used if re.search(".mp3|.Mp3|.mP3|.MP3", foo): # test for moronic capitalization mp3s = [foo for foo in dir_files] # ->find mp3 files if len(mp3s) != 0: # ->if mp3 files were found format = 'mp3' # ->->format is mp3 elif re.search(".ape|.Ape|.aPe|.apE|.APe|.ApE|.aPE|.APE", foo): # test for moronic capitalization apes = [foo for foo in dir_files] # ->find ape files if len(apes) != 0: # ->if ape files were found format = 'ape' # ->->format is ape else: # not mp3 or ape format = 'FLAC' # ->format is FLAC (default) # print file description print '\n' + str(len(t_artist)+extra_files) + ' files:' # number of files present print str(len(t_artist)) + ' ' + format + ' files' # number and format of tracks print cuefile[0], # .cue filename print '- EAC "Multiple WAV Files With Gaps... (Noncompliant)" CUE sheet' # cue explanation if extra_files == 3: # if a .log exists print logfile[0] + ' - EAC log' # ->.log filename and explanation print m3ufile[0] + ' - playlist' # .m3u filename and explanation