diff options
| author | Ryan Kavanagh <rak@debian.org> | 2013-07-25 08:34:21 -0400 | 
|---|---|---|
| committer | Ryan Kavanagh <rak@debian.org> | 2013-07-25 08:45:10 -0400 | 
| commit | ee35290abc58f6582550a08a497c9ae75a19cd91 (patch) | |
| tree | a4bc76027f47bb0089038167c705abe7c8b6f684 /bin | |
| parent | Added mailx-alias script which converts mutt aliases to mailx aliases (diff) | |
Added a python mutagen script for tagging my flacs
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/my_tagger | 90 | 
1 files changed, 90 insertions, 0 deletions
| diff --git a/bin/my_tagger b/bin/my_tagger new file mode 100755 index 0000000..f09b031 --- /dev/null +++ b/bin/my_tagger @@ -0,0 +1,90 @@ +#!/usr/bin/python + +import codecs +import re + +from mutagen import flac + +FILENAME='tracks' + +CONFIRM=True +DEBUG=False +SWAPNAME=True +SAVE=True +SPLIT=False + +if CONFIRM or DEBUG: +    import pprint +    # From +    # http://stackoverflow.com/questions/10883399/unable-to-encode-decode-pprint-output +    class PPUTF8(pprint.PrettyPrinter): +        def format(self, object, context, maxlevels, level): +            if isinstance(object, unicode): +                return (object.encode('utf8'), True, False) +            return pprint.PrettyPrinter.format(self, object, context, +                        maxlevels, level) + + +if __name__ == '__main__': +    if CONFIRM or DEBUG: +        pp = PPUTF8(indent=4) + +    with codecs.open(FILENAME, encoding='utf-8', mode='r') as f: +        lines = f.readlines() +    headers = lines[0][:-1].split('\t') + +    if SWAPNAME: +        first_last = re.compile('^(?P<last>[^,]+),\s+(?P<first>.+)$') + +    files = {} +    for i, file in enumerate(lines[1:]): +        # i starts counting at zero while we need it to start counting at 1 +        # since 0 is the header line +        i += 1 +        line = lines[i].replace('\n', '').split('\t') +        file = line[0] +        files[file] = {} +        for j, tag in enumerate(line[1:]): +            # j starts counting at 0, should be 1 +            j += 1 +            header = headers[j] +            files[file][header] = tag.strip() +            if header == 'ARTIST' or header == 'LYRICIST' or \ +                    header == 'ARRANGER': +                if SPLIT: +                    files[file][header] = [name.strip() for name in +                                                        tag.split(';')] +                else: +                    files[file][header] = tag.strip() +                if SWAPNAME: +                    for n, name in enumerate(files[file][header]): +                        res = first_last.match(name) +                        if res: +                            files[file][header][n] = res.group('first') + \ +                                    ' ' + res.group('last') + + +     +    if CONFIRM: +        print "Loaded data:\n" +        pp.pprint(files) + +    if CONFIRM: +        if raw_input("\nSave files? [Y/n]").lower()in ['', 'y']: +            SAVE = True +        else: +            SAVE = False + +    file = flac.FLAC() +    for name in files.keys(): +        file.load(name) +        # Delete tags +        file.delete() +        for key, val in files[name].iteritems(): +            file[key] = val +            if SAVE: +                file.save() + +        if DEBUG: +            print "\n\nData in flac file:", name +            pp.pprint(file) | 
