~ubuntu-branches/ubuntu/vivid/chromaprint/vivid

« back to all changes in this revision

Viewing changes to tools/fillpuid.py

  • Committer: Package Import Robot
  • Author(s): Simon Chopin
  • Date: 2014-09-28 20:27:39 UTC
  • mfrom: (8.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140928202739-fn4slakstjtksz35
Tags: 1.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
import time
4
 
import sys
5
 
from optparse import OptionParser
6
 
import subprocess
7
 
import os.path
8
 
from xml.etree import ElementTree
9
 
 
10
 
 
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")
17
 
 
18
 
(options, args) = parser.parse_args()
19
 
if len(args) != 1:
20
 
    parser.error("no log file specified")
21
 
 
22
 
 
23
 
def read_log_file(input):
24
 
    group = {}
25
 
    for line in input:
26
 
        line = line.strip()
27
 
        if not line:
28
 
            if group:
29
 
                yield group
30
 
            group = {}
31
 
            continue
32
 
        name, value = line.split('=', 1)
33
 
        group[name] = value
34
 
    if group:
35
 
        yield group
36
 
 
37
 
 
38
 
def make_groups(input, size=10):
39
 
    group = []
40
 
    for entry in input:
41
 
        group.append(entry)
42
 
        if len(group) >= size:
43
 
            yield group
44
 
            group = []
45
 
    if group:
46
 
        yield group
47
 
 
48
 
 
49
 
 
50
 
def write_log_file(entry):
51
 
    for row in entry.iteritems():
52
 
        print '%s=%s' % row
53
 
    print
54
 
 
55
 
 
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)
61
 
    puids = {}
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']
65
 
    for entry in entries:
66
 
        path = os.path.normpath(entry['FILENAME'])
67
 
        if path in puids:
68
 
            entry['PUID'] = puids[path]
69
 
 
70
 
 
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])
73
 
    for entry in entries:
74
 
        print >>sys.stderr, entry['FILENAME']
75
 
        write_log_file(entry)
76