Skip to content

Commit

Permalink
Fix for #1221
Browse files Browse the repository at this point in the history
  • Loading branch information
philborman committed Feb 15, 2018
1 parent f208dac commit 236b615
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lazylibrarian/librarysync.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,10 @@ def LibraryScan(startdir=None, library='eBook', authid=None, remove=True):
extn = os.path.splitext(files)[1]

# if it's an epub or a mobi we can try to read metadata from it
if (extn == ".epub") or (extn == ".mobi"):
if extn in [".epub", ".mobi"]:
book_filename = os.path.join(rootdir, files)
if PY2:
book_filename = book_filename.encode(lazylibrarian.SYS_ENCODING)

try:
res = get_book_info(book_filename)
except Exception as e:
Expand Down
12 changes: 8 additions & 4 deletions lazylibrarian/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ def processAlternate(source_dir=None):
logger.debug('No metadata file found for %s' % new_book)
if 'title' not in metadata or 'creator' not in metadata:
# if not got both, try to get metadata from the book file
try:
metadata = get_book_info(new_book)
except Exception as e:
logger.debug('No metadata found in %s, %s %s' % (new_book, type(e).__name__, str(e)))
extn = os.path.splitext(new_book)[1]
if extn in [".epub", ".mobi"]:
if PY2:
new_book = new_book.encode(lazylibrarian.SYS_ENCODING)
try:
metadata = get_book_info(new_book)
except Exception as e:
logger.debug('No metadata found in %s, %s %s' % (new_book, type(e).__name__, str(e)))
if 'title' in metadata and 'creator' in metadata:
authorname = metadata['creator']
bookname = metadata['title']
Expand Down
14 changes: 13 additions & 1 deletion lazylibrarian/webServe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,7 @@ def editBook(self, bookid=None):

@cherrypy.expose
def bookUpdate(self, bookname='', bookid='', booksub='', bookgenre='', booklang='',
manual='0', authorname='', cover='', **kwargs):
manual='0', authorname='', cover='', newid='', **kwargs):

myDB = database.DBConnection()
if bookid:
Expand All @@ -1905,6 +1905,17 @@ def bookUpdate(self, bookname='', bookid='', booksub='', bookgenre='', booklang=
if bookgenre == 'None':
bookgenre = ''
manual = bool(check_int(manual, 0))

if not (bookid == newid):
cmd = "SELECT BookName,Authorname from books,authors "
cmd += "WHERE books.AuthorID = authors.AuthorID and BookID=?"
match = myDB.match(cmd, (newid,))
if match:
logger.warn("Cannot change bookid to %s, in use by %s/%s" %
(newid, match['BookName'], match['AuthorName']))
else:
logger.warn("Updating bookid is not supported yet")
# edited += "BookID "
if not (bookdata["BookName"] == bookname):
edited += "Title "
if not (bookdata["BookSub"] == booksub):
Expand Down Expand Up @@ -2001,6 +2012,7 @@ def bookUpdate(self, bookname='', bookid='', booksub='', bookgenre='', booklang=
else:
logger.debug('Book [%s] has not been moved' % bookname)
# if edited or moved:

raise cherrypy.HTTPRedirect("editBook?bookid=%s" % bookid)

raise cherrypy.HTTPRedirect("books")
Expand Down
6 changes: 5 additions & 1 deletion lib/mobi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from struct import *
from pprint import pprint
from lib.six import PY2

if PY2:
import utils
from lz77 import uncompress_lz77
Expand Down Expand Up @@ -358,7 +359,10 @@ def parseMobiHeader(self):
self.offset += resultsDict['header length'];

def onebits(x, width=16):
return len(filter(lambda x: x == "1", (str((x>>i)&1) for i in xrange(width-1,-1,-1))));
if PY2:
return len(filter(lambda x: x == "1", (str((x>>i)&1) for i in xrange(width-1,-1,-1))));
else:
return len([x for x in (str((x>>i)&1) for i in range(width-1,-1,-1)) if x == "1"]);

resultsDict['extra bytes'] = 2*onebits(unpack(">H", self.contents[self.offset-2:self.offset])[0] & 0xFFFE)

Expand Down

0 comments on commit 236b615

Please sign in to comment.