precommit formatting

This commit is contained in:
th3r00t
2022-11-14 00:03:50 -05:00
parent 1e4d52bf0a
commit c8a80406e5
2 changed files with 37 additions and 20 deletions

33
src/backend/lib/library.py vendored Executable file → Normal file
View File

@@ -2,8 +2,8 @@
import os import os
import re import re
import zipfile import zipfile
import PyPDF2
import PyPDF2
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from mobi import Mobi from mobi import Mobi
@@ -105,7 +105,8 @@ class Catalogue:
if re.match(self.title_sanitization_regx, title): if re.match(self.title_sanitization_regx, title):
if re.match(self.title_sanitization_lvl2_regx, title): if re.match(self.title_sanitization_lvl2_regx, title):
title = re.split(r"-+\W", title)[1] 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") author = soup.find("dc:creator")
if author is not None: if author is not None:
@@ -161,7 +162,7 @@ class Catalogue:
return book_details return book_details
def extract_metadata_pdf(self, book): def extract_metadata_pdf(self, book):
""" Return extracted metadata """Return extracted metadata
:NOTES: Retrieval of data has been problematic, some pdf's providing :NOTES: Retrieval of data has been problematic, some pdf's providing
reliable titles that corespond with the actual, and others being reliable titles that corespond with the actual, and others being
nonsense. nonsense.
@@ -226,21 +227,23 @@ class Catalogue:
author = book.author().decode("utf-8") author = book.author().decode("utf-8")
book_config = book.config book_config = book.config
try: 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: except KeyError:
description = None description = None
try: try:
identifier = book_config['exth']['records'][104].decode("utf-8") identifier = book_config["exth"]["records"][104].decode("utf-8")
except KeyError: except KeyError:
identifier = None identifier = None
try: try:
publisher = book_config['exth']['records'][101].decode("utf-8") publisher = book_config["exth"]["records"][101].decode("utf-8")
except KeyError: except KeyError:
publisher = None publisher = None
date = None date = None
rights = None rights = None
try: try:
ftags = book_config['exth']['records'][105].decode("utf-8") ftags = book_config["exth"]["records"][105].decode("utf-8")
if ":" in ftags: if ":" in ftags:
ftags = ftags.replace(":", ",") ftags = ftags.replace(":", ",")
elif ";" in ftags: elif ";" in ftags:
@@ -282,10 +285,12 @@ class Catalogue:
Opens epub as zip file filters then stores as list any files matching cover_regx Opens epub as zip file filters then stores as list any files matching cover_regx
""" """
try: 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) cover = book_zip.read(cover.name)
return cover return cover
except Exception as e: except Exception:
return False return False
def compare_shelf_current(self): def compare_shelf_current(self):
@@ -303,7 +308,9 @@ class Catalogue:
on_disk.append(_x) on_disk.append(_x)
for _y in stored: for _y in stored:
in_storage.append(_y) 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) c = set.difference(a, b)
return c return c
@@ -314,14 +321,14 @@ class Catalogue:
Iterates over list and inserts new books into database. Iterates over list and inserts new books into database.
""" """
try: try:
fsocket = kwargs['socket'] fsocket = kwargs["socket"]
except KeyError: except KeyError:
fsocket = '/dev/null' fsocket = "/dev/null"
book_list = self.compare_shelf_current() book_list = self.compare_shelf_current()
db = Storage(self.config) db = Storage(self.config)
for book in book_list: for book in book_list:
book = self.process_by_filetype(book) book = self.process_by_filetype(book)
with open(fsocket, 'w') as _socket: with open(fsocket, "w") as _socket:
try: try:
_socket.write(book[0]) _socket.write(book[0])
except TypeError: except TypeError:

24
src/backend/lib/storage.py vendored Executable file → Normal file
View File

@@ -1,10 +1,11 @@
#!/usr/bin/python #!/usr/bin/python
import datetime
import re 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 sqlalchemy.orm import Session
from .models import Book, Collection
class Storage: class Storage:
"""Contains all methods for system storage""" """Contains all methods for system storage"""
@@ -15,7 +16,9 @@ class Storage:
self.password = config.password self.password = config.password
self.db_host = config.db_host self.db_host = config.db_host
self.db_port = config.db_port 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 self.config = config
def create_tables(self): def create_tables(self):
@@ -37,7 +40,7 @@ class Storage:
if not book[2]: # If cover image is missing unset entry if not book[2]: # If cover image is missing unset entry
cover_image = None cover_image = None
if not book[1]: if not book[1]:
author = "None" pass
_book = Book( _book = Book(
title=book[0], title=book[0],
author=book[1], author=book[1],
@@ -47,7 +50,7 @@ class Storage:
identifier=book[5], identifier=book[5],
publisher=book[6], publisher=book[6],
rights=book[8], rights=book[8],
tags=book[9] tags=book[9],
) )
session.add(_book) session.add(_book)
session.commit() session.commit()
@@ -68,6 +71,7 @@ class Storage:
def make_collections(self): def make_collections(self):
# TODO: Check this still works with the switch to sqlalchemy # 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") _title_regx = re.compile(r"^[0-9][0-9]*|-|\ \B")
session = Session(self.engine) session = Session(self.engine)
_set = session.execute(select(Book.book_id, Book.file_name)).all() _set = session.execute(select(Book.book_id, Book.file_name)).all()
@@ -86,7 +90,12 @@ class Storage:
_x = re.sub(_title_regx, "", _s) _x = re.sub(_title_regx, "", _s)
_s = _x.strip() _s = _x.strip()
_sess = Session(self.engine) _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() _sess.close()
if _q.fetchone() is None: if _q.fetchone() is None:
_collection = Collection(collection=_s, book_id=book.book_id) _collection = Collection(collection=_s, book_id=book.book_id)
@@ -99,3 +108,4 @@ class Storage:
except Exception as e: except Exception as e:
self.config.logger.error(f"Collection {_s} failed: {e}") self.config.logger.error(f"Collection {_s} failed: {e}")
_collections.append(_p) _collections.append(_p)
self.config.logger.info("Finished making collections.")