blob: e43aea63847d537eff0311975403fdc814f0ccb7 [file] [log] [blame]
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +00001# This file implements a class which forms an interface to the .cddb
2# directory that is maintained by SGI's cdman program.
3#
4# Usage is as follows:
5#
6# import readcd
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +00007# r = readcd.Readcd()
8# c = Cddb(r.gettrackinfo())
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +00009#
10# Now you can use c.artist, c.title and c.track[trackno] (where trackno
11# starts at 1). When the CD is not recognized, all values will be the empty
12# string.
13# It is also possible to set the above mentioned variables to new values.
14# You can then use c.write() to write out the changed values to the
15# .cdplayerrc file.
16
Sjoerd Mullendere202c381994-09-06 16:19:03 +000017import string, posix, os
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +000018
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +000019_cddbrc = '.cddb'
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +000020_DB_ID_NTRACKS = 5
21_dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
22def _dbid(v):
23 if v >= len(_dbid_map):
24 return string.zfill(v, 2)
25 else:
26 return _dbid_map[v]
27
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +000028def tochash(toc):
29 if type(toc) == type(''):
30 tracklist = []
31 for i in range(2, len(toc), 4):
32 tracklist.append((None,
Eric S. Raymonddcd3a872001-02-09 16:45:10 +000033 (int(toc[i:i+2]),
34 int(toc[i+2:i+4]))))
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +000035 else:
36 tracklist = toc
37 ntracks = len(tracklist)
38 hash = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
39 if ntracks <= _DB_ID_NTRACKS:
40 nidtracks = ntracks
41 else:
42 nidtracks = _DB_ID_NTRACKS - 1
43 min = 0
44 sec = 0
45 for track in tracklist:
46 start, length = track
47 min = min + length[0]
48 sec = sec + length[1]
49 min = min + sec / 60
50 sec = sec % 60
51 hash = hash + _dbid(min) + _dbid(sec)
52 for i in range(nidtracks):
53 start, length = tracklist[i]
54 hash = hash + _dbid(length[0]) + _dbid(length[1])
55 return hash
56
Guido van Rossumd3166071993-05-24 14:16:22 +000057class Cddb:
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +000058 def __init__(self, tracklist):
Sjoerd Mullendere202c381994-09-06 16:19:03 +000059 if os.environ.has_key('CDDB_PATH'):
60 path = os.environ['CDDB_PATH']
Eric S. Raymonddcd3a872001-02-09 16:45:10 +000061 cddb_path = path.split(',')
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +000062 else:
Sjoerd Mullendere202c381994-09-06 16:19:03 +000063 home = os.environ['HOME']
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +000064 cddb_path = [home + '/' + _cddbrc]
Sjoerd Mullendere202c381994-09-06 16:19:03 +000065
66 self._get_id(tracklist)
67
68 for dir in cddb_path:
69 file = dir + '/' + self.id + '.rdb'
70 try:
71 f = open(file, 'r')
72 self.file = file
73 break
74 except IOError:
75 pass
Eric S. Raymonddcd3a872001-02-09 16:45:10 +000076 ntracks = int(self.id[:2], 16)
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +000077 self.artist = ''
78 self.title = ''
Sjoerd Mullendere202c381994-09-06 16:19:03 +000079 self.track = [None] + [''] * ntracks
Sjoerd Mullender3d697ce1995-02-01 14:23:37 +000080 self.trackartist = [None] + [''] * ntracks
81 self.notes = []
Sjoerd Mullendere202c381994-09-06 16:19:03 +000082 if not hasattr(self, 'file'):
83 return
Guido van Rossum9694fca1997-10-22 21:00:49 +000084 import re
85 reg = re.compile(r'^([^.]*)\.([^:]*):[\t ]+(.*)')
Sjoerd Mullendere202c381994-09-06 16:19:03 +000086 while 1:
87 line = f.readline()
88 if not line:
89 break
Guido van Rossum9694fca1997-10-22 21:00:49 +000090 match = reg.match(line)
91 if not match:
Sjoerd Mullendere202c381994-09-06 16:19:03 +000092 print 'syntax error in ' + file
93 continue
Guido van Rossum9694fca1997-10-22 21:00:49 +000094 name1, name2, value = match.group(1, 2, 3)
Sjoerd Mullendere202c381994-09-06 16:19:03 +000095 if name1 == 'album':
96 if name2 == 'artist':
97 self.artist = value
98 elif name2 == 'title':
99 self.title = value
100 elif name2 == 'toc':
101 if not self.toc:
102 self.toc = value
103 if self.toc != value:
104 print 'toc\'s don\'t match'
Sjoerd Mullender3d697ce1995-02-01 14:23:37 +0000105 elif name2 == 'notes':
106 self.notes.append(value)
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000107 elif name1[:5] == 'track':
108 try:
Eric S. Raymonddcd3a872001-02-09 16:45:10 +0000109 trackno = int(name1[5:])
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000110 except strings.atoi_error:
111 print 'syntax error in ' + file
112 continue
113 if trackno > ntracks:
114 print 'track number ' + `trackno` + \
115 ' in file ' + file + \
116 ' out of range'
117 continue
Sjoerd Mullender3d697ce1995-02-01 14:23:37 +0000118 if name2 == 'title':
119 self.track[trackno] = value
120 elif name2 == 'artist':
121 self.trackartist[trackno] = value
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000122 f.close()
123 for i in range(2, len(self.track)):
124 track = self.track[i]
125 # if track title starts with `,', use initial part
126 # of previous track's title
127 if track and track[0] == ',':
128 try:
Eric S. Raymonddcd3a872001-02-09 16:45:10 +0000129 off = self.track[i - 1].index(',')
130 except ValueError:
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000131 pass
132 else:
133 self.track[i] = self.track[i-1][:off] \
134 + track
135
136 def _get_id(self, tracklist):
137 # fill in self.id and self.toc.
138 # if the argument is a string ending in .rdb, the part
139 # upto the suffix is taken as the id.
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000140 if type(tracklist) == type(''):
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000141 if tracklist[-4:] == '.rdb':
142 self.id = tracklist[:-4]
143 self.toc = ''
144 return
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000145 t = []
146 for i in range(2, len(tracklist), 4):
147 t.append((None, \
Eric S. Raymonddcd3a872001-02-09 16:45:10 +0000148 (int(tracklist[i:i+2]), \
149 int(tracklist[i+2:i+4]))))
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000150 tracklist = t
151 ntracks = len(tracklist)
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000152 self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
153 if ntracks <= _DB_ID_NTRACKS:
154 nidtracks = ntracks
155 else:
156 nidtracks = _DB_ID_NTRACKS - 1
157 min = 0
158 sec = 0
159 for track in tracklist:
160 start, length = track
161 min = min + length[0]
162 sec = sec + length[1]
163 min = min + sec / 60
164 sec = sec % 60
165 self.id = self.id + _dbid(min) + _dbid(sec)
166 for i in range(nidtracks):
167 start, length = tracklist[i]
168 self.id = self.id + _dbid(length[0]) + _dbid(length[1])
169 self.toc = string.zfill(ntracks, 2)
170 for track in tracklist:
171 start, length = track
172 self.toc = self.toc + string.zfill(length[0], 2) + \
173 string.zfill(length[1], 2)
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000174
175 def write(self):
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +0000176 import posixpath
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000177 if os.environ.has_key('CDDB_WRITE_DIR'):
178 dir = os.environ['CDDB_WRITE_DIR']
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +0000179 else:
Sjoerd Mullendere202c381994-09-06 16:19:03 +0000180 dir = os.environ['HOME'] + '/' + _cddbrc
Sjoerd Mullenderc9d73d11993-09-27 12:36:01 +0000181 file = dir + '/' + self.id + '.rdb'
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +0000182 if posixpath.exists(file):
183 # make backup copy
184 posix.rename(file, file + '~')
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000185 f = open(file, 'w')
186 f.write('album.title:\t' + self.title + '\n')
187 f.write('album.artist:\t' + self.artist + '\n')
188 f.write('album.toc:\t' + self.toc + '\n')
Sjoerd Mullender3d697ce1995-02-01 14:23:37 +0000189 for note in self.notes:
190 f.write('album.notes:\t' + note + '\n')
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191 prevpref = None
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000192 for i in range(1, len(self.track)):
Sjoerd Mullenderd9b6b5c1995-03-01 10:03:08 +0000193 if self.trackartist[i]:
194 f.write('track'+`i`+'.artist:\t'+self.trackartist[i]+'\n')
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195 track = self.track[i]
196 try:
Eric S. Raymonddcd3a872001-02-09 16:45:10 +0000197 off = track.index(',')
198 except ValuError:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199 prevpref = None
200 else:
201 if prevpref and track[:off] == prevpref:
202 track = track[off:]
203 else:
204 prevpref = track[:off]
205 f.write('track' + `i` + '.title:\t' + track + '\n')
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +0000206 f.close()