diff --git a/cps/book_formats.py b/cps/book_formats.py index d295452a..64ec86e8 100644 --- a/cps/book_formats.py +++ b/cps/book_formats.py @@ -99,7 +99,7 @@ def pdf_preview(tmp_file_path, tmp_dir): return None else: cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg" - with Image(filename=tmp_file_path +"[0]", resolution=150) as img: + with Image(filename=tmp_file_path + "[0]", resolution=150) as img: img.compression_quality = 88 img.save(filename=os.path.join(tmp_dir, cover_file_name)) return cover_file_name diff --git a/cps/db.py b/cps/db.py index 67526e2c..f6ee790e 100755 --- a/cps/db.py +++ b/cps/db.py @@ -32,29 +32,29 @@ def title_sort(title): Base = declarative_base() books_authors_link = Table('books_authors_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('author', Integer, ForeignKey('authors.id'), primary_key=True) - ) + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('author', Integer, ForeignKey('authors.id'), primary_key=True) + ) books_tags_link = Table('books_tags_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('tag', Integer, ForeignKey('tags.id'), primary_key=True) - ) + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('tag', Integer, ForeignKey('tags.id'), primary_key=True) + ) books_series_link = Table('books_series_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('series', Integer, ForeignKey('series.id'), primary_key=True) - ) + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('series', Integer, ForeignKey('series.id'), primary_key=True) + ) books_ratings_link = Table('books_ratings_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('rating', Integer, ForeignKey('ratings.id'), primary_key=True) - ) + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('rating', Integer, ForeignKey('ratings.id'), primary_key=True) + ) books_languages_link = Table('books_languages_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('lang_code', Integer, ForeignKey('languages.id'), primary_key=True) - ) + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('lang_code', Integer, ForeignKey('languages.id'), primary_key=True) + ) class Identifiers(Base): @@ -227,7 +227,7 @@ class Books(Base): identifiers = relationship('Identifiers', backref='books') def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover, - authors, tags): # ToDO check Authors and tags necessary + authors, tags): self.title = title self.sort = sort self.author_sort = author_sort diff --git a/cps/fb2.py b/cps/fb2.py index aa887068..205f69ce 100644 --- a/cps/fb2.py +++ b/cps/fb2.py @@ -6,7 +6,7 @@ import os import uploader import StringIO -# ToDo: Check usage of original_file_name + def get_fb2_info(tmp_file_path, original_file_extension): ns = { @@ -20,37 +20,35 @@ def get_fb2_info(tmp_file_path, original_file_extension): authors = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:author', namespaces=ns) def get_author(element): - last_name=element.xpath('fb:last-name/text()', namespaces=ns) + last_name = element.xpath('fb:last-name/text()', namespaces=ns) if len(last_name): - last_name=last_name[0] + last_name = last_name[0] else: - last_name=u'' - middle_name=element.xpath('fb:middle-name/text()', namespaces=ns) + last_name = u'' + middle_name = element.xpath('fb:middle-name/text()', namespaces=ns) if len(middle_name): - middle_name=middle_name[0] + middle_name = middle_name[0] else: - middle_name=u'' - first_name=element.xpath('fb:first-name/text()', namespaces=ns) + middle_name = u'' + first_name = element.xpath('fb:first-name/text()', namespaces=ns) if len(first_name): - first_name=first_name[0] + first_name = first_name[0] else: - first_name=u'' - return first_name + ' ' + middle_name + ' ' + last_name + first_name = u'' + return first_name + ' ' + middle_name + ' ' + last_name author = unicode(", ".join(map(get_author, authors))) title = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:book-title/text()', namespaces=ns) if len(title): - title=unicode(title[0]) + title = unicode(title[0]) else: - title=u'' + title = u'' description = tree.xpath('/fb:FictionBook/fb:description/fb:publish-info/fb:book-name/text()', namespaces=ns) if len(description): - description=unicode(description[0]) + description = unicode(description[0]) else: - description=u'' - - + description = u'' return uploader.BookMeta( file_path=tmp_file_path, diff --git a/cps/helper.py b/cps/helper.py index a0867660..f9251147 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -22,6 +22,11 @@ from email.generator import Generator from flask_babel import gettext as _ import subprocess import shutil +try: + import unidecode + use_unidecode=True +except: + use_unidecode=False def update_download(book_id, user_id): check = ub.session.query(ub.Downloads).filter(ub.Downloads.user_id == user_id).filter(ub.Downloads.book_id == @@ -203,7 +208,7 @@ def get_attachment(file_path): return attachment except IOError: traceback.print_exc() - message = (_('The requested file could not be read. Maybe wrong permissions?')) # ToDo: What is this? + app.logger.error = (u'The requested file could not be read. Maybe wrong permissions?') return None @@ -212,47 +217,54 @@ def get_valid_filename(value, replace_whitespace=True): Returns the given string converted to a string that can be used for a clean filename. Limits num characters to 128 max. """ - value = value[:128] - # re_slugify = re.compile('[^\w\s-]', re.UNICODE) - value = unicodedata.normalize('NFKD', value) - re_slugify = re.compile('[^\w\s-]', re.UNICODE) - value = unicode(re_slugify.sub('', value).strip()) + if value[-1:] ==u'.': + value = value[:-1]+u'_' + if use_unidecode: + value=(unidecode.unidecode(value)).strip() + else: + value=value.replace('§','SS') + value=value.replace('ß','ss') + value = unicodedata.normalize('NFKD', value) + re_slugify = re.compile('[\W\s-]', re.UNICODE) + value = unicode(re_slugify.sub('', value).strip()) if replace_whitespace: - value = re.sub('[\s]+', '_', value, flags=re.U) - value = value.replace(u"\u00DF", "ss") + #*+:\"/<>? werden durch _ ersetzt + value = re.sub('[\*\+:\\\"/<>\?]+', '_', value, flags=re.U) + + value = value[:128] return value +def get_sorted_author(value): + regexes = ["^(JR|SR)\.?$","^I{1,3}\.?$","^IV\.?$"] + combined = "(" + ")|(".join(regexes) + ")" + value = value.split(" ") + if re.match(combined,value[-1].upper()): + value2 = value[-2] + ", " + " ".join(value[:-2]) + " " + value[-1] + else: + value2 = value[-1] + ", " + " ".join(value[:-1]) + return value2 -def get_normalized_author(value): - """ - Normalizes sorted author name - """ - value = unicodedata.normalize('NFKD', value) - value = re.sub('[^\w,\s]', '', value, flags=re.U) - value = " ".join(value.split(", ")[::-1]) - return value - def update_dir_stucture(book_id, calibrepath): db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() - path = os.path.join(calibrepath, book.path) + path = os.path.join(calibrepath, book.path)#.replace('/',os.path.sep)).replace('\\',os.path.sep) - authordir = book.path.split(os.sep)[0] - new_authordir = get_valid_filename(book.authors[0].name, False) - titledir = book.path.split(os.sep)[1] - new_titledir = get_valid_filename(book.title, False) + " (" + str(book_id) + ")" + authordir = book.path.split('/')[0] + new_authordir = get_valid_filename(book.authors[0].name) + titledir = book.path.split('/')[1] + new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" if titledir != new_titledir: new_title_path = os.path.join(os.path.dirname(path), new_titledir) os.rename(path, new_title_path) path = new_title_path - book.path = book.path.split(os.sep)[0] + os.sep + new_titledir + book.path = book.path.split('/')[0] + '/' + new_titledir if authordir != new_authordir: new_author_path = os.path.join(os.path.join(calibrepath, new_authordir), os.path.basename(path)) - os.renames(path, new_author_path) - book.path = new_authordir + os.sep + book.path.split(os.sep)[1] + os.rename(path, new_author_path) + book.path = new_authordir + '/' + book.path.split('/')[1] db.session.commit() diff --git a/cps/templates/admin.html b/cps/templates/admin.html index 2b70e17b..31c167ee 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -2,7 +2,7 @@ {% block body %}