A mostly working mobi parser

This commit is contained in:
Raelon Masters
2020-06-18 01:46:24 -04:00
parent d397ed8ee7
commit 6a28660e95
2 changed files with 28 additions and 11 deletions

View File

@@ -165,8 +165,7 @@ class Catalogue:
p = re.compile(r"<.*?>") p = re.compile(r"<.*?>")
return p.sub("", source) return p.sub("", source)
@staticmethod def extract_metadata_mobi(self, book):
def extract_metadata_mobi(book):
book = Mobi(book) book = Mobi(book)
book.parse() book.parse()
try: try:
@@ -175,11 +174,30 @@ class Catalogue:
cover_image = None cover_image = None
title = book.title().decode("utf-8") title = book.title().decode("utf-8")
author = book.author().decode("utf-8") author = book.author().decode("utf-8")
book_config = book.config
try:
description = self.stripTags(book_config['exth']['records'][103].decode("utf-8"))
except KeyError:
description = None description = None
try:
identifier = book_config['exth']['records'][104].decode("utf-8")
except KeyError:
identifier = None identifier = None
try:
publisher = book_config['exth']['records'][101].decode("utf-8")
except KeyError:
publisher = None publisher = None
date = None date = None
rights = None rights = None
try:
ftags = book_config['exth']['records'][105].decode("utf-8")
if ":" in ftags:
ftags = ftags.replace(":", ",")
elif ";" in ftags:
ftags = ftags.replace(";", ",")
elif re.search(r"\s", ftags): # Must be final assignment to avoid spliting on multiple delimeters
ftags = ftags.replace(" ", ",")
except KeyError:
ftags = None ftags = None
return [ return [
title, title,

View File

@@ -53,7 +53,7 @@ class Storage:
Insert book in database Insert book in database
:returns: True if succeeds False if not :returns: True if succeeds False if not
""" """
q = "INSERT INTO books (title, author, cover, progress, file_name, pages, description, identifier, publisher, date, rights, tags) values (%s, %s, %s, 0, %s, 0, %s, %s, %s, %s, %s, %s);" q = "INSERT INTO books (title, author, cover, progress, file_name, pages, description, identifier, publisher, rights, tags) values (%s, %s, %s, 0, %s, 0, %s, %s, %s, %s, %s);"
try: try:
try: try:
cover_image = book[2].data cover_image = book[2].data
@@ -71,18 +71,17 @@ class Storage:
book[4], # descr book[4], # descr
book[5], # ident book[5], # ident
book[6], # publisher book[6], # publisher
book[7], # date # book[7], # date # TODO: set import time
book[8], # rights book[8], # rights
book[9], # tags book[9], # tags
), ),
) )
return True return True
except Exception as e: except Exception as e:
if int(e.pgcode) == 22007: if e.pgcode == '22007': # psycopg2's error code for invalid date
book[7] = psycopg2.Date(int(book[7]), 1, 1) book[7] = psycopg2.Date(int(book[7]), 1, 1)
self.insert_book(book) self.insert_book(book)
print(e) raise e
return False
def book_paths_list(self): def book_paths_list(self):
""" """