-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreedb.py
322 lines (257 loc) · 8.95 KB
/
freedb.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/local/env python
# Copyright (c) 2006 Øyvind Skaar, Jon Anders Skorpen
import telnetlib
import re
# Kone
# Module for communicating with freedb serverse
# CDDB should work equally well, if the server adr. is
# hardwired, but is complitly untested
class Freedb:
def __init__(self, parrent, client, version, username, hostname):
self.connection = None # Telnet objekt
self.preferred_server = "freedb.freedb.org" # Fallback, should be set to another string by find_best_server()
self.username = username
self.hostname = hostname
self.client = client
self.version = version
self.debug = parrent.debug
def find_best_server(self):
"""
NOT WORKING, TODO
Tries to select the best freedb server by getting a list from the
official freedb.freedb.org and "pinging" them one by one
This feature of the freedb server seems to be broke atm, makes this func useless
(see http://www.freedb.org/en/forum/read.php?1,16 )
"""
sites = []
# Connect
status = self.connect("freedb.freedb.org") # hard-wired official server
if status:
# Something went wrong .. this is bad
print("Could not connect: %s" % status)
return None
# get the sites list
self.connection.write("sites\n")
for l in self.connection.read_until("\n.", 20).split("\n"): # A dot markes the end of output from the server
# We only want the servers (and lines) that supports the cddb protocol
print l
if "CDDBP" in l:
sites.append(l)
self.disconnect()
# find the "best" one
# return answer, or None for error
def discid(self, nr_of_tracks, tableofcontents):
"""
Compute the discid from the TOC (availible through the tableofcontents dict.
"""
def freedb_sum(n):
ret = 0
while (n > 0):
ret = ret + (n % 10)
n = n / 10
return ret
i = 0
t = 0
n = 0
while (i < nr_of_tracks):
tmp = tableofcontents[i]['addr']
n = n + freedb_sum((tmp['min']*60)+tmp['sec'])
i = i+1;
tmp1 = tableofcontents[nr_of_tracks]['addr']
tmp2 = tableofcontents[0]['addr']
t = ((tmp1['min']*60)+tmp1['sec']) - ((tmp2['min']*60)+tmp2['sec'])
return "%08x" % ((n % 0xff) << 24 | t << 8 | nr_of_tracks )
def discid_remote(self, nr_of_tracks, offset_list, total_len):
"""
Asks the server to compute the discid (instead of doing this ourself)
returns this id as a string
"""
status = self.connect()
if status:
print ("could not connect to remote server: %s" % status )
return None
offsets = ""
for o in offset_list: offsets += str(o) + " ";
q = "discid %d %s %d\n" % (nr_of_tracks, offsets, total_len)
self.connection.write(q)
self.connection.write("help help\n") # to get a reply thats acutally parsable
if self.debug: print " discid_remote() sendt:\n\"%s\"" % q
a = self.connection.read_until("\n.", 5)
match = re.search(r"200\s*Disc\s*ID\s*is\s*(\w+)", a)
if not match:
print "Could not get DiscID in discid(), server returned:\n\t %s" % a
return None
discid = match.group(1)
return discid
def query(self, discid, nr_of_tracks, offset_list, total_len):
"""
Ask the server for matches,
can be zero, one or more
this function _must_ be called before the other cddb function read()
"""
status = self.connect()
if status:
print ("could not connect to remote server: %s" % status )
return None
# Get a list of matches
# the first line is either:
# 200 categ discid dtitle
# or: 211 close matches found
# or: 210 Found exact matches, list follows (until terminating marker)
# or: Another code
#
# If code is 210 or 211 the next lines (until .) are the matches, on the form:
# categ discid dtitle
# Codes:
# 200 Found exact match
# 210 Found exact matches, list follows (until terminating marker)
# 211 Found inexact matches, list follows (until terminating marker)
# 202 No match found
# 403 Database entry is corrupt
# 409 No handshake
offsets = ""
for o in offset_list: offsets += str(o) + " "; # Make a string of the list of frame offsets
# expected answers (index used to understand the response)
expected = [
"200.*$", # exact match
"202", # no match
"403", # db entry corrupt
"409", # no handshake (HELLO)
"\n\." ] # 210 & 211. They require more output
q = "CDDB QUERY %s %d %s%d\n" % (discid, nr_of_tracks, offsets, total_len)
self.connection.write(q)
(index, match, text) = self.connection.expect(expected, 10)
# DEBUG INFO
# print "match: "
# print text
# print index
# print expected
if not match:
# No reg. exp. match, this is an error and does not mean
# the cd was not found
print "Did not understand CDDB QUERY response, got %s" % text
print "\n and sendt: %s" % q
return None
# Parse reply
text = text.strip()
cds = []
# code cat discid disctile (eg Artist / Album)
r = r"(\d*)\s*(\w+)\s*([a-z0-9]+)\s*(.*)"
if index is 0:
m = re.search(r, text)
cds.append( m.group(2,3,4) )
elif index is 1:
# Do _something_ indecating that the cd was not found in the db
pass
elif index is 2:
print "Server said the db entry is corrupt (403):\n\t%s" % text
return None
elif index is 3:
print "Server said \"No handshake\" (409):\n\t%s" % text
return None
elif index is 4:
for line in text.split("\n")[1:-1]: # dont need the 1st or the last line
m = None
m = re.search(r, line)
if not m:
print " Error parsing in query(), got:"
print "\"%s\"" % line
cds.append( m.group(2,3,4) )
return cds
def read(self, cat, discid):
"""
Get the cd-data from the db
query() _must_ be called befor this function,
even if we already have the needed info: cat & discid
(this is required by the server, failing to do so may result in wrong data !!)
"""
self.connection.write("CDDB READ %s %s\n" % (cat, discid) )
# expected answers (index used to understand the response)
expected = [
"\n401", # Specified CDDB entry not found.
"\n402", # Server error.
"\n403", # Database entry is corrupt.
"\n409", # No handshake.
"\n\." # (210) OK, CDDB database entry follows (until terminating marker)
]
(index, match, text) = self.connection.expect(expected, 10)
if index is 0:
# Do _something_ indecating that the cd was not found in the db
print "CD was not found in the db"
if self.debug: print text
return None
elif index is 1:
print "FreeDB server error (402) in read()"
return None
elif index is 2:
print "Server said the db entry is corrupt (403):\n\t%s" % text
return None
elif index is 3:
print "Server said \"No handshake\" (409):\n\t%s" % text
return None
elif index is 4:
# Ok, output follows
# make a list
list = text.split("\n")
list = [ x for x in list if x and x[0] is not "#" ]
dtitle = [ x[7:] for x in list if "DTITLE" in x ][0]
(artist, album) = re.search(r"(.*)\s/\s(.*)", dtitle).group(1,2)
(artist, album) = (artist.strip(), album.strip() ) # Strip newline (if any)
year = [ x[6:].strip() for x in list if "DYEAR" in x ][0]
genre = [ x[7:].strip() for x in list if "DGENRE" in x ][0]
titles = [ re.search(r"(\w+\d+=)(.*)", x).group(2).strip() for x in list if "TTITLE" in x ]
return (artist, album, year, genre, titles)
def connect(self, adr=None):
"""
(Re)Connect to a freedb server, return an message on error (or _at least_ != 0)
"""
port = 8880
if not adr:
adr = self.preferred_server
if self.connection:
# already connected, disconnect() (could be wrong server, or dead connection)
# TODO: Implement a check to stop reconnecting if not necessary
self.disconnect()
self.connection = telnetlib.Telnet(adr, port)
# test connection
self.connection.write("\n")
self.connection.write("proto 6\n") # set protocol level 6
a = self.connection.read_until("now: 6", 10)
if not "OK" in a:
return "Connection not working properly in connect()"
# say hi
self.connection.write("CDDB HELLO %s %s %s %s\n" % (self.username, self.hostname, self.client, self.version) )
a = self.connection.read_until(self.version + ".", 5)
if not a:
print "server did not respond to our polite hello.. "
return None
def disconnect(self):
"""
Disconnect from whatever server we are connected to and remove the pointer Telnet obj.
"""
if self.connection:
self.connection.close()
self.connection = None
if __name__ == "__main__":
# run some tests
# fake a parrent
import kone
parrent = kone.Kone("kone", "0.1", "/dev/acd0", 1, "os", "gunda.odots.org")
l = ["150", "20825", "47257", "68720", "93935", "113557", "133887", "153737", "163640", "181920", "197050" ]
id = "8e0b6f0b"
nr = 11
len = 2929
l = ['150', '26282', '51500', '76850', '101592', '121407', '143785', '154072', '178470', '209117', '237547', '237968', '257870', '275813']
nr = 14
len = 3834
id = parrent.fdb.discid_remote(nr, l, len)
print id
print "d40ef80e"
print parrent.debug
cds = parrent.fdb.query(id, nr, l, len )
(artist, album, year, genre, titles) = parrent.fdb.read(cds[0][0], cds[0][1])
print artist
print album
print year
print genre
print titles