-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.py
202 lines (152 loc) · 7.97 KB
/
books.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
__author__ = 'Nicolas Briche'
__license__ = 'GPL v3'
__docformat__ = 'restructuredtext en'
from logger import log
import os
import sqlite3
import time
from contextlib import closing
from calibre.devices.usbms.books import Book
from calibre.ebooks.metadata import author_to_author_sort
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
class BookeenDatabase(object):
def __init__(self, path_main, path_card=None):
log.debug("init db")
self.databases = {'main': {'uuid': '', 'path': path_main, 'prefix_fs': '/mnt/fat/', 'books': {}}, 'carda': {'uuid': '', 'path': path_card, 'prefix_fs': '/mnt/sd/', 'books': {}}}
self.collections = {}
self.shelves_links = {}
self.books = {}
self.can_set_as_read = False
self.to_close = []
self.to_set_as_read = []
self.set_as_read_field = None
self.init_db('main')
if self.databases['carda']['path']:
self.init_db('carda')
def init_db(self, database):
self.databases[database]['uuid'] = ''
with closing(sqlite3.connect(str(self.databases[database]['path']))) as connection:
log.debug("connect to db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute('SELECT f_db_uuid FROM T_UUIDDB')
self.databases[database]['uuid'] = cursor.fetchone()['f_db_uuid']
log.debug("dbuuid: {}".format(self.databases[database]['uuid']))
if database == 'main':
log.debug("this is the main db")
cursor.execute('SELECT * FROM T_SHELF')
for shelf in cursor.fetchall():
self.collections[shelf['f_id_shelf']] = BookeenShelf(shelf)
cursor.execute('SELECT * FROM T_SHELF_LINK')
for link in cursor.fetchall():
self.shelves_links[link['f_item_id']] = (link['f_shelf_id'], link['f_db_uuid'])
log.debug("finding books")
cursor.execute('SELECT * FROM T_ITEM WHERE f_item_filetype=2')
for book_row in cursor.fetchall():
finished = True if book_row['f_islastpage'] else False
book = BookeenBook('', book_row['f_internal_uri'][len(self.databases[database]['prefix_fs']):], book_row['f_title'], finished=finished, current_page=book_row['f_current_page'], date=time.gmtime(book_row['f_documenttime']))
if book_row['f_id_item'] in self.shelves_links.keys():
self.collections[self.shelves_links[book_row['f_id_item']][0]].add_book(book)
self.books[(self.databases[database]['uuid'], book_row['f_id_item'])] = book
self.databases[database]['books'][book_row['f_id_item']] = book
log.debug("Found {} books on {}".format(len(self.books), database))
def match(self, oncard, calibre_books, threshold=0):
# ['#pages', '#cleaned', 'rights', 'author_sort', 'author_link_map', 'publisher', 'db_id', 'device_collections', '#onmuse', 'authors', 'languages', '#words', 'uuid', 'rating', 'tags', '#list_remove_orizon', 'cover', 'toc', 'user_metadata', '#author_sort', '#list_orizon', '#read', 'publication_type', 'series_index', 'size', 'series', 'last_modified', '#maj_late', 'identifiers', '#isbn', '#goodreads', '#list_remove_muse', '#onorizon', 'comments', 'title_sort', '#list_muse', 'author_sort_map', 'guide', 'thumbnail', 'formats', 'lpath', 'timestamp', 'pubdate', 'book_producer', 'user_categories', 'spine', 'mime', '#progression', '#chapters', 'cover_data', '#updated', 'title', 'application_id', 'manifest']
to_close = []
to_set_as_read = []
if oncard is None:
oncard = 'main'
elif oncard != 'carda':
log.debug("No database for '{}'".format(oncard))
raise BookeenDatabaseException(oncard)
log.debug("Matching books for {} (threshold is {})...".format(oncard, threshold))
for device_book_id, device_book in self.databases[oncard]['books'].items():
assert isinstance(device_book, BookeenBook)
found = False
for idx, calibre_book in enumerate(calibre_books):
if device_book.lpath == calibre_book.lpath:
# log.debug("Found : {} -> {}::{} ({} / {})".format(calibre_book.uuid, self.databases[oncard]['uuid'], book_id, calibre_book.title, device_book.title))
found = True
# calibre_books[idx].bookeen_id = (self.databases[oncard]['uuid'], device_book_id)
if device_book.finished:
to_set_as_read.append(calibre_book.application_id)
if not found:
log.debug("{} ({}) wasn't found in Calibre".format(device_book.title, device_book.lpath))
if threshold > 0:
# log.debug("Current page is {}, threshold is {} for {}".format(device_book.current_page, threshold, device_book))
if 0 <= device_book.current_page <= threshold:
to_close.append((self.databases[oncard]['uuid'], device_book_id))
return to_close, to_set_as_read
def close_book(self, book_id):
# uuid, bookid = book_id
# log.debug("closing book {}".format(book_id))
# with closing(sqlite3.connect(self.get_database_path(uuid))) as connection:
# log.debug("connect to db")
# connection.row_factory = dict_factory
# cursor = connection.cursor()
# cursor.execute('UPDATE T_ITEM SET f_current_page = -1, f_last_read = NULL WHERE f_id_item = {}'.format(bookid))
# connection.commit()
log.debug("Closed {} ({})".format(book_id, self.books[book_id]))
def get_database_path(self, uuid):
for database in self.databases.values():
if uuid == database['uuid']:
return database['path']
class Descriptions(object):
filetype = {
1: "Directory",
2: "File"
}
fileformat = {
0: "Directory",
1: "EPUB",
3: "JPEG"
}
class BookeenShelf(object):
def __init__(self, data):
self.name = data['f_name']
self.id = data['f_id_shelf']
self.readonly = True if data['f_readonly'] == 1 else False
self.books = []
log.debug("init'ed shelf {}".format(self.id))
def __str__(self):
return "{} - {} ({})".format(self.name, self.id, [book.title for book in self.books])
def add_book(self, book):
self.books.append(book)
def get_books(self):
return self.books
class BookeenBook(Book):
def __init__(self, prefix, lpath, title=None, authors=None, date=None, finished=False, current_page=None, size=None, other=None):
Book.__init__(self, prefix, lpath, size, other)
self.current_page = current_page
self.finished = finished
self.bookeen_id = None
self.size = size
if title is not None and len(title) > 0:
self.title = title
else:
self.title = "(No title)"
if authors is not None and len(authors) > 0:
self.authors_from_string(authors)
if self.author_sort is None or self.author_sort == "Unknown":
self.author_sort = author_to_author_sort(authors)
self.date = date
self._new_book = False
self.device_collections = []
self.path = os.path.join(prefix, lpath)
if os.sep == '\\':
self.path = self.path.replace('/', '\\')
self.lpath = lpath.replace('\\', '/')
else:
self.lpath = lpath
def __str__(self):
return "{}: {} - {}{} - {}".format(self.uuid, self.title, self.current_page if self.current_page > 0 else "Not opened", " - Finished" if self.finished else "", self.lpath)
class BookeenDatabaseException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return "{} has no database".format(self.value)