5
from optparse import OptionParser
8
from xml.etree import ElementTree
11
usage = "usage: %prog [options] logfile"
12
parser = OptionParser(usage=usage)
13
parser.add_option("-a", "--musicdns-apikey", dest="musicdns_api_key", metavar="KEY",
14
help="MusicDNS API key")
15
parser.add_option("-g", "--genpuid", dest="genpuid_path", metavar="PATH",
16
help="path to the GenPUID binary", default="genpuid")
18
(options, args) = parser.parse_args()
20
parser.error("no log file specified")
23
def read_log_file(input):
32
name, value = line.split('=', 1)
38
def make_groups(input, size=10):
42
if len(group) >= size:
50
def write_log_file(entry):
51
for row in entry.iteritems():
56
def call_genpuid(entries):
57
paths = [e['FILENAME'] for e in entries]
58
process = subprocess.Popen([options.genpuid_path, options.musicdns_api_key, '-xml'] + paths, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
59
out, err = process.communicate()
60
tree = ElementTree.fromstring('<?xml version="1.0" encoding="iso-8859-1"?>' + out)
62
for track in tree.findall("track"):
63
if 'puid' in track.attrib and 'file' in track.attrib:
64
puids[os.path.normpath(track.attrib['file']).encode('iso-8859-1')] = track.attrib['puid']
66
path = os.path.normpath(entry['FILENAME'])
68
entry['PUID'] = puids[path]
71
for entries in make_groups(read_log_file(open(args[0]) if args[0] != '-' else sys.stdin), 20):
72
call_genpuid([e for e in entries if 'MBID' not in e])
74
print >>sys.stderr, entry['FILENAME']