X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tagger.py;h=be22447719f0953762987ff3e7601d4f6478db8f;hb=5ac1cd86715517f7fe135eb692833d468c245a3b;hp=bda52afdac2e6388fcc7bcd924c0df058c067609;hpb=d1028f9bf3b801a24c130e75de6b035cd03851d0;p=cmccabe-bin diff --git a/tagger.py b/tagger.py index bda52af..be22447 100755 --- a/tagger.py +++ b/tagger.py @@ -44,6 +44,7 @@ import sys dry_run = False verbose = False self_test = False +audiobook = False # globals total_albums = 0 @@ -65,16 +66,30 @@ def find_companion_script(target): print "ERROR: can't find id3v2_wrapper.sh: " + str(e) sys.exit(1) +# Verifies that a given program is installed. +def verify_program_installed(prog): + try: + proc = subprocess.Popen(prog, stdout=subprocess.PIPE) + line = proc.stdout.readline() + return True + except Exception, e: + print "failed to execute " + str(prog) + return False + # Regular expressions for parsing file names-- # which is, after all, what this program is all about music_file_re = re.compile(".*\.mp3$") music_file_name_re = re.compile(".*/" + "(?P[^/]*)/" + - "(?P[0123456789][0123456789]) - " + + "(?P[0123456789][0123456789]*) - " + "(?P[^/]*)" + "\.[a-zA-Z0123456789]*$") +audiobook_file_name_re = re.compile(".*/" + + "(?P[^/]*)/" + + "(?P[0123456789][0123456789]*)"); + dir_name_re = re.compile("(.*/)?" + "(?P[0-9A-Za-z _.\-]*?) - " + "(?P[0-9A-Za-z _(),'.\-\+]*)" + @@ -219,13 +234,18 @@ class MusicFile(object): self.track_number = int(track_number) def from_filename(filename): - match = music_file_name_re.match(filename) + if (audiobook): + match = audiobook_file_name_re.match(filename) + track_name = "" + else: + match = music_file_name_re.match(filename) + track_name = match.group('track_name') if (not match): raise MusicFileErr("can't parse music file name \"" + filename + "\"") album = Album.from_dirname(match.group('dir_name')) return MusicFile(filename, album, - match.group('track_name'), + track_name, match.group('track_number')) from_filename = staticmethod(from_filename) @@ -252,6 +272,11 @@ class MusicFile(object): break # CODE +## Make sure that id3v2 is installed +if not verify_program_installed(["id3v2", "--version"]): + print "You must install the id3v2 program to run this script." + sys.exit(1) + ## Find id3v2_wrapper.sh id3v2_wrapper = find_companion_script('id3v2_wrapper.sh') @@ -264,12 +289,13 @@ def Usage(): print "-h: this help message" print "-d: dry-run mode" print "-s: self-test" + print "-A: audiobook mode" print "dirs: directories to search for albums." print "This program skips dirs with \"[LL]\" in the name." sys.exit(1) try: - optlist, dirs = getopt.getopt(sys.argv[1:], ':dhi:sv') + optlist, dirs = getopt.getopt(sys.argv[1:], ':dhi:svA') except getopt.GetoptError: Usage() @@ -282,6 +308,8 @@ for opt in optlist: verbose = True if opt[0] == '-s': self_test = True + if opt[0] == '-A': + audiobook = True if (self_test): run_self_test()