Initial file loading

epub content parsing is horribly borked
This commit is contained in:
BasioMeusPuga
2017-11-11 23:21:49 +05:30
parent 5d495cfde3
commit 7fbea194c0
6 changed files with 236 additions and 120 deletions

View File

@@ -3,6 +3,7 @@
import sqlite3
import os
class DatabaseInit:
def __init__(self, location_prefix):
os.makedirs(location_prefix, exist_ok=True)
@@ -16,7 +17,7 @@ class DatabaseInit:
self.database.execute(
"CREATE TABLE books \
(id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, \
Path TEXT, ISBN TEXT, Tags TEXT, Hash TEXT, CoverImage BLOB)")
Path TEXT, Position TEXT, ISBN TEXT, Tags TEXT, Hash TEXT, CoverImage BLOB)")
self.database.execute(
"CREATE TABLE cache \
(id INTEGER PRIMARY KEY, Name TEXT, Path TEXT, CachedDict BLOB)")
@@ -26,28 +27,29 @@ class DatabaseInit:
self.database.commit()
self.database.close()
class DatabaseFunctions:
def __init__(self, location_prefix):
database_path = os.path.join(location_prefix, 'Lector.db')
self.database = sqlite3.connect(database_path)
def add_to_database(self, book_data):
# book_data is expected to be a dictionary
def add_to_database(self, data):
# data is expected to be a dictionary
# with keys corresponding to the book hash
# and corresponding items containing
# whatever else needs insertion
# Haha I said insertion
for i in book_data.items():
for i in data.items():
book_hash = i[0]
book_title = i[1]['title']
book_author = i[1]['author']
book_year = i[1]['year']
if not book_year:
book_year = 9999
book_path = i[1]['path']
book_cover = i[1]['cover_image']
book_isbn = i[1]['isbn']
title = i[1]['title']
author = i[1]['author']
year = i[1]['year']
if not year:
year = 9999
path = i[1]['path']
cover = i[1]['cover_image']
isbn = i[1]['isbn']
sql_command_add = (
"INSERT INTO books (Title,Author,Year,Path,ISBN,Hash,CoverImage) VALUES(?, ?, ?, ?, ?, ?, ?)")
@@ -55,11 +57,11 @@ class DatabaseFunctions:
# TODO
# This is a placeholder. You will need to generate book covers
# in case none are found
if book_cover:
if cover:
self.database.execute(
sql_command_add,
[book_title, book_author, book_year,
book_path, book_isbn, book_hash, sqlite3.Binary(book_cover)])
[title, author, year,
path, isbn, book_hash, sqlite3.Binary(cover)])
self.database.commit()
@@ -95,15 +97,15 @@ class DatabaseFunctions:
sql_command_fetch = sql_command_fetch[:-3] # Truncate the last OR
# book data is returned as a list of tuples
book_data = self.database.execute(sql_command_fetch).fetchall()
data = self.database.execute(sql_command_fetch).fetchall()
if book_data:
if data:
# Because this is the result of a fetchall(), we need an
# ugly hack (tm) to get correct results
if fetch_one:
return book_data[0][0]
return data[0][0]
return book_data
return data
else:
return None