summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryvesf <yvesf-git@xapek.org>2010-11-20 01:16:56 +0100
committeryvesf <yvesf-git@xapek.org>2010-11-20 01:16:56 +0100
commit0ae1e5e802871903d73d6542252aa0a8d08fba39 (patch)
tree06b0ef2e53177f724f3f141576364622fdaf1698
parent6c91093cacd0e4ebcdfd1bca7c79dc906527b94c (diff)
downloadbooksearch-0ae1e5e802871903d73d6542252aa0a8d08fba39.tar.gz
booksearch-0ae1e5e802871903d73d6542252aa0a8d08fba39.zip
web stuff
-rw-r--r--templates/search.html30
-rw-r--r--web.py40
2 files changed, 70 insertions, 0 deletions
diff --git a/templates/search.html b/templates/search.html
new file mode 100644
index 0000000..e6bfe07
--- /dev/null
+++ b/templates/search.html
@@ -0,0 +1,30 @@
+<html>
+ <head>
+ <title>{{ objects.__len__() + skip}} matches</title>
+ </head>
+ <body>
+ {% if objects.__len__() == 0 %}
+ No Matches
+ {% else %}
+ {% for obj in objects %}
+ <hr />
+ <h2> {{ obj['title'] }} </h2>
+ <pre> {{ obj['path'] }} </pre>
+ {% autoescape false %}
+ <div>{{ obj['excerpt'] }}</div>
+ {% endautoescape %}
+ <hr />
+ {% endfor %}
+ {% endif %}
+
+ <div>
+ {% if skip > 0 %}
+ <a href="{{ url_for("do_search", term=term, skip=skip-5) }}">Previous 5</a>
+ {% else %}
+ Previous 5
+ {% endif %}
+ -
+ <a href="{{ url_for("do_search", term=term, skip=skip+5) }}">Next 5</a>
+ </div>
+ </body>
+</html>
diff --git a/web.py b/web.py
new file mode 100644
index 0000000..429c516
--- /dev/null
+++ b/web.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python2.6
+# coding: utf-8
+from whoosh.index import open_dir
+from whoosh.qparser import QueryParser
+import whoosh.fields as fields
+import whoosh.analysis as analysis
+from whoosh import highlight
+import flask
+from flask import Flask
+
+app = Flask("booksearch")
+
+index = open_dir(u"index", mapped=False)
+searcher = index.searcher()
+
+
+@app.route("/search/skip=<int:skip>/<path:term>",methods=["GET"])
+@app.route("/search/<path:term>",methods=["GET"])
+def do_search(skip=0,term=None):
+ query = QueryParser("content").parse(term)
+ results = searcher.search(query, limit=skip+5)
+
+ terms = [text for fieldname, text in query.all_terms()
+ if fieldname == "content"]
+ objects = []
+ for result in results[skip:skip+5]:
+ title = result.get("title")
+ path = result.get("path")
+# high = highlight.highlight(result.get("content"),
+# terms,
+# analysis.StandardAnalyzer(),
+# highlight.SimpleFragmenter(),
+# highlight.HtmlFormatter())
+ objects.append({ 'title' : title, 'path' : path, 'excerpt' : 'TODO' })
+ return flask.render_template('search.html', objects=objects, term=term, skip=skip)
+
+
+if __name__ == "__main__":
+ app.debug = True
+ app.run(host="0.0.0.0")