forked from brewsterkahle/archivecd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheac_log_to_musicbrainz_discid.py
75 lines (63 loc) · 2.15 KB
/
eac_log_to_musicbrainz_discid.py
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
#!/usr/bin/python
"""
Reads EAC log, generates musicbrainz disc TOC listing for use as discid.
Opens browser with discid query on musicbrainz.org.
Warning: may work wrong for discs having data tracks. May generate wrong results on other non-standard cases.
"""
import re
import sys
import webbrowser
class NotSupportedTOCError(Exception):
pass
def filter_toc_entries(lines):
"""
Take iterator of lines, return iterator of toc entries
"""
while True:
line = lines.next()
# to allow internationalized EAC output where column headings
# may differ
if re.match(r""" \s*
.+\s+ \| (?#track)
\s+.+\s+ \| (?#start)
\s+.+\s+ \| (?#length)
\s+.+\s+ \| (?#start sec)
\s+.+\s*$ (?#end sec)
""", line, re.X):
lines.next()
break
while True:
line = lines.next()
m = re.match(r"""
^\s*
(?P<num>\d+)
\s*\|\s*
(?P<start_time>[0-9:.]+)
\s*\|\s*
(?P<length_time>[0-9:.]+)
\s*\|\s*
(?P<start_sector>\d+)
\s*\|\s*
(?P<end_sector>\d+)
\s*$
""", line, re.X)
if not m:
break
yield m.groupdict()
def calculate_mb_toc_numbers(eac_entries):
"""
Take iterator of toc entries, return list of numbers for musicbrainz disc id
"""
eac = list(eac_entries)
num_tracks = len(eac)
tracknums = [int(e['num']) for e in eac]
if range(1,num_tracks+1) != tracknums:
raise NotSupportedTOCError("Non-standard track number sequence: %s", tracknums)
#print "Number of Eac entries:", len(eac)
leadout_offset = int(eac[-1]['end_sector']) + 150 + 1
offsets = [(int(x['start_sector']) + 150) for x in eac]
return [1, num_tracks, leadout_offset] + offsets
#f = open(sys.argv[1])
#conv = (line.decode(sys.argv[2]) for line in f)
#mb_toc_urlpart = "%20".join(str(x) for x in calculate_mb_toc_numbers(filter_toc_entries(f)))
#webbrowser.open("http://musicbrainz.org/bare/cdlookup.html?toc=%s" % mb_toc_urlpart)