-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
79 lines (60 loc) · 2.88 KB
/
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
import os, glob, json, re
from flask import Flask, url_for, request, jsonify, Response
from Book import Book
from os import listdir
from bs4 import BeautifulSoup
from urllib2 import urlopen
#from flask.ext.login import LoginManager, current_user, login_user, logout_user
app = Flask(__name__)
app.debug = True
URLHeader = "http://2466427d.ngrok.io"
#login_manager = LoginManager()
#login_manager.init_app(app)
#Return files under ChallengeBooks/ direction
booksList = os.listdir("static/ChallengeBooks/")
booksImageList = os.listdir("static/BooksImages/")
@app.route('/books')
def getBooksJson():
jsonarr = []
for index in range(len(booksList)):
#Filter html file under the director
if booksList[index].endswith(".html"):
book = BeautifulSoup(open('static/ChallengeBooks/'+booksList[index]), "html.parser")
bookName = book.title.string
authorName = book.h4.find_next(re.compile('^h')).string
bookURL = URLHeader+url_for('static', filename='ChallengeBooks/')+booksList[index]
bookImageURL = URLHeader+url_for('static', filename='BooksImages/')+booksImageList[index]
i = Book(index, bookName, authorName, bookURL, bookImageURL)
jsonarr.append({'bookId':i.getBookId(), 'bookName':i.getBookName(), 'authorName':i.getAuthorName(),
'bookURL':i.getBookURL(), 'bookImageURL':i.getBookImageURL()})
#Turn jsonarr into json object
return json.dumps(jsonarr)
@app.route('/search', methods = ['POST'])
def getSearchResultJson():
query = request.form['query']
jsonarr = []
for index in range(len(booksList)):
#Filter html file under the director
if booksList[index].endswith(".html"):
book = BeautifulSoup(open('static/ChallengeBooks/'+booksList[index]), "html.parser")
bookName = book.title.string
authorName = book.h4.find_next(re.compile('^h')).string
bookURL = URLHeader+url_for('static', filename='ChallengeBooks/')+booksList[index]
bookImageURL = URLHeader+url_for('static', filename='BooksImages/')+booksImageList[index]
i = Book(index, bookName, authorName, bookURL, bookImageURL)
if query in i.getBookName().lower() or query in i.getAuthorName().lower():
jsonarr.append({'bookId':i.getBookId(), 'bookName':i.getBookName(), 'authorName':i.getAuthorName(),
'bookURL':i.getBookURL(), 'bookImageURL':i.getBookImageURL()})
#Turn jsonarr into json object
return json.dumps(jsonarr)
@app.route('/login', methods = ['POST'])
def login():
username = request.form['username']
password = request.form['password']
if username != 'jess' or password != '123':
abort(403)
else:
return Response(status=200, \
mimetype="application/json")
if __name__ == "__main__":
app.run (host = 'localhost', port = 8080)