-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
155 lines (117 loc) · 4.29 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
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
import os, random
from flask import Flask, redirect, render_template, request, url_for
import sqlite3
from Searcher import Searcher
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'upload')
def get_article(id):
'''
Get specified article from the database.
Input: `id`: id of the article
Output: article infomation
'''
sql_text = "SELECT * FROM MSN_TECH WHERE _id='{}'".format(id)
result = list(db_cursor.execute(sql_text))[0]
info = {
'id': result[0],
'url': result[1],
'title': result[2],
'author': result[3],
'date': result[4],
'article': result[5],
'html': result[6],
'images': eval(result[7]),
'related': eval(result[8]),
}
return info
def search_handle(request, domain):
'''
Handle search redirects and file uploads for webpages.
Input: `request`: `request` variable received
`domain`: search domain
'''
try:
keyword = request.form['keyword']
return redirect(url_for(domain + '_search', keyword=keyword))
except:
pass
try:
file = request.files['file']
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('visual_search', keyword=filename))
except:
pass
def render_main(request, template, domain):
'''
Render 'main' page for website and image search.
Input: `request`: `request` variable received
`template`: template HTML file
`domain`: search domain
'''
if request.method == "POST":
return search_handle(request, domain)
return render_template(template)
def render_results(request, template, domain):
'''
Render 'results' page for website and image search.
Input: `request`: `request` variable received
`template`: template HTML file
`domain`: search domain
'''
if request.method == "POST":
return search_handle(request, domain)
keyword = request.args.get('keyword')
sorting = '_score'
if request.args.get('sorting'):
sorting = request.args.get('sorting')
try:
results = searcher.search(keyword, domain, sorting)
return render_template(template, keyword=keyword, results=results)
except RuntimeError as e:
return redirect(url_for('visual', error=e))
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == "POST":
return search_handle(request, 'web')
main = random.sample(range(7360), 3)
content = list(map(lambda x: get_article(x), main))
return render_template('index.html', content=content)
@app.route('/article', methods=['POST', 'GET'])
def article():
if request.method == "POST":
return search_handle(request, 'web')
id = request.args.get('id')
content = get_article(id)
if -1 in content['related']:
content['related'] = content['related'][:content['related'].index(-1)]
content['related'] = list(map(lambda x: get_article(x), content['related']))
return render_template("article.html", content=content)
@app.route('/web', methods=['POST', 'GET'])
def web():
return render_main(request, 'web.html', 'web')
@app.route('/web/search', methods=['POST', 'GET'])
def web_search():
return render_results(request, 'web_results.html', 'web')
@app.route('/images', methods=['POST', 'GET'])
def images():
return render_main(request, 'images.html', 'images')
@app.route('/images/search', methods=['POST', 'GET'])
def images_search():
return render_results(request, 'images_results.html', 'images')
@app.route('/visual', methods=['POST', 'GET'])
def visual():
if request.method == "POST":
return search_handle(request, 'image')
error = request.args.get('error')
return render_template('visual.html', error=error)
@app.route('/visual/search', methods=['POST', 'GET'])
def visual_search():
return render_results(request, 'visual_results.html', 'visual')
searcher = None # To be initialized in __main__
db_cursor = None # To be initialized in __main__
if __name__ == '__main__':
searcher = Searcher('localhost')
db = sqlite3.connect('data/MSN_technology.db', check_same_thread = False)
db_cursor = db.cursor()
app.run(debug=True, port=8080)