diff --git a/data/catalogue.db b/data/catalogue.db index 181a078..a20ae30 100644 Binary files a/data/catalogue.db and b/data/catalogue.db differ diff --git a/lib/library.py b/lib/library.py index 22cdb53..02b5c17 100755 --- a/lib/library.py +++ b/lib/library.py @@ -19,12 +19,14 @@ class Catalogue: self.opf_regx = re.compile(r'\.opf') self.cover_regx = re.compile(r'\.jpg|\.jpeg|\.png|\.bmp|\.gif') self.html_regx = re.compile(r'\.html') + """ with open(config.book_shelf, 'r') as f: try: self.catalogue = json.load(f) self.current_files = self.scan_folder() except Exception: self.filter_books() + """ def scan_folder(self, folder=config.book_path): for f in os.listdir(folder): @@ -90,12 +92,15 @@ class Catalogue: title = soup.find("dc:title") if title == None: title = book['path'].split('/')[-1].rsplit('.', 1)[0] + else: title = title.contents[0] author = soup.find("dc:creator") + if author == None: author = 'Unlisted' + else: author = author.contents[0] try: cover = self.extract_cover_image(book_zip, book) except IndexError: # cover = self.extract_cover_html(book_zip, book) cover = DuckDuckGo().image_result(title) - book_details = [title.contents[0], author.contents[0], cover] + book_details = [title, author, cover, book['path']] return book_details def extract_content(self, book_zip, book): @@ -122,7 +127,8 @@ class Catalogue: filter(self.cover_regx.search, book['files']) )[0] ) - return cover + try: cover = book_zip.read(cover.name); return cover + except KeyError: return False def compare_shelf_current(self): try: diff --git a/lib/pyShelf.py b/lib/pyShelf.py index aefeb2a..d5da1cd 100755 --- a/lib/pyShelf.py +++ b/lib/pyShelf.py @@ -2,8 +2,10 @@ import os import zipfile from config import Config +from library import Catalogue +from storage import Storage config = Config() - +Storage = Storage() class InitFiles: """First run file creation operations""" def __init__(self, file_array): @@ -25,9 +27,14 @@ class Epub: def __init__(self): global config self.book_path = config.book_path + self.Catalogue = Catalogue() - def import_book(self): - pass + def import_books(self): + book_list = self.Catalogue.filter_books() + for book in book_list: + extracted = self.Catalogue.extract_metadata(book_list[book]) + Storage.insert_book(extracted) + Storage.commit() def book_list(self): pass diff --git a/lib/storage.py b/lib/storage.py index fdf29a7..b52e761 100644 --- a/lib/storage.py +++ b/lib/storage.py @@ -12,6 +12,7 @@ class Storage: def __init__(self): self.db_file = db_pointer self.database() + self.create_tables() def database(self): """Create database cursor""" @@ -26,11 +27,11 @@ class Storage: """Create table structure""" q_check = "SELECT * FROM books" q_create = '''CREATE TABLE books(title text, author text, - categories text, cover blob, pages int, progress int, + categories text, cover blob, pages int, progress int, file_name text)''' try: self.cursor.execute(q_check) - except Exception as e: + except sqlite3.OperationalError as e: self.cursor.execute(q_create) def insert_book(self, book): @@ -38,11 +39,19 @@ class Storage: Insert book in database :returns: True if succeeds False if not """ - q = '''INSERT INTO books (title, author, categories, cover, - pages, progress, file_name) values (%s, %s, %s, %s, 0, %s)''' % () + q_x = '''SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE `title` = ?)''' + q = '''INSERT INTO books (title, author, cover, file_name) values (?, ?, ?, ?)''' try: - self.cursor.execute(q) + try: cover_image = book[2].data + except: cover_image = book[2] + x = self.cursor.execute(q_x, (book[0],)) + try: len(x.fetchone()) > 0 + except Exception: self.cursor.execute(q, (book[0], book[1], cover_image, book[3])) return True except Exception as e: print(e) - return False \ No newline at end of file + return False + + def commit(self): + try: self.db.commit(); return True + except Exception as e: return False \ No newline at end of file diff --git a/main.py b/main.py index fcbb4a9..26bc496 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys from PIL import Image sys.path.insert(1, 'lib/') -from pyShelf import InitFiles +from pyShelf import InitFiles, Epub from config import Config from library import Catalogue # Get configuration settings @@ -14,15 +14,4 @@ InitFiles(config.file_array) Catalogue = Catalogue() # Filter Your books # This only needs to be run on first run, & when new books are added - -# TODO Refactor to its own function -book_list = Catalogue.filter_books() -for book in book_list: - extracted = Catalogue.extract_metadata(book_list[book]) - # TODO Insert extracted data to database - if extracted[2] != False: - # Only display if a image is present - cover_image = Image.open(extracted[2]) - cover_image.show() - # TODO Insert book in database - print(extracted) \ No newline at end of file +Epub().import_books() \ No newline at end of file