mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Refactored call to filter_books into Epub class
This commit is contained in:
Binary file not shown.
@@ -19,12 +19,14 @@ class Catalogue:
|
|||||||
self.opf_regx = re.compile(r'\.opf')
|
self.opf_regx = re.compile(r'\.opf')
|
||||||
self.cover_regx = re.compile(r'\.jpg|\.jpeg|\.png|\.bmp|\.gif')
|
self.cover_regx = re.compile(r'\.jpg|\.jpeg|\.png|\.bmp|\.gif')
|
||||||
self.html_regx = re.compile(r'\.html')
|
self.html_regx = re.compile(r'\.html')
|
||||||
|
"""
|
||||||
with open(config.book_shelf, 'r') as f:
|
with open(config.book_shelf, 'r') as f:
|
||||||
try:
|
try:
|
||||||
self.catalogue = json.load(f)
|
self.catalogue = json.load(f)
|
||||||
self.current_files = self.scan_folder()
|
self.current_files = self.scan_folder()
|
||||||
except Exception:
|
except Exception:
|
||||||
self.filter_books()
|
self.filter_books()
|
||||||
|
"""
|
||||||
|
|
||||||
def scan_folder(self, folder=config.book_path):
|
def scan_folder(self, folder=config.book_path):
|
||||||
for f in os.listdir(folder):
|
for f in os.listdir(folder):
|
||||||
@@ -90,12 +92,15 @@ class Catalogue:
|
|||||||
title = soup.find("dc:title")
|
title = soup.find("dc:title")
|
||||||
if title == None:
|
if title == None:
|
||||||
title = book['path'].split('/')[-1].rsplit('.', 1)[0]
|
title = book['path'].split('/')[-1].rsplit('.', 1)[0]
|
||||||
|
else: title = title.contents[0]
|
||||||
author = soup.find("dc:creator")
|
author = soup.find("dc:creator")
|
||||||
|
if author == None: author = 'Unlisted'
|
||||||
|
else: author = author.contents[0]
|
||||||
try: cover = self.extract_cover_image(book_zip, book)
|
try: cover = self.extract_cover_image(book_zip, book)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
# cover = self.extract_cover_html(book_zip, book)
|
# cover = self.extract_cover_html(book_zip, book)
|
||||||
cover = DuckDuckGo().image_result(title)
|
cover = DuckDuckGo().image_result(title)
|
||||||
book_details = [title.contents[0], author.contents[0], cover]
|
book_details = [title, author, cover, book['path']]
|
||||||
return book_details
|
return book_details
|
||||||
|
|
||||||
def extract_content(self, book_zip, book):
|
def extract_content(self, book_zip, book):
|
||||||
@@ -122,7 +127,8 @@ class Catalogue:
|
|||||||
filter(self.cover_regx.search, book['files'])
|
filter(self.cover_regx.search, book['files'])
|
||||||
)[0]
|
)[0]
|
||||||
)
|
)
|
||||||
return cover
|
try: cover = book_zip.read(cover.name); return cover
|
||||||
|
except KeyError: return False
|
||||||
|
|
||||||
def compare_shelf_current(self):
|
def compare_shelf_current(self):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
import os
|
import os
|
||||||
import zipfile
|
import zipfile
|
||||||
from config import Config
|
from config import Config
|
||||||
|
from library import Catalogue
|
||||||
|
from storage import Storage
|
||||||
config = Config()
|
config = Config()
|
||||||
|
Storage = Storage()
|
||||||
class InitFiles:
|
class InitFiles:
|
||||||
"""First run file creation operations"""
|
"""First run file creation operations"""
|
||||||
def __init__(self, file_array):
|
def __init__(self, file_array):
|
||||||
@@ -25,9 +27,14 @@ class Epub:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
global config
|
global config
|
||||||
self.book_path = config.book_path
|
self.book_path = config.book_path
|
||||||
|
self.Catalogue = Catalogue()
|
||||||
|
|
||||||
def import_book(self):
|
def import_books(self):
|
||||||
pass
|
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):
|
def book_list(self):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class Storage:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.db_file = db_pointer
|
self.db_file = db_pointer
|
||||||
self.database()
|
self.database()
|
||||||
|
self.create_tables()
|
||||||
|
|
||||||
def database(self):
|
def database(self):
|
||||||
"""Create database cursor"""
|
"""Create database cursor"""
|
||||||
@@ -26,11 +27,11 @@ class Storage:
|
|||||||
"""Create table structure"""
|
"""Create table structure"""
|
||||||
q_check = "SELECT * FROM books"
|
q_check = "SELECT * FROM books"
|
||||||
q_create = '''CREATE TABLE books(title text, author text,
|
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)'''
|
file_name text)'''
|
||||||
try:
|
try:
|
||||||
self.cursor.execute(q_check)
|
self.cursor.execute(q_check)
|
||||||
except Exception as e:
|
except sqlite3.OperationalError as e:
|
||||||
self.cursor.execute(q_create)
|
self.cursor.execute(q_create)
|
||||||
|
|
||||||
def insert_book(self, book):
|
def insert_book(self, book):
|
||||||
@@ -38,11 +39,19 @@ 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 = '''INSERT INTO books (title, author, categories, cover,
|
q_x = '''SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE `title` = ?)'''
|
||||||
pages, progress, file_name) values (%s, %s, %s, %s, 0, %s)''' % ()
|
q = '''INSERT INTO books (title, author, cover, file_name) values (?, ?, ?, ?)'''
|
||||||
try:
|
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
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
try: self.db.commit(); return True
|
||||||
|
except Exception as e: return False
|
||||||
15
main.py
15
main.py
@@ -3,7 +3,7 @@
|
|||||||
import sys
|
import sys
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
sys.path.insert(1, 'lib/')
|
sys.path.insert(1, 'lib/')
|
||||||
from pyShelf import InitFiles
|
from pyShelf import InitFiles, Epub
|
||||||
from config import Config
|
from config import Config
|
||||||
from library import Catalogue
|
from library import Catalogue
|
||||||
# Get configuration settings
|
# Get configuration settings
|
||||||
@@ -14,15 +14,4 @@ InitFiles(config.file_array)
|
|||||||
Catalogue = Catalogue()
|
Catalogue = Catalogue()
|
||||||
# Filter Your books
|
# Filter Your books
|
||||||
# This only needs to be run on first run, & when new books are added
|
# This only needs to be run on first run, & when new books are added
|
||||||
|
Epub().import_books()
|
||||||
# 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)
|
|
||||||
Reference in New Issue
Block a user