diff --git a/__main__.py b/__main__.py index cb7c61f..1a339fd 100755 --- a/__main__.py +++ b/__main__.py @@ -145,7 +145,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): if my_file[0]: self.listView.setEnabled(False) self.last_open_path = os.path.dirname(my_file[0][0]) - books = sorter.BookSorter(my_file[0]) + books = sorter.BookSorter(my_file[0], self.database_path) parsed_books = books.initiate_threads() database.DatabaseFunctions(self.database_path).add_to_database(parsed_books) self.listView.setEnabled(True) diff --git a/database.py b/database.py index b1f416f..61f1cfb 100644 --- a/database.py +++ b/database.py @@ -49,22 +49,13 @@ class DatabaseFunctions: book_cover = i[1]['cover_image'] book_isbn = i[1]['isbn'] - # Check if the file might not already be in the database - # A None return signifies that that addition is possible - hash_from_database = self.fetch_data( - ('Title',), - 'books', - {'Hash': book_hash}, - 'EQUALS', - True) - sql_command_add = ( "INSERT INTO books (Title,Author,Year,Path,ISBN,Hash,CoverImage) VALUES(?, ?, ?, ?, ?, ?, ?)") # TODO # This is a placeholder. You will need to generate book covers # in case none are found - if not hash_from_database and book_cover: + if book_cover: self.database.execute( sql_command_add, [book_title, book_author, book_year, diff --git a/sorter.py b/sorter.py index 568faed..daac2f8 100644 --- a/sorter.py +++ b/sorter.py @@ -6,11 +6,11 @@ import hashlib from multiprocessing.dummy import Pool +import database from parsers.epub import ParseEPUB - class BookSorter: - def __init__(self, file_list): + def __init__(self, file_list, database_path): # Have the GUI pass a list of files straight to here # Then, on the basis of what is needed, pass the # filenames to the requisite functions @@ -19,17 +19,33 @@ class BookSorter: # Caching upon closing self.file_list = file_list self.all_books = {} + self.database_path = database_path + self.hashes = [] + self.database_hashes() + + def database_hashes(self): + all_hashes = database.DatabaseFunctions( + self.database_path).fetch_data( + ('Hash',), + 'books', + {'Hash': ''}, + 'LIKE') + + if all_hashes: + self.hashes = [i[0] for i in all_hashes] def read_book(self, filename): # filename is expected as a string containg the # full path of the ebook file - # TODO - # See if you want to include a hash of the book's name and author with open(filename, 'rb') as current_book: file_md5 = hashlib.md5(current_book.read()).hexdigest() - if file_md5 in self.all_books.items(): + # Do not allow addition in case the file is dupicated in the directory + # OR is already in the database + # TODO + # See if you want to include a hash of the book's name and author + if file_md5 in self.all_books.items() or file_md5 in self.hashes: return # TODO diff --git a/subclasses.py b/subclasses.py index 2662a64..27ed1de 100644 --- a/subclasses.py +++ b/subclasses.py @@ -151,4 +151,4 @@ class Settings: self.settings.beginGroup('runtimeVariables') self.settings.setValue('lastOpenPath', self.parent_window.last_open_path) self.settings.setValue('databasePath', self.parent_window.database_path) - self.settings.endGroup() \ No newline at end of file + self.settings.endGroup()