aboutsummaryrefslogtreecommitdiff
path: root/bin/my_tagger
blob: f09b031278f5fe27069c8f85ec9b80a92fa03a24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)