summaryrefslogtreecommitdiff
path: root/web.py
diff options
context:
space:
mode:
Diffstat (limited to 'web.py')
-rw-r--r--web.py46
1 files changed, 38 insertions, 8 deletions
diff --git a/web.py b/web.py
index 429c516..b747010 100644
--- a/web.py
+++ b/web.py
@@ -7,16 +7,45 @@ import whoosh.analysis as analysis
from whoosh import highlight
import flask
from flask import Flask
-
+import pyPdf
+from StringIO import StringIO
+import werkzeug
app = Flask("booksearch")
index = open_dir(u"index", mapped=False)
searcher = index.searcher()
+@app.route("/")
+def do_index():
+ return flask.redirect(flask.url_for("do_search",term=""))
+
+@app.route("/download/file/<int:docnum>")
+def do_download_file(docnum):
+ document = index.reader().stored_fields(docnum)
+ filepath = document['path']
+ return werkzeug.Response(open(filepath, "r"), mimetype="application/pdf")
+
+@app.route("/download/page/<int:docnum>", methods=["GET"])
+def do_download_page(docnum):
+ document = index.reader().stored_fields(docnum)
+ filepath = document['path']
+ pagenumber = document['pagenumber']
+ inputfile = pyPdf.PdfFileReader(file(filepath, 'r'))
+ page = inputfile.getPage(pagenumber)
+ outbuf = StringIO()
+ outfile = pyPdf.PdfFileWriter()
+ outfile.addPage(page)
+ outfile.write(outbuf)
+ outbuf.seek(0)
+ return werkzeug.Response(outbuf, mimetype="application/pdf")
@app.route("/search/skip=<int:skip>/<path:term>",methods=["GET"])
@app.route("/search/<path:term>",methods=["GET"])
+@app.route("/search/", methods=["GET"])
def do_search(skip=0,term=None):
+ if skip == 0 and not term:
+ return flask.render_template('search.html', objects=[], term="", skip=0)
+
query = QueryParser("content").parse(term)
results = searcher.search(query, limit=skip+5)
@@ -26,14 +55,15 @@ def do_search(skip=0,term=None):
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)
+ print path
+ high = highlight.highlight(result.get("content"),
+ terms,
+ analysis.StandardAnalyzer(),
+ highlight.SimpleFragmenter(),
+ highlight.HtmlFormatter())
+ objects.append({ 'title' : title, 'path' : path, 'excerpt' : high, 'docnum':result.docnum })
+ return flask.render_template('search.html', objects=objects, term=term, skip=skip)
if __name__ == "__main__":
app.debug = True