/
database.py
191 lines (158 loc) · 6.09 KB
/
database.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
#!/usr/bin/env python
# Database interactions
# -*- coding: utf_8 -*-
from pysqlite2 import dbapi2 as sqlite
import hashlib
def setup(dbname):
'''Setup the database and tables for a new profile.'''
connection = sqlite.connect(dbname)
cursor = connection.cursor()
booksTable = '''CREATE TABLE books (
id VARCHAR(56) UNIQUE NOT NULL PRIMARY KEY,
title VARCHAR(255) NULL,
author VARCHAR(100) NULL
)'''
cursor.execute(booksTable)
connection.commit()
clipsTable = '''CREATE TABLE clips (
id INTEGER UNIQUE NOT NULL PRIMARY KEY,
book VARCHAR(56) NULL,
location INTEGER NULL,
entry_header VARCHAR(255) NULL,
hash_id VARCHAR(56) NULL,
quote BLOB NULL
)'''
cursor.execute(clipsTable)
connection.commit()
cursor.close()
connection.commit()
connection.close()
def dump(db, data, line_ending):
'''
Input data into the database on a fresh parse.
table setup: id | book (book_hash_id to match book table id)* | location* | entry_header*^ | hash_id* | quote*^
* inserted by us ^ pickled object
pickle the dirty data
iterate over all the entries for each book and check the hash_id to make sure not previously inserted and if not dump it
also break up the book title into title and author and create a hash_id_book to dump as the id for that table
book table setup: book_hash_id* | title*^ | author*^
* inserted by us ^ pickled object
'''
connection = sqlite.connect(db)
cursor = connection.cursor()
connection.text_factory = str
error = []
for k in data:
book_hash_id = hashlib.sha224(k).hexdigest()
if "(" in k:
book_string = k.rpartition("(")
book_title = book_string[0]
book_author = book_string[2].rstrip(''.join([")",line_ending]))
else:
book_title = k
book_author = "NULL"
book_check = []
try:
sql = "SELECT title FROM books WHERE id=?"
cursor.execute(sql, (book_hash_id,))
book_check = cursor.fetchone()
except connection.Error, err:
e = "Error: %s" % err.args[0]
error.append(e)
if not book_check:
try:
sql = "INSERT INTO books VALUES(?,?,?)"
cursor.execute(sql, (book_hash_id, book_title, book_author,))
connection.commit()
except connection.Error, err:
e = "Error: %s" % err.args[0]
error.append(e)
the_keys = sorted(data[k].iterkeys())
for e in the_keys:
entry_header = data[k][e][1]
location = int(data[k][e][0])
hash_id = e
if len(data[k][e])>2:
quote = data[k][e][2]
else:
# should be a bookmark if empty (?)
quote = "-place holder entity-"
quote_check = []
try:
sql = "SELECT id FROM clips WHERE hash_id LIKE ?"
cursor.execute(sql, (hash_id,))
quote_check = cursor.fetchone()
except connection.Error, err:
e = "Error: %s" % err.args[0]
error.append(e)
if not quote_check:
sql = "INSERT INTO clips VALUES(null,?,?,?,?,?)"
try:
cursor.execute(sql, (book_hash_id, location, entry_header, hash_id,quote,))
connection.commit()
except connection.Error, err:
e = "Error: %s" % err.args[0]
error.append(e)
cursor.close()
connection.commit()
connection.close()
return error
class Retrieve():
'''Grab database data.'''
def __init__(self, db):
self.connection = sqlite.connect(db)
self.cursor = self.connection.cursor()
self.connection.text_factory = str
self.error = []
def books(self):
'''Get all books.'''
try:
sql = "SELECT * FROM books"
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data
except self.connection.Error, err:
e = "Error: %s" % err.args[0]
self.error.append(e)
def quotes(self, book=None):
'''Get quotes for a selected book id.'''
try:
sql = "SELECT * FROM clips WHERE book=?"
self.cursor.execute(sql, (book,))
data = self.cursor.fetchall()
return data
except self.connection.Error, err:
e = "Error: %s" % err.args[0]
self.error.append(e)
class Search():
'''Search database for string.'''
def __init__(self, db, b_id, s):
connection = sqlite.connect(db)
cursor = connection.cursor()
connection.text_factory = str
self.error = []
self.query_list = s.split(" ")
array = list(s.replace(" ", "%"))
array[:0] = "%"
array.append("%")
query_string = "".join(array)
try:
sql = "SELECT * FROM clips WHERE quote LIKE ? and book=?"
cursor.execute(sql, (query_string, b_id,))
self.clips = cursor.fetchall()
except connection.Error, err:
e = "Error: %s" % err.args[0]
self.error.append(e)
self.book_ids = []
for c in self.clips:
if c[1] not in self.book_ids:
self.book_ids.append(c[1])
try:
self.books = []
for i in self.book_ids:
sql = "SELECT * FROM books WHERE id=?"
cursor.execute(sql, (i,))
self.books.append(cursor.fetchone())
except connection.Error, err:
e = "Error: %s" % err.args[0]
self.error.append(e)