-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
65 lines (53 loc) · 1.53 KB
/
app.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
from flask import Flask, request, render_template, redirect, url_for
from goodreads import client
import os
gc = client.GoodreadsClient(os.environ.get('GCLIENT_TOKEN'),os.environ.get('GCLIENT_KEY'))
app = Flask(__name__)
links = []
@app.route('/')
def form():
return render_template('form.html')
@app.route('/search/ID/<idnum>')
def display_booksById(idnum):
return render_template('index.html', len = 7, links=links)
@app.route('/search/ISBN/<isbn>')
def display_booksByIsbn(isbn):
return render_template('index.html', len = 7, links=links)
@app.route('/', methods=['POST', 'GET'])
def form_post():
global links
links=[]
try:
links,book_obj,query_num = get_search_tag()
links.insert(0,book_obj)
print(type(links[0]))
if search_by_id() == True:
return redirect(url_for('display_booksById', idnum=query_num, len=7))
else:
return redirect(url_for('display_booksByIsbn', isbn=query_num, len=7))
except KeyError:
return render_template('notFound.html')
except IndexError:
return render_template('indexError.html')
except:
return render_template('404.html')
def get_search_tag():
global links
user_input = request.form['text']
query_num = user_input.strip()
if search_by_id() == True:
book_obj = gc.book(query_num)
else:
book_obj = gc.book(isbn=query_num)
print(book_obj.average_rating)
links = book_obj.similar_books[:7]
return links,book_obj,query_num
def search_by_id():
if request.form['userChoice'] == 'isbn_no':
return False
else:
return True
def main():
app.run()
if __name__=="__main__":
main()