forked from ankur-b/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (164 loc) · 6.47 KB
/
main.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
from flask import Flask, render_template,request,redirect,url_for,session
import sqlite3
import goodreads_api_client as gr
import os
import requests
from xml.etree import ElementTree
import shutil
current = ""
adminCh = False
DevKey = 'UvqPf2fGbH2YoWaetp1bA'
client = gr.Client(developer_key = DevKey)
app = Flask(__name__)
app.secret_key = '\xf0\xa5\x9ewe6\x82RU\x8b\t\x0b\xb6\xcc\xf8\xb2\xdb\x02\x83\xab\x0f\x13\x15'
conn = sqlite3.connect("Users.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS users (first_name text NOT NULL,last_name text NOT NULL,email text PRIMARY KEY NOT NULL, password text NOT NULL, username text NOT NULL)")
c.close()
conn.close()
@app.route('/',methods=['GET', 'POST'])
def index():
conn = sqlite3.connect("Users.db")
c = conn.cursor()
if request.method == 'POST':
if request.form.get('first_name'):
fname = request.form['first_name']
lname = request.form['last_name']
email = request.form['email']
passw = request.form['password']
cpass = request.form['cpassword']
uname = request.form['username']
t = (email.lower(),)
c.execute("SELECT email FROM users WHERE email = ?", t)
checkEmail = c.fetchone()
if checkEmail != None:
c.close()
conn.close()
return render_template('signup.html', error = "Email already in use")
t = (uname.lower(),)
c.execute("SELECT username FROM users WHERE username = ?", t)
checkUser = c.fetchone()
if checkUser != None:
c.close()
conn.close()
return render_template('signup.html', error = "Username already in use")
c.execute("INSERT INTO users VALUES(?,?,?,?,?)",(fname,lname,email,passw,uname))
conn.commit()
c.close()
conn.close()
return redirect(url_for('signin'))
if request.form.get('searchB'):
searchB = request.form['searchB']
searchB = list(searchB.split())
qu = 'https://www.goodreads.com/search/index.xml?key='+DevKey+'&q='
for i in searchB:
qu = qu + i + "+"
qu = qu.rstrip("+")
response = requests.get(qu)
tree = ElementTree.fromstring(response.content)
noBooks = (tree[1][2].text)
if int(noBooks) == 0:
c.close()
conn.close()
return redirect(url_for('noResults'))
totList = {}
for i in range(int(noBooks)):
id = str(tree[1][6][i][-0].text)
title = tree[1][6][i][-1][1].text
author = tree[1][6][i][-1][2][1].text
imgurl = tree[1][6][i][-1][3].text
simgurl = tree[1][6][i][-1][4].text
dict = {}
dict['title'] = title
dict['author'] = author
dict['imgurl'] = imgurl
dict['simgurl'] = simgurl
totList[i] = dict
searchB = ' '.join(searchB)
session['listRes'] = totList
return redirect(url_for('result', totList = totList, sear = searchB))
c.close()
conn.close()
if current != '':
return render_template('user.html', username = current)
return render_template('signup.html', error = '')
@app.route('/signin' ,methods=['GET','POST'])
def signin():
conn = sqlite3.connect("Users.db")
c = conn.cursor()
global current
if request.method == 'POST':
emai = request.form['email']
passc = request.form['password']
if emai == "[email protected]" and passc == "abhi1234":
c.close()
conn.close()
global adminCh
adminCh = True
return redirect(url_for('admin'))
else:
t = (emai.lower(),)
c.execute("SELECT email, username, password FROM users WHERE email = ?",t)
checkLogin = c.fetchone()
if checkLogin == None:
c.execute("SELECT email, username, password FROM users WHERE lower(username) = ?",t)
checkLogin = c.fetchone()
if checkLogin != None:
if checkLogin[2] == passc:
c.close()
conn.close()
current = checkLogin[1]
session['logged_in'] = True
session['username'] = checkLogin[0]
return redirect(url_for('index'))
else:
c.close()
conn.close()
return render_template('signin.html', error = "Username and password do not match.")
c.close()
conn.close()
return render_template('signin.html', error = "Username or Email address doesn't exist. Signup first.")
if checkLogin[2] == passc:
c.close()
conn.close()
current = checkLogin[1]
session['logged_in'] = True
session['username'] = checkLogin[0]
return redirect(url_for('index'))
c.close()
conn.close()
return render_template('signin.html', error = "Email and password do not match.")
if current != '':
c.close()
conn.close()
return redirect(url_for('index'))
c.close()
conn.close()
return render_template('signin.html', error = '')
@app.route('/admin')
def admin():
global admin
if adminCh == True:
return render_template('admin.html', username="admin")
else:
return redirect(url_for('signin'))
@app.route('/result', methods = ['GET'])
def result():
if request.method == 'GET':
listRes = session['listRes']
sear = request.args['sear']
return render_template('searchResults.html', sear = sear, listRes = listRes, username = current)
@app.route('/noResults')
def noResults():
return render_template('noResults.html')
@app.route('/logout')
def logout():
session.pop('username', None)
session['logged_in'] = False
global adminCh
global current
adminCh = False
current = ''
return redirect(url_for('signin'))
if __name__ == '__main__':
app.run(debug = True)