-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeluHelu_Server.py
137 lines (127 loc) · 4.37 KB
/
HeluHelu_Server.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
import sqlite3
import Crypto.Random
import Crypto.Protocol.KDF
import urllib.request
import xml.etree.ElementTree
class Book():
pass
class BookShelf():
shelf = {}
def create(conn, books_string):
curs = conn.cursor()
split_string = books_string.split(';')
for id in split_string:
result1 = curs.execute('''SELECT * FROM books
WHERE book_id=?''', (id,))
book = Book()
book.book_id = id
book.title = result1.fetchone()[1]
book.author = result1.fetchone()[2]
book.year = result1.fetchone()[3]
book.image = result1.fetchone()[4]
self.shelf[book.book_id] = book
def add(book):
if book.book_id in self.shelf:
return False
else:
self.shelf[book.book_id] = book
return True
def remove(book):
if book.book_id in self.shelf:
del self.shelf[book.book_id]
return True
else:
return False
class Cache():
cache = {}
def add(self, keywords, results_list):
self.cache[keywords] = results_list
def check(self, keywords):
if keywords in self.cache:
return self.cache[keywords]
else:
return None
def add_user(conn, username, password):
curs = conn.cursor()
salt = Crypto.Random.get_random_bytes(8)
hash = Crypto.Protocol.KDF.PBKDF2(password, salt)
print('\nADDING...')
print(username)
print(salt.hex())
print(hash.hex())
curs.execute('INSERT INTO users VALUES (?,?,?,?,?)',
(username, salt.hex(), hash.hex(), ' ', ' '))
conn.commit()
def authenticate_user(conn, username, password):
curs = conn.cursor()
results = curs.execute('SELECT salt, hash FROM users WHERE username=?',
(username,))
row = results.fetchone()
salt = bytes.fromhex(row[0])
hash = bytes.fromhex(row[1])
if hash == Crypto.Protocol.KDF.PBKDF2(password, salt):
return True
else:
return False
def get_books(conn, username):
curs = conn.cursor()
results = curs.execute(
'SELECT books FROM users WHERE username=?',
(username,))
return results.fetchone()[0]
def get_favorites(curs, username):
curs = conn.cursor()
results = curs.execute(
'SELECT favorites FROM users WHERE username=?',
(username,))
return results.fetchone()[0].split(';')
def search_books(api_key, keywords, cache):
cached_results = cache.check(keywords)
if cached_results:
return cached_results
else:
prefix = 'https://www.goodreads.com/search.xml?key='
url_keywords = keywords.replace(' ', '+')
with urllib.request.urlopen(prefix +
api_key +
'&q=' +
url_keywords) as response:
results_root = xml.etree.ElementTree.fromstring(response.read())
results_list = []
for work in results_root[1][6]:
book = Book()
book.book_id = work[0].text
book.title = work[8][1].text
book.author = work[8][2][1].text
book.year = work[4].text
book.image = work[8][3].text
results_list.append(book)
cache.add(keywords, results_list[:5])
return results_list[:5]
def add_book(conn, username, book):
curs = conn.cursor()
results = curs.execute(
'SELECT books FROM users WHERE username=?',
(username,))
old_list = results.fetchone()[0]
new_list = book.title + ';' + old_list
curs.execute('UPDATE users SET books=? WHERE username=?',
(new_list, username))
book_count = curs.execute(
'SELECT count FROM books WHERE book_id=?',
(book.book_id,)).fetchone()
if book_count:
curs.execute('UPDATE books SET count=? WHERE book_id=?',
(book_count[0] + 1, book.book_id))
else:
curs.execute(
'''INSERT INTO books VALUES (?,?,?,?,?,?,?)''',
(book.book_id, book.title, book.author,
book.year, book.image, 1, 0))
conn.commit()
# MAIN
api_key = open('api_key.txt', 'r').read()[:-1]
Crypto.Random.new()
db = input('enter database filename: ')
conn = sqlite3.connect(db)
cache = Cache()