diff --git a/src/backend/lib/library.py b/src/backend/lib/library.py old mode 100755 new mode 100644 index 8cde59f..23f6014 --- a/src/backend/lib/library.py +++ b/src/backend/lib/library.py @@ -2,8 +2,8 @@ import os import re import zipfile -import PyPDF2 +import PyPDF2 from bs4 import BeautifulSoup from mobi import Mobi @@ -105,7 +105,8 @@ class Catalogue: if re.match(self.title_sanitization_regx, title): if re.match(self.title_sanitization_lvl2_regx, title): title = re.split(r"-+\W", title)[1] - else: title = re.split(self.title_sanitization_regx, title)[2] + else: + title = re.split(self.title_sanitization_regx, title)[2] author = soup.find("dc:creator") if author is not None: @@ -161,7 +162,7 @@ class Catalogue: return book_details def extract_metadata_pdf(self, book): - """ Return extracted metadata + """Return extracted metadata :NOTES: Retrieval of data has been problematic, some pdf's providing reliable titles that corespond with the actual, and others being nonsense. @@ -226,21 +227,23 @@ class Catalogue: author = book.author().decode("utf-8") book_config = book.config try: - description = self.stripTags(book_config['exth']['records'][103].decode("utf-8")) + 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") + identifier = book_config["exth"]["records"][104].decode("utf-8") except KeyError: identifier = None try: - publisher = book_config['exth']['records'][101].decode("utf-8") + publisher = book_config["exth"]["records"][101].decode("utf-8") except KeyError: publisher = None date = None rights = None try: - ftags = book_config['exth']['records'][105].decode("utf-8") + ftags = book_config["exth"]["records"][105].decode("utf-8") if ":" in ftags: ftags = ftags.replace(":", ",") elif ";" in ftags: @@ -282,10 +285,12 @@ class Catalogue: Opens epub as zip file filters then stores as list any files matching cover_regx """ try: - cover = book_zip.open(list(filter(self.cover_regx.search, book["files"]))[0]) + cover = book_zip.open( + list(filter(self.cover_regx.search, book["files"]))[0] + ) cover = book_zip.read(cover.name) return cover - except Exception as e: + except Exception: return False def compare_shelf_current(self): @@ -303,7 +308,9 @@ class Catalogue: on_disk.append(_x) for _y in stored: in_storage.append(_y) - a, b, = set(on_disk), set(in_storage) + a, b, = set( + on_disk + ), set(in_storage) c = set.difference(a, b) return c @@ -314,14 +321,14 @@ class Catalogue: Iterates over list and inserts new books into database. """ try: - fsocket = kwargs['socket'] + fsocket = kwargs["socket"] except KeyError: - fsocket = '/dev/null' + fsocket = "/dev/null" book_list = self.compare_shelf_current() db = Storage(self.config) for book in book_list: book = self.process_by_filetype(book) - with open(fsocket, 'w') as _socket: + with open(fsocket, "w") as _socket: try: _socket.write(book[0]) except TypeError: diff --git a/src/backend/lib/storage.py b/src/backend/lib/storage.py old mode 100755 new mode 100644 index 0235d2d..1490ab4 --- a/src/backend/lib/storage.py +++ b/src/backend/lib/storage.py @@ -1,10 +1,11 @@ #!/usr/bin/python -import datetime import re -from .models import Book, Collection -from sqlalchemy import create_engine, text, select + +from sqlalchemy import create_engine, select from sqlalchemy.orm import Session +from .models import Book, Collection + class Storage: """Contains all methods for system storage""" @@ -15,7 +16,9 @@ class Storage: self.password = config.password self.db_host = config.db_host self.db_port = config.db_port - self.engine = create_engine(f"postgresql://{self.user}:{self.password}@{self.db_host}:{self.db_port}/{self.sql}") + self.engine = create_engine( + f"postgresql://{self.user}:{self.password}@{self.db_host}:{self.db_port}/{self.sql}" + ) self.config = config def create_tables(self): @@ -37,7 +40,7 @@ class Storage: if not book[2]: # If cover image is missing unset entry cover_image = None if not book[1]: - author = "None" + pass _book = Book( title=book[0], author=book[1], @@ -47,7 +50,7 @@ class Storage: identifier=book[5], publisher=book[6], rights=book[8], - tags=book[9] + tags=book[9], ) session.add(_book) session.commit() @@ -68,6 +71,7 @@ class Storage: def make_collections(self): # TODO: Check this still works with the switch to sqlalchemy + self.config.logger.info("Making collections.") _title_regx = re.compile(r"^[0-9][0-9]*|-|\ \B") session = Session(self.engine) _set = session.execute(select(Book.book_id, Book.file_name)).all() @@ -86,7 +90,12 @@ class Storage: _x = re.sub(_title_regx, "", _s) _s = _x.strip() _sess = Session(self.engine) - _q = _sess.execute(select(Collection.collection_id).where(Collection.collection == _s, Collection.book_id == book.book_id)) + _q = _sess.execute( + select(Collection.collection_id).where( + Collection.collection == _s, + Collection.book_id == book.book_id, + ) + ) _sess.close() if _q.fetchone() is None: _collection = Collection(collection=_s, book_id=book.book_id) @@ -99,3 +108,4 @@ class Storage: except Exception as e: self.config.logger.error(f"Collection {_s} failed: {e}") _collections.append(_p) + self.config.logger.info("Finished making collections.")