mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
A mostly working mobi parser
This commit is contained in:
@@ -165,8 +165,7 @@ class Catalogue:
|
||||
p = re.compile(r"<.*?>")
|
||||
return p.sub("", source)
|
||||
|
||||
@staticmethod
|
||||
def extract_metadata_mobi(book):
|
||||
def extract_metadata_mobi(self, book):
|
||||
book = Mobi(book)
|
||||
book.parse()
|
||||
try:
|
||||
@@ -175,12 +174,31 @@ class Catalogue:
|
||||
cover_image = None
|
||||
title = book.title().decode("utf-8")
|
||||
author = book.author().decode("utf-8")
|
||||
description = None
|
||||
identifier = None
|
||||
publisher = None
|
||||
book_config = book.config
|
||||
try:
|
||||
description = self.stripTags(book_config['exth']['records'][103].decode("utf-8"))
|
||||
except KeyError:
|
||||
description = None
|
||||
try:
|
||||
identifier = book_config['exth']['records'][104].decode("utf-8")
|
||||
except KeyError:
|
||||
identifier = None
|
||||
try:
|
||||
publisher = book_config['exth']['records'][101].decode("utf-8")
|
||||
except KeyError:
|
||||
publisher = None
|
||||
date = None
|
||||
rights = None
|
||||
ftags = 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
|
||||
return [
|
||||
title,
|
||||
author,
|
||||
|
||||
@@ -53,7 +53,7 @@ class Storage:
|
||||
Insert book in database
|
||||
: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:
|
||||
cover_image = book[2].data
|
||||
@@ -71,18 +71,17 @@ class Storage:
|
||||
book[4], # descr
|
||||
book[5], # ident
|
||||
book[6], # publisher
|
||||
book[7], # date
|
||||
# book[7], # date # TODO: set import time
|
||||
book[8], # rights
|
||||
book[9], # tags
|
||||
),
|
||||
)
|
||||
return True
|
||||
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)
|
||||
self.insert_book(book)
|
||||
print(e)
|
||||
return False
|
||||
raise e
|
||||
|
||||
def book_paths_list(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user