-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_search.py
executable file
·207 lines (165 loc) · 6.07 KB
/
web_search.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2014-2015 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
from flask import Flask, request, Response
import html
import time
import sys
import urllib.parse
import datetime
from flask_cors import CORS
sys.path.append("models/")
from pagination import Pagination
from glossary import Glossary
from stats import Stats
from search import Search
from usage import Usage
app = Flask(__name__)
CORS(app)
class SearchAPIResults(object):
def _get_result_text(self, result, key):
highlighted = result.highlights(key)
if highlighted is not None and len(highlighted) > 0:
return highlighted
return html.escape(result[key])
def get_result(self, result):
result_dict = {
"source": self._get_result_text(result, "source"),
"target": self._get_result_text(result, "target"),
"project": result["project"],
"comment": None,
"context": None,
}
if (
"comment" in result.fields()
and result["comment"] is not None
and len(result["comment"]) > 0
):
# Comments can be multi-line because they contain multiple lines or
# because we concatenated tcomments with comments from the PO. So
# it is necessary to adapt it to properly integrate into HTML.
comment = (
html.escape(result["comment"]).replace("\n", "<br />").replace("\r", "")
)
result_dict["comment"] = comment
if (
"context" in result.fields()
and result["context"] is not None
and len(result["context"]) > 0
):
result_dict["context"] = html.escape(result["context"])
return result_dict
def do(self, search, page):
"""Search a term in the Whoosh index."""
aborted_search = False
results = []
num_results = 0
total_time = 0
PER_PAGE = 100
start_time = time.time()
if search.has_invalid_search_term:
aborted_search = True
pagination = None
glossary = None
pages = 0
else:
g = Glossary(search.source)
g.search()
glossary = g.get_results()
raw_results = search.get_results()
num_results = raw_results.scored_length()
if len(raw_results) > 0:
url = request.url
o = urllib.parse.urlparse(url)
url = "?" + o.query
pagination = Pagination(PER_PAGE, len(raw_results), url, page)
start = (pagination.page - 1) * PER_PAGE
end = start
max_end = start + PER_PAGE
if num_results - start < max_end:
end += num_results - start
else:
end += PER_PAGE
for i in range(start, end):
results.append(self.get_result(raw_results[i]))
pages = pagination.pages
else:
pagination = None
pages = 0
total_time = time.time() - start_time
ctx = {
"source": search.source,
"target": search.target,
"project": search.project,
"num_results": num_results,
"time": "{:.2f}".format(total_time),
"aborted_search": aborted_search,
"glossary": glossary,
"pages": pages,
"results": results,
}
return ctx
@app.route("/search", methods=["GET"])
def search_api():
source = request.args.get("source")
target = request.args.get("target")
page = request.args.get("page")
project = ",".join(request.args.getlist("project"))
search = Search(source, target, project)
results = SearchAPIResults()
json = results.do(search, page)
usage = Usage()
usage.log()
return json
@app.route("/glossary/search", methods=["GET"])
def glossary_search_api():
source = request.args.get("source")
glossary = Glossary(source)
glossary.search()
return Response(glossary.get_json(), mimetype="application/json")
@app.route("/memory/search", methods=["GET"])
def memory_search_api():
source = request.args.get("source")
target = request.args.get("target")
project = request.args.get("project")
search = Search(source, target, project)
return Response(search.get_json(), mimetype="application/json")
@app.route("/stats/", methods=["GET"])
def stats_api():
try:
requested = request.args.get("date")
date_requested = datetime.datetime.strptime(requested, "%Y-%m-%d")
except Exception:
return Response({}, mimetype="application/json", status=400)
stats = Stats()
return Response(stats.get_json(date_requested), mimetype="application/json")
@app.route("/projects", methods=["GET"])
def projects_api():
with open("projects.json", "r") as file:
projects = file.read()
return Response(projects, mimetype="application/json")
@app.route("/index", methods=["GET"])
def index_api():
with open("index.json", "r") as file:
projects = file.read()
return Response(projects, mimetype="application/json")
if __name__ == "__main__":
app.debug = True
app.run()