9 c.execute('SELECT metadata.SortStr(%s)', [name])
10 return c.fetchone()[0]
12 print "No connection to DB"
15 def SortAuthorName(name):
18 c.execute('SELECT metadata.SortAuthor(%s)', [name])
19 return c.fetchone()[0]
21 print "No connection to DB"
24 def GetOrCreateAuthor(name):
27 c.execute('SELECT metadata.GetOrCreateAuthor(%s)', [name])
28 return c.fetchone()[0]
30 print "No connection to DB"
33 def GetOrCreateLang(name):
36 c.execute('SELECT metadata.GetOrCreateLang(%s)', [name])
37 return c.fetchone()[0]
39 print "No connection to DB"
42 def GetOrCreatePublisher(name):
45 c.execute('SELECT metadata.GetOrCreatePublisher(%s)', [name])
46 return c.fetchone()[0]
48 print "No connection to DB"
51 def GetOrCreateSeries(name):
54 c.execute('SELECT metadata.GetOrCreateSeries(%s)', [name])
55 return c.fetchone()[0]
57 print "No connection to DB"
60 def GetOrCreateTag(name):
63 c.execute('SELECT metadata.GetOrCreateTag(%s)', [name])
64 return c.fetchone()[0]
66 print "No connection to DB"
69 def CreateBook(title,pubdate,series_index,isbn):
72 c.execute('SELECT metadata.CreateBook(%s,%s,%s,%s)', [title,pubdate,series_index,isbn])
73 return c.fetchone()[0]
75 print "No connection to DB"
78 def LinkBookToAuthors(book_id,author_ids):
81 for author_id in author_ids:
82 c.execute('INSERT INTO metadata.books_authors_link(book,author) VALUES (%s,%s)', [book_id,author_id])
84 print "No connection to DB"
87 def LinkBookToLangs(book_id,lang_ids):
91 for lang_id in lang_ids:
93 c.execute('INSERT INTO metadata.books_languages_link(book,lang_code,item_order) VALUES (%s,%s,%s)', [book_id,lang_id,io])
95 print "No connection to DB"
98 def LinkBookToPublishers(book_id,pub_id):
100 c = database.cursor()
101 c.execute('INSERT INTO metadata.books_publishers_link(book,publisher) VALUES (%s,%s)', [book_id,pub_id])
103 print "No connection to DB"
106 def LinkBookToSeries(book_id,ser_id):
108 c = database.cursor()
109 c.execute('INSERT INTO metadata.books_series_link(book,series) VALUES (%s,%s)', [book_id,ser_id])
111 print "No connection to DB"
114 def LinkBookToTags(book_id,tag_ids):
116 c = database.cursor()
117 for tag_id in tag_ids:
118 c.execute('INSERT INTO metadata.books_tags_link(book,tag) VALUES (%s,%s)', [book_id,tag_id])
120 print "No connection to DB"
123 def SetPath(book_id,path,dataname,filesize,cover):
125 c = database.cursor()
126 c.execute('UPDATE metadata.books SET path=%s, has_cover=%s WHERE id=%s', [path,cover,book_id])
127 c.execute('INSERT INTO metadata.data(book,format,uncompressed_size,name) values (%s,%s,%s,%s)',[book_id,'FB2',filesize,dataname])
129 print "No connection to DB"
132 def StoreComment(book_id,comment):
134 c = database.cursor()
135 c.execute('INSERT INTO metadata.comments(book,text) values (%s,%s)',[book_id,comment])
137 print "No connection to DB"
140 def PathByID(book_id):
142 c = database.cursor()
143 c.execute('SELECT path FROM metadata.books WHERE id=%s',[book_id])
144 return c.fetchone()[0]
146 print "No connection to DB"
149 def DataByID(book_id,format):
151 c = database.cursor()
152 c.execute('SELECT name FROM metadata.data WHERE book=%s and format=%s',[book_id,format])
153 return c.fetchone()[0]+'.'+format.lower()
155 print "No connection to DB"
158 def DelBook(book_id):
160 c = database.cursor()
161 c.execute('DELETE FROM metadata.books WHERE id=%s',[book_id])
163 print "No connection to DB"
166 def ChangeBookFormat(book_id,old_format,new_format):
168 c = database.cursor()
169 c.execute('UPDATE metadata.data SET format=%s WHERE book=%s and format=%s',[new_format,book_id,old_format])
171 print "No connection to DB"
174 def TestArchive(name):
176 c = database.cursor()
177 c.execute('SELECT count(*) from metadata.processed_archives WHERE filename=%s',[name])
178 return c.fetchone()[0]
180 print "No connection to DB"
183 def MarkArchive(name):
185 c = database.cursor()
186 c.execute('insert into metadata.processed_archives(filename) values (%s)',[name])
188 print "No connection to DB"
191 def ListByFormat(format,limit=100):
193 c = database.cursor()
194 c.execute('SELECT DISTINCT book FROM metadata.data WHERE format=%s ORDER BY book LIMIT 0,%s',[format,limit])
197 print "No connection to DB"
200 def ListDups(limit=100):
202 c = database.cursor()
203 c.execute('SELECT b.title,l.author,max(b.id) id FROM metadata.books b,metadata.books_authors_link l where b.id=l.book group by b.title,l.author having count(*)>%s',[limit])
206 print "No connection to DB"
209 def ListByTitleAndAuthor(title,author,id=0):
211 c = database.cursor()
212 c.execute('SELECT b.id FROM metadata.books b,metadata.books_authors_link l where b.id=l.book and b.title=%s and l.author=%s and b.id<>%s',[title,author,id])
215 print "No connection to DB"
223 print "No connection to DB"
230 print "No connection to DB"
236 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
237 cfg.readfp(open('/etc/openlib.conf'))
238 dbhost = cfg.get("mysql","host")
239 dbuser = cfg.get("mysql","user")
240 dbpasswd = cfg.get("mysql","passwd")
241 file_root = cfg.get("storage","files")
242 tmp_files = cfg.get("storage","temp")
243 failed_files = cfg.get("storage","failed")
244 upload_files = cfg.get("storage","upload")
248 print "Error reading configuration file"
253 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,use_unicode=True)
254 database.set_character_set('utf8')
255 c = database.cursor()
256 c.execute('SET NAMES utf8;')
260 print "Error connecting database"