2 # -*- coding: utf-8 -*-
13 def GetTagsMapping(db):
16 c.execute("select tag_mask,tag_result from metadata.tags_mapping")
17 mapping = c.fetchall()
20 result.append((re.compile(item[0]),item[1].encode('utf-8')))
26 if item[0].match(tag):
33 GetTagsMapping(db.database)
36 new_tags.add(Matches(item))
42 self.sort_title = db.SortName(self.title).replace(' ','_');
43 self.sort_author = db.SortAuthorName(self.author).replace(' ','_');
44 self.path = ("%s/%s/%s/%s/%s (%d)" % (self.sort_author[0],self.sort_author[0:2],self.sort_author[0:4],self.sort_author[0:32],self.sort_title[0:64],self.book_id))
45 self.dataname = (self.title.decode('utf-8')[0:64]+' '+self.author.decode('utf-8')[0:32]).replace('/','')
47 def __init__(self,meta_dict,size):
51 tags=NewTags(meta_dict['tags'])
64 self.author=meta_dict['authors'][0].encode('utf-8')
65 self.authors = meta_dict['authors']
68 for author in self.authors:
69 author_ids.add(db.GetOrCreateAuthor(author.encode('utf-8')))
73 self.author='Неизвестный Автор (%s)' % (tag)
79 self.langs=meta_dict['languages']
83 for lang in meta_dict['languages']:
84 lang_ids.add(db.GetOrCreateLang(lang.encode('utf-8')))
90 self.publisher = meta_dict['publisher'].encode('utf-8')
91 pub_id=db.GetOrCreatePublisher(self.publisher)
96 title = meta_dict['book_title'].encode('utf-8')
98 title='Название неизвестно'
102 pub_date=meta_dict['pubdate']
105 self.pub_date=pub_date
108 isbn=meta_dict['isbn'].encode('utf-8')
114 self.series = meta_dict['series'].encode('utf-8')
115 ser_id=db.GetOrCreateSeries(meta_dict['series'])
119 self.series_idx = meta_dict['series_index']
120 ser_num=meta_dict['series_index']
127 tag_ids.add(db.GetOrCreateTag(tag))
132 self.cover=meta_dict['cover']
138 self.comments=meta_dict['comments'].encode('utf-8')
142 book_id = db.CreateBook(title,pub_date,ser_num,isbn)
143 self.book_id = book_id
145 db.LinkBookToAuthors(book_id,author_ids);
146 db.LinkBookToLangs(book_id,lang_ids);
148 db.LinkBookToPublishers(book_id,pub_id);
150 db.LinkBookToSeries(book_id,ser_id);
151 db.LinkBookToTags(book_id,tag_ids);
153 db.StoreComment(book_id,self.comments)
156 db.SetPath(self.book_id,self.path,self.dataname,self.size,self.has_cover);
159 def ProcessFile(filename):
161 size = os.path.getsize(filename)
162 stream = open(filename)
163 meta = fb2_meta.get_metadata(stream)
167 book = MetaData(meta.__dict__,size)
169 if book.state=="done":
171 new_file_path = db.file_root + book.path + '/' + book.dataname + '.fb2'
172 cover_path = db.file_root + book.path + '/cover.jpg'
173 new_dir_path = db.file_root + book.path
175 os.makedirs(new_dir_path,0755)
176 shutil.move(filename,new_file_path)
179 cover_path = new_dir_path + '/cover.jpg'
180 print "Book has cover, try to store to "+cover_path
181 coverfile = open(cover_path,'w')
182 coverfile.write(book.cover.decode('base64'))
186 print "Moved to "+new_dir_path
188 elif book.state=="trash":
190 print "Tags blacklisted, trashing"
195 shutil.move(filename,db.failed_files+os.path.basename(filename))
196 print "Moved to failed_files"
201 shutil.move(filename,db.failed_files+os.path.basename(filename))
202 print "Moved to failed_files"
205 def ProcessDir(dirname):
206 for file in os.listdir(dirname):
207 if file.endswith(".fb2"):
208 print "Processing "+file
209 ProcessFile(os.path.join(dirname,file))
212 path = os.path.join(db.file_root,db.PathByID(id))
214 for file in os.listdir(path):
215 os.remove(os.path.join(path,file))
220 def CompressBook(id):
223 datafile = os.path.join(db.file_root,path,db.DataByID(id,'FB2'))
224 datapath = datafile.replace("\"","\\\"")
225 datapath=datapath.replace("`","\`")
226 datapath=datapath.replace("$","\$")
227 zipfile = datapath + '.zip'
228 command = "zip --move --junk-paths \"%s\" \"%s\"" % (zipfile,datapath)
229 command = command.encode('utf-8')
231 if os.system(command)==0:
232 db.ChangeBookFormat(id,'FB2','FB2.ZIP')
235 def UnCompressBook(id):
238 datafile = os.path.join(db.file_root,path,db.DataByID(id,'FB2.ZIP'))
239 datapath = datafile.replace("\"","\\\"")
240 datapath=datapath.replace("`","\`")
241 datapath=datapath.replace("$","\$")
242 command = "unzip \"%s\" -d \"%s\"" % (datapath,os.path.join(db.file_root,path))
243 command = command.encode('utf-8')
245 if os.system(command)==0:
247 db.ChangeBookFormat(id,'FB2.ZIP','FB2')
250 def CompressAll(limit=100):
251 ids = db.ListByFormat('FB2',limit)
258 def CheckFiles(delete = 0):
259 ids = db.ListByFormat('FB2',300000)
263 sys.stdout.write("\r%s"%(cnt))
264 datafile = os.path.join(db.file_root,db.PathByID(id[0]),db.DataByID(id[0],'FB2'))
265 if not os.path.exists(datafile):
266 print "\r File %s not found" % (datafile)
270 ids = db.ListByFormat('FB2.ZIP',300000)
274 sys.stdout.write("\r%s"%(cnt))
275 datafile = os.path.join(db.file_root,db.PathByID(id[0]),db.DataByID(id[0],'FB2.ZIP'))
276 if not os.path.exists(datafile):
277 print "\r File %s not found" % (datafile)
282 def RemoveDups(limit = 100):
286 recs = db.ListDups(limit);
288 ids = db.ListByTitleAndAuthor(rec[0],rec[1],rec[2])
292 print "\r Deleting %s..." % (id)
296 print "Processing...\r"
297 ProcessDir(db.tmp_files)
300 if __name__ == "__main__":
303 sys.setdefaultencoding('utf-8')