webapp.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. '''
  3. searx is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. searx is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  13. (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
  14. '''
  15. if __name__ == "__main__":
  16. from sys import path
  17. from os.path import realpath, dirname
  18. path.append(realpath(dirname(realpath(__file__))+'/../'))
  19. from flask import Flask, request, flash, render_template, url_for, Response
  20. import ConfigParser
  21. from os import getenv
  22. from searx.engines import search, engines
  23. import json
  24. cfg = ConfigParser.SafeConfigParser()
  25. cfg.read('/etc/searx.conf')
  26. cfg.read(getenv('HOME')+'/.searxrc')
  27. cfg.read(getenv('HOME')+'/.config/searx/searx.conf')
  28. cfg.read('searx.conf')
  29. app = Flask(__name__)
  30. app.secret_key = cfg.get('app', 'secret_key')
  31. opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
  32. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  33. <ShortName>searx</ShortName>
  34. <Description>Search searx</Description>
  35. <InputEncoding>UTF-8</InputEncoding>
  36. <LongName>searx meta search engine</LongName>
  37. <Url type="text/html" method="post" template="{host}">
  38. <Param name="q" value="{{searchTerms}}" />
  39. </Url>
  40. </OpenSearchDescription>
  41. '''
  42. def render(template_name, **kwargs):
  43. kwargs['engines'] = engines.keys()
  44. return render_template(template_name, **kwargs)
  45. @app.route('/', methods=['GET', 'POST'])
  46. def index():
  47. if request.method=='POST':
  48. if not request.form.get('q'):
  49. flash('Wrong post data')
  50. return render('index.html')
  51. selected_engines = []
  52. for pd_name,pd in request.form.items():
  53. if pd_name.startswith('engine_'):
  54. selected_engines.append(pd_name[7:])
  55. if not len(selected_engines):
  56. selected_engines = engines.keys()
  57. query = request.form['q'].encode('utf-8')
  58. results = search(query, request, selected_engines)
  59. if request.form.get('format') == 'json':
  60. # TODO HTTP headers
  61. return json.dumps({'query': query, 'results': results})
  62. return render('results.html', results=results, q=query.decode('utf-8'))
  63. return render('index.html')
  64. @app.route('/favicon.ico', methods=['GET'])
  65. def fav():
  66. return ''
  67. @app.route('/opensearch.xml', methods=['GET'])
  68. def opensearch():
  69. global opensearch_xml
  70. ret = opensearch_xml.format(host=url_for('index', _external=True))
  71. resp = Response(response=ret,
  72. status=200,
  73. mimetype="application/xml")
  74. return resp
  75. if __name__ == "__main__":
  76. from gevent import monkey
  77. monkey.patch_all()
  78. app.run(debug = cfg.get('server', 'debug')
  79. ,use_debugger = cfg.get('server', 'debug')
  80. ,port = int(cfg.get('server', 'port'))
  81. )