Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 1 | # 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 |
| 7 | # r = readcd.Readcd() |
| 8 | # c = Cddb(r.gettrackinfo()) |
| 9 | # |
| 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 | |
| 17 | import string, posix, os |
| 18 | |
| 19 | _cddbrc = '.cddb' |
| 20 | _DB_ID_NTRACKS = 5 |
| 21 | _dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz' |
| 22 | def _dbid(v): |
| 23 | if v >= len(_dbid_map): |
| 24 | return string.zfill(v, 2) |
| 25 | else: |
| 26 | return _dbid_map[v] |
| 27 | |
| 28 | def tochash(toc): |
| 29 | if type(toc) == type(''): |
| 30 | tracklist = [] |
| 31 | for i in range(2, len(toc), 4): |
| 32 | tracklist.append((None, |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 33 | (int(toc[i:i+2]), |
| 34 | int(toc[i+2:i+4])))) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 35 | 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 | |
| 57 | class Cddb: |
| 58 | def __init__(self, tracklist): |
| 59 | if os.environ.has_key('CDDB_PATH'): |
| 60 | path = os.environ['CDDB_PATH'] |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 61 | cddb_path = path.split(',') |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 62 | else: |
| 63 | home = os.environ['HOME'] |
| 64 | cddb_path = [home + '/' + _cddbrc] |
| 65 | |
| 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. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 76 | ntracks = int(self.id[:2], 16) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 77 | self.artist = '' |
| 78 | self.title = '' |
| 79 | self.track = [None] + [''] * ntracks |
| 80 | self.trackartist = [None] + [''] * ntracks |
| 81 | self.notes = [] |
| 82 | if not hasattr(self, 'file'): |
| 83 | return |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 84 | import re |
| 85 | reg = re.compile(r'^([^.]*)\.([^:]*):[\t ]+(.*)') |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 86 | while 1: |
| 87 | line = f.readline() |
| 88 | if not line: |
| 89 | break |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 90 | match = reg.match(line) |
| 91 | if not match: |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 92 | print 'syntax error in ' + file |
| 93 | continue |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 94 | name1, name2, value = match.group(1, 2, 3) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 95 | 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' |
| 105 | elif name2 == 'notes': |
| 106 | self.notes.append(value) |
| 107 | elif name1[:5] == 'track': |
| 108 | try: |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 109 | trackno = int(name1[5:]) |
| 110 | except ValueError: |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 111 | print 'syntax error in ' + file |
| 112 | continue |
| 113 | if trackno > ntracks: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 114 | print 'track number %r in file %s out of range' % (trackno, file) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 115 | continue |
| 116 | if name2 == 'title': |
| 117 | self.track[trackno] = value |
| 118 | elif name2 == 'artist': |
| 119 | self.trackartist[trackno] = value |
| 120 | f.close() |
| 121 | for i in range(2, len(self.track)): |
| 122 | track = self.track[i] |
| 123 | # if track title starts with `,', use initial part |
| 124 | # of previous track's title |
| 125 | if track and track[0] == ',': |
| 126 | try: |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 127 | off = self.track[i - 1].index(',') |
| 128 | except ValueError: |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 129 | pass |
| 130 | else: |
| 131 | self.track[i] = self.track[i-1][:off] \ |
| 132 | + track |
| 133 | |
| 134 | def _get_id(self, tracklist): |
| 135 | # fill in self.id and self.toc. |
| 136 | # if the argument is a string ending in .rdb, the part |
| 137 | # upto the suffix is taken as the id. |
| 138 | if type(tracklist) == type(''): |
| 139 | if tracklist[-4:] == '.rdb': |
| 140 | self.id = tracklist[:-4] |
| 141 | self.toc = '' |
| 142 | return |
| 143 | t = [] |
| 144 | for i in range(2, len(tracklist), 4): |
| 145 | t.append((None, \ |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 146 | (int(tracklist[i:i+2]), \ |
| 147 | int(tracklist[i+2:i+4])))) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 148 | tracklist = t |
| 149 | ntracks = len(tracklist) |
| 150 | self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF) |
| 151 | if ntracks <= _DB_ID_NTRACKS: |
| 152 | nidtracks = ntracks |
| 153 | else: |
| 154 | nidtracks = _DB_ID_NTRACKS - 1 |
| 155 | min = 0 |
| 156 | sec = 0 |
| 157 | for track in tracklist: |
| 158 | start, length = track |
| 159 | min = min + length[0] |
| 160 | sec = sec + length[1] |
| 161 | min = min + sec / 60 |
| 162 | sec = sec % 60 |
| 163 | self.id = self.id + _dbid(min) + _dbid(sec) |
| 164 | for i in range(nidtracks): |
| 165 | start, length = tracklist[i] |
| 166 | self.id = self.id + _dbid(length[0]) + _dbid(length[1]) |
| 167 | self.toc = string.zfill(ntracks, 2) |
| 168 | for track in tracklist: |
| 169 | start, length = track |
| 170 | self.toc = self.toc + string.zfill(length[0], 2) + \ |
| 171 | string.zfill(length[1], 2) |
| 172 | |
| 173 | def write(self): |
| 174 | import posixpath |
| 175 | if os.environ.has_key('CDDB_WRITE_DIR'): |
| 176 | dir = os.environ['CDDB_WRITE_DIR'] |
| 177 | else: |
| 178 | dir = os.environ['HOME'] + '/' + _cddbrc |
| 179 | file = dir + '/' + self.id + '.rdb' |
| 180 | if posixpath.exists(file): |
| 181 | # make backup copy |
| 182 | posix.rename(file, file + '~') |
| 183 | f = open(file, 'w') |
| 184 | f.write('album.title:\t' + self.title + '\n') |
| 185 | f.write('album.artist:\t' + self.artist + '\n') |
| 186 | f.write('album.toc:\t' + self.toc + '\n') |
| 187 | for note in self.notes: |
| 188 | f.write('album.notes:\t' + note + '\n') |
| 189 | prevpref = None |
| 190 | for i in range(1, len(self.track)): |
| 191 | if self.trackartist[i]: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 192 | f.write('track%r.artist:\t%s\n' % (i, self.trackartist[i]) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 193 | track = self.track[i] |
| 194 | try: |
Eric S. Raymond | dcd3a87 | 2001-02-09 16:45:10 +0000 | [diff] [blame] | 195 | off = track.index(',') |
| 196 | except ValueError: |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 197 | prevpref = None |
| 198 | else: |
| 199 | if prevpref and track[:off] == prevpref: |
| 200 | track = track[off:] |
| 201 | else: |
| 202 | prevpref = track[:off] |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 203 | f.write('track%r.title:\t%s\n' % (i, track)) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 204 | f.close() |