1992-12-14 09:16:55 -04:00
|
|
|
# This file implements a class which forms an interface to the .cddb
|
|
|
|
# directory that is maintained by SGI's cdman program.
|
|
|
|
#
|
|
|
|
# Usage is as follows:
|
|
|
|
#
|
|
|
|
# import readcd
|
1993-11-08 11:10:43 -04:00
|
|
|
# r = readcd.Readcd()
|
|
|
|
# c = Cddb(r.gettrackinfo())
|
1992-12-14 09:16:55 -04:00
|
|
|
#
|
|
|
|
# Now you can use c.artist, c.title and c.track[trackno] (where trackno
|
|
|
|
# starts at 1). When the CD is not recognized, all values will be the empty
|
|
|
|
# string.
|
|
|
|
# It is also possible to set the above mentioned variables to new values.
|
|
|
|
# You can then use c.write() to write out the changed values to the
|
|
|
|
# .cdplayerrc file.
|
|
|
|
|
1994-09-06 13:19:03 -03:00
|
|
|
import string, posix, os
|
1992-12-14 09:16:55 -04:00
|
|
|
|
1993-09-27 09:36:01 -03:00
|
|
|
_cddbrc = '.cddb'
|
1992-12-14 09:16:55 -04:00
|
|
|
_DB_ID_NTRACKS = 5
|
|
|
|
_dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
|
|
|
|
def _dbid(v):
|
|
|
|
if v >= len(_dbid_map):
|
|
|
|
return string.zfill(v, 2)
|
|
|
|
else:
|
|
|
|
return _dbid_map[v]
|
|
|
|
|
1993-09-27 09:36:01 -03:00
|
|
|
def tochash(toc):
|
|
|
|
if type(toc) == type(''):
|
|
|
|
tracklist = []
|
|
|
|
for i in range(2, len(toc), 4):
|
|
|
|
tracklist.append((None,
|
2001-02-09 12:45:10 -04:00
|
|
|
(int(toc[i:i+2]),
|
|
|
|
int(toc[i+2:i+4]))))
|
1993-09-27 09:36:01 -03:00
|
|
|
else:
|
|
|
|
tracklist = toc
|
|
|
|
ntracks = len(tracklist)
|
|
|
|
hash = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
|
|
|
|
if ntracks <= _DB_ID_NTRACKS:
|
|
|
|
nidtracks = ntracks
|
|
|
|
else:
|
|
|
|
nidtracks = _DB_ID_NTRACKS - 1
|
|
|
|
min = 0
|
|
|
|
sec = 0
|
|
|
|
for track in tracklist:
|
|
|
|
start, length = track
|
|
|
|
min = min + length[0]
|
|
|
|
sec = sec + length[1]
|
|
|
|
min = min + sec / 60
|
|
|
|
sec = sec % 60
|
|
|
|
hash = hash + _dbid(min) + _dbid(sec)
|
|
|
|
for i in range(nidtracks):
|
|
|
|
start, length = tracklist[i]
|
|
|
|
hash = hash + _dbid(length[0]) + _dbid(length[1])
|
|
|
|
return hash
|
|
|
|
|
1993-05-24 11:16:22 -03:00
|
|
|
class Cddb:
|
1993-11-08 11:10:43 -04:00
|
|
|
def __init__(self, tracklist):
|
1994-09-06 13:19:03 -03:00
|
|
|
if os.environ.has_key('CDDB_PATH'):
|
|
|
|
path = os.environ['CDDB_PATH']
|
2001-02-09 12:45:10 -04:00
|
|
|
cddb_path = path.split(',')
|
1993-09-27 09:36:01 -03:00
|
|
|
else:
|
1994-09-06 13:19:03 -03:00
|
|
|
home = os.environ['HOME']
|
1993-09-27 09:36:01 -03:00
|
|
|
cddb_path = [home + '/' + _cddbrc]
|
1994-09-06 13:19:03 -03:00
|
|
|
|
|
|
|
self._get_id(tracklist)
|
|
|
|
|
1993-09-27 09:36:01 -03:00
|
|
|
for dir in cddb_path:
|
|
|
|
file = dir + '/' + self.id + '.rdb'
|
|
|
|
try:
|
|
|
|
f = open(file, 'r')
|
|
|
|
self.file = file
|
|
|
|
break
|
|
|
|
except IOError:
|
|
|
|
pass
|
2001-02-09 12:45:10 -04:00
|
|
|
ntracks = int(self.id[:2], 16)
|
1994-09-06 13:19:03 -03:00
|
|
|
self.artist = ''
|
|
|
|
self.title = ''
|
|
|
|
self.track = [None] + [''] * ntracks
|
1995-02-01 10:23:37 -04:00
|
|
|
self.trackartist = [None] + [''] * ntracks
|
|
|
|
self.notes = []
|
1993-09-27 09:36:01 -03:00
|
|
|
if not hasattr(self, 'file'):
|
1993-11-08 11:10:43 -04:00
|
|
|
return
|
1997-10-22 18:00:49 -03:00
|
|
|
import re
|
|
|
|
reg = re.compile(r'^([^.]*)\.([^:]*):[\t ]+(.*)')
|
1992-12-14 09:16:55 -04:00
|
|
|
while 1:
|
|
|
|
line = f.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
1997-10-22 18:00:49 -03:00
|
|
|
match = reg.match(line)
|
|
|
|
if not match:
|
1992-12-14 09:16:55 -04:00
|
|
|
print 'syntax error in ' + file
|
|
|
|
continue
|
1997-10-22 18:00:49 -03:00
|
|
|
name1, name2, value = match.group(1, 2, 3)
|
1992-12-14 09:16:55 -04:00
|
|
|
if name1 == 'album':
|
|
|
|
if name2 == 'artist':
|
|
|
|
self.artist = value
|
|
|
|
elif name2 == 'title':
|
|
|
|
self.title = value
|
|
|
|
elif name2 == 'toc':
|
1994-09-06 13:19:03 -03:00
|
|
|
if not self.toc:
|
|
|
|
self.toc = value
|
1992-12-14 09:16:55 -04:00
|
|
|
if self.toc != value:
|
|
|
|
print 'toc\'s don\'t match'
|
1995-02-01 10:23:37 -04:00
|
|
|
elif name2 == 'notes':
|
|
|
|
self.notes.append(value)
|
1992-12-14 09:16:55 -04:00
|
|
|
elif name1[:5] == 'track':
|
1993-03-29 06:49:59 -04:00
|
|
|
try:
|
2001-02-09 12:45:10 -04:00
|
|
|
trackno = int(name1[5:])
|
1993-03-29 06:49:59 -04:00
|
|
|
except strings.atoi_error:
|
|
|
|
print 'syntax error in ' + file
|
|
|
|
continue
|
|
|
|
if trackno > ntracks:
|
2004-02-12 13:35:32 -04:00
|
|
|
print 'track number %r in file %r out of range' % (trackno, file)
|
1993-03-29 06:49:59 -04:00
|
|
|
continue
|
1995-02-01 10:23:37 -04:00
|
|
|
if name2 == 'title':
|
|
|
|
self.track[trackno] = value
|
|
|
|
elif name2 == 'artist':
|
|
|
|
self.trackartist[trackno] = value
|
1992-12-14 09:16:55 -04:00
|
|
|
f.close()
|
1994-08-01 08:34:53 -03:00
|
|
|
for i in range(2, len(self.track)):
|
|
|
|
track = self.track[i]
|
|
|
|
# if track title starts with `,', use initial part
|
|
|
|
# of previous track's title
|
1994-09-06 13:19:03 -03:00
|
|
|
if track and track[0] == ',':
|
1994-08-01 08:34:53 -03:00
|
|
|
try:
|
2001-02-09 12:45:10 -04:00
|
|
|
off = self.track[i - 1].index(',')
|
|
|
|
except ValueError:
|
1994-08-01 08:34:53 -03:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.track[i] = self.track[i-1][:off] \
|
|
|
|
+ track
|
1992-12-14 09:16:55 -04:00
|
|
|
|
1994-09-06 13:19:03 -03:00
|
|
|
def _get_id(self, tracklist):
|
|
|
|
# fill in self.id and self.toc.
|
|
|
|
# if the argument is a string ending in .rdb, the part
|
|
|
|
# upto the suffix is taken as the id.
|
|
|
|
if type(tracklist) == type(''):
|
|
|
|
if tracklist[-4:] == '.rdb':
|
|
|
|
self.id = tracklist[:-4]
|
|
|
|
self.toc = ''
|
|
|
|
return
|
|
|
|
t = []
|
|
|
|
for i in range(2, len(tracklist), 4):
|
|
|
|
t.append((None, \
|
2001-02-09 12:45:10 -04:00
|
|
|
(int(tracklist[i:i+2]), \
|
|
|
|
int(tracklist[i+2:i+4]))))
|
1994-09-06 13:19:03 -03:00
|
|
|
tracklist = t
|
|
|
|
ntracks = len(tracklist)
|
|
|
|
self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
|
|
|
|
if ntracks <= _DB_ID_NTRACKS:
|
|
|
|
nidtracks = ntracks
|
|
|
|
else:
|
|
|
|
nidtracks = _DB_ID_NTRACKS - 1
|
|
|
|
min = 0
|
|
|
|
sec = 0
|
|
|
|
for track in tracklist:
|
|
|
|
start, length = track
|
|
|
|
min = min + length[0]
|
|
|
|
sec = sec + length[1]
|
|
|
|
min = min + sec / 60
|
|
|
|
sec = sec % 60
|
|
|
|
self.id = self.id + _dbid(min) + _dbid(sec)
|
|
|
|
for i in range(nidtracks):
|
|
|
|
start, length = tracklist[i]
|
|
|
|
self.id = self.id + _dbid(length[0]) + _dbid(length[1])
|
|
|
|
self.toc = string.zfill(ntracks, 2)
|
|
|
|
for track in tracklist:
|
|
|
|
start, length = track
|
|
|
|
self.toc = self.toc + string.zfill(length[0], 2) + \
|
|
|
|
string.zfill(length[1], 2)
|
|
|
|
|
1992-12-14 09:16:55 -04:00
|
|
|
def write(self):
|
1993-11-08 11:10:43 -04:00
|
|
|
import posixpath
|
1994-09-06 13:19:03 -03:00
|
|
|
if os.environ.has_key('CDDB_WRITE_DIR'):
|
|
|
|
dir = os.environ['CDDB_WRITE_DIR']
|
1993-09-27 09:36:01 -03:00
|
|
|
else:
|
1994-09-06 13:19:03 -03:00
|
|
|
dir = os.environ['HOME'] + '/' + _cddbrc
|
1993-09-27 09:36:01 -03:00
|
|
|
file = dir + '/' + self.id + '.rdb'
|
1993-11-08 11:10:43 -04:00
|
|
|
if posixpath.exists(file):
|
|
|
|
# make backup copy
|
|
|
|
posix.rename(file, file + '~')
|
1992-12-14 09:16:55 -04:00
|
|
|
f = open(file, 'w')
|
|
|
|
f.write('album.title:\t' + self.title + '\n')
|
|
|
|
f.write('album.artist:\t' + self.artist + '\n')
|
|
|
|
f.write('album.toc:\t' + self.toc + '\n')
|
1995-02-01 10:23:37 -04:00
|
|
|
for note in self.notes:
|
|
|
|
f.write('album.notes:\t' + note + '\n')
|
1994-08-01 08:34:53 -03:00
|
|
|
prevpref = None
|
1992-12-14 09:16:55 -04:00
|
|
|
for i in range(1, len(self.track)):
|
1995-03-01 06:03:08 -04:00
|
|
|
if self.trackartist[i]:
|
2004-02-12 13:35:32 -04:00
|
|
|
f.write('track%r.artist:\t%s\n' % (i, self.trackartist[i]))
|
1994-08-01 08:34:53 -03:00
|
|
|
track = self.track[i]
|
|
|
|
try:
|
2001-02-09 12:45:10 -04:00
|
|
|
off = track.index(',')
|
|
|
|
except ValuError:
|
1994-08-01 08:34:53 -03:00
|
|
|
prevpref = None
|
|
|
|
else:
|
|
|
|
if prevpref and track[:off] == prevpref:
|
|
|
|
track = track[off:]
|
|
|
|
else:
|
|
|
|
prevpref = track[:off]
|
2004-02-12 13:35:32 -04:00
|
|
|
f.write('track%r.title:\t%s\n' % (i, track))
|
1992-12-14 09:16:55 -04:00
|
|
|
f.close()
|