mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Optimized file comparisons
This commit is contained in:
@@ -143,7 +143,7 @@ class Catalogue:
|
|||||||
"""
|
"""
|
||||||
db = Storage(self.db_pointer, self.config)
|
db = Storage(self.db_pointer, self.config)
|
||||||
stored = db.book_paths_list()
|
stored = db.book_paths_list()
|
||||||
closed = db.close()
|
db.close()
|
||||||
if self.books is None:
|
if self.books is None:
|
||||||
self.filter_books()
|
self.filter_books()
|
||||||
on_disk, in_storage = [], []
|
on_disk, in_storage = [], []
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from psycopg2 import Error
|
||||||
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
||||||
@@ -16,21 +17,14 @@ class Storage:
|
|||||||
self.sql = config.catalogue_db
|
self.sql = config.catalogue_db
|
||||||
self.user = config.user
|
self.user = config.user
|
||||||
self.password = config.password
|
self.password = config.password
|
||||||
self.database()
|
self.db_host = config.db_host
|
||||||
|
self.db_port = config.db_port
|
||||||
|
self.db = psycopg2.connect(
|
||||||
|
database=self.sql, user=self.user, password=self.password, host=self.db_host
|
||||||
|
)
|
||||||
|
self.cursor = self.db.cursor()
|
||||||
# self.create_tables()
|
# self.create_tables()
|
||||||
|
|
||||||
def database(self):
|
|
||||||
"""Create database cursor"""
|
|
||||||
try:
|
|
||||||
# self.db = sqlite3.connect(self.db_file)
|
|
||||||
self.db = psycopg2.connect(database=self.sql, user=self.user)
|
|
||||||
self.cursor = self.db.cursor()
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
print(self.sql)
|
|
||||||
print(e)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def create_tables(self):
|
def create_tables(self):
|
||||||
"""Create table structure"""
|
"""Create table structure"""
|
||||||
q_check = "SELECT * FROM books"
|
q_check = "SELECT * FROM books"
|
||||||
@@ -47,22 +41,15 @@ class Storage:
|
|||||||
Insert book in database
|
Insert book in database
|
||||||
:returns: True if succeeds False if not
|
:returns: True if succeeds False if not
|
||||||
"""
|
"""
|
||||||
q_x = (
|
q = "INSERT INTO books (title, author, cover, progress, file_name, pages) values (%s, %s, %s, 0, %s, 0);"
|
||||||
"SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE title = %s)"
|
|
||||||
)
|
|
||||||
q = "INSERT INTO books (title, author, cover, progress, file_name, pages) values (%s, %s, %s, 0, %s, 0)"
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
cover_image = book[2].data
|
cover_image = book[2].data
|
||||||
except:
|
except:
|
||||||
cover_image = book[2]
|
cover_image = book[2]
|
||||||
x = self.cursor.execute(q_x, (book[0],))
|
if not book[2]: # If cover image is missing unset entry
|
||||||
try:
|
cover_image = None
|
||||||
len(x.fetchone()) > 0
|
self.cursor.execute(q, (book[0], book[1], cover_image, book[3]))
|
||||||
except Exception:
|
|
||||||
if not book[2]: # If cover image is missing unset entry
|
|
||||||
cover_image = None
|
|
||||||
self.cursor.execute(q, (book[0], book[1], cover_image, book[3]))
|
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
@@ -72,11 +59,12 @@ class Storage:
|
|||||||
"""
|
"""
|
||||||
Get file paths from database for comparison to system files
|
Get file paths from database for comparison to system files
|
||||||
"""
|
"""
|
||||||
q = """SELECT file_name FROM books"""
|
q = "SELECT file_name FROM books;"
|
||||||
x = self.cursor.execute(q)
|
self.cursor.execute(q)
|
||||||
try:
|
try:
|
||||||
x = x.fetchall()
|
x = self.cursor.fetchall()
|
||||||
except Exception:
|
except psycopg2.Error as e:
|
||||||
|
print(e)
|
||||||
x = []
|
x = []
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user