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 print author.encode('utf-8')
70 author_ids.add(db.GetOrCreateAuthor(author.encode('utf-8')))
74 self.author='Неизвестный Автор (%s)' % (tag)
80 self.langs=meta_dict['languages']
84 for lang in meta_dict['languages']:
85 lang_ids.add(db.GetOrCreateLang(lang.encode('utf-8')))
91 self.publisher = meta_dict['publisher'].encode('utf-8')
92 pub_id=db.GetOrCreatePublisher(self.publisher)
97 title = meta_dict['book_title'].encode('utf-8')
99 title='Название неизвестно'
103 pub_date=meta_dict['pubdate']
106 self.pub_date=pub_date
109 isbn=meta_dict['isbn'].encode('utf-8')
115 self.series = meta_dict['series'].encode('utf-8')
116 ser_id=db.GetOrCreateSeries(meta_dict['series'])
120 self.series_idx = meta_dict['series_index']
121 ser_num=meta_dict['series_index'].split(',')[0]
128 tag_ids.add(db.GetOrCreateTag(tag))
133 self.cover=meta_dict['cover']
139 self.comments=meta_dict['comments']
140 if len(self.comments)>20000:
141 self.comments=self.comments[:20000]
142 self.comments=self.comments.encode('utf-8')
146 book_id = db.CreateBook(title,pub_date,ser_num,isbn)
147 self.book_id = book_id
149 db.LinkBookToAuthors(book_id,author_ids);
150 db.LinkBookToLangs(book_id,lang_ids);
152 db.LinkBookToPublishers(book_id,pub_id);
154 db.LinkBookToSeries(book_id,ser_id);
155 db.LinkBookToTags(book_id,tag_ids);
157 db.StoreComment(book_id,self.comments)
160 db.SetPath(self.book_id,self.path,self.dataname,self.size,self.has_cover);
163 def ProcessFile(filename):
165 size = os.path.getsize(filename)
166 stream = open(filename)
167 meta = fb2_meta.get_metadata(stream)
171 book = MetaData(meta.__dict__,size)
173 if book.state=="done":
175 new_file_path = db.file_root + book.path + '/' + book.dataname + '.fb2'
176 cover_path = db.file_root + book.path + '/cover.jpg'
177 new_dir_path = db.file_root + book.path
179 os.makedirs(new_dir_path,0755)
180 shutil.move(filename,new_file_path)
183 cover_path = new_dir_path + '/cover.jpg'
184 print "Book has cover, try to store to "+cover_path
185 coverfile = open(cover_path,'w')
186 coverfile.write(book.cover.decode('base64'))
190 print "Moved to "+new_dir_path
192 elif book.state=="trash":
194 print "Tags blacklisted, trashing"
199 shutil.move(filename,db.failed_files+os.path.basename(filename))
200 print "Moved to failed_files"
205 shutil.move(filename,db.failed_files+os.path.basename(filename))
206 print "Moved to failed_files"
209 def ProcessDir(dirname):
210 for file in os.listdir(dirname):
211 if file.endswith(".fb2"):
212 print "Processing "+file
213 ProcessFile(os.path.join(dirname,file))
216 path = os.path.join(db.file_root,db.PathByID(id))
218 for file in os.listdir(path):
219 os.remove(os.path.join(path,file))
224 def CompressBook(id):
227 datafile = os.path.join(db.file_root,path,db.DataByID(id,'FB2'))
228 datapath = datafile.replace("\"","\\\"")
229 datapath=datapath.replace("`","\`")
230 datapath=datapath.replace("$","\$")
231 zipfile = datapath + '.zip'
232 command = "zip --move --junk-paths \"%s\" \"%s\"" % (zipfile,datapath)
233 command = command.encode('utf-8')
235 if os.system(command)==0:
236 db.ChangeBookFormat(id,'FB2','FB2.ZIP')
239 def UnCompressBook(id):
242 datafile = os.path.join(db.file_root,path,db.DataByID(id,'FB2.ZIP'))
243 datapath = datafile.replace("\"","\\\"")
244 datapath=datapath.replace("`","\`")
245 datapath=datapath.replace("$","\$")
246 command = "unzip \"%s\" -d \"%s\"" % (datapath,os.path.join(db.file_root,path))
247 command = command.encode('utf-8')
249 if os.system(command)==0:
251 db.ChangeBookFormat(id,'FB2.ZIP','FB2')
254 def CompressAll(limit=100):
255 ids = db.ListByFormat('FB2',limit)
262 def CheckFiles(delete = 0):
263 ids = db.ListByFormat('FB2',1000000)
267 sys.stdout.write("\r%s"%(cnt))
268 datafile = os.path.join(db.file_root,db.PathByID(id[0]),db.DataByID(id[0],'FB2'))
269 if not os.path.exists(datafile):
270 print "\r File %s not found" % (datafile)
274 ids = db.ListByFormat('FB2.ZIP',1000000)
278 sys.stdout.write("\r%s"%(cnt))
279 datafile = os.path.join(db.file_root,db.PathByID(id[0]),db.DataByID(id[0],'FB2.ZIP'))
280 if not os.path.exists(datafile):
281 print "\r File %s not found" % (datafile)
286 def RemoveDups(limit = 100):
290 recs = db.ListDups(limit);
292 ids = db.ListByTitleAndAuthor(rec[0],rec[1],rec[2])
296 print "\r Deleting %s..." % (id)
300 print "Processing...\r"
301 ProcessDir(db.tmp_files)
304 if __name__ == "__main__":
307 sys.setdefaultencoding('utf-8')