webapp.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  20. import ConfigParser
  21. from os import getenv
  22. from searx.engines import search, engines
  23. cfg = ConfigParser.SafeConfigParser()
  24. cfg.read('/etc/searx.conf')
  25. cfg.read(getenv('HOME')+'/.searxrc')
  26. cfg.read(getenv('HOME')+'/.config/searx/searx.conf')
  27. cfg.read('searx.conf')
  28. app = Flask(__name__)
  29. app.secret_key = cfg.get('app', 'secret_key')
  30. def render(template_name, **kwargs):
  31. kwargs['engines'] = engines.keys()
  32. return render_template(template_name, **kwargs)
  33. @app.route('/', methods=['GET', 'POST'])
  34. def index():
  35. if request.method=='POST':
  36. if not request.form.get('q'):
  37. flash('Wrong post data')
  38. return render('index.html')
  39. selected_engines = []
  40. for pd_name,pd in request.form.items():
  41. if pd_name.startswith('engine_'):
  42. selected_engines.append(pd_name[7:])
  43. query = request.form['q'].encode('utf-8')
  44. results = search(query, request, selected_engines)
  45. return render('results.html', results=results, q=query.decode('utf-8'))
  46. return render('index.html')
  47. if __name__ == "__main__":
  48. from gevent import monkey
  49. monkey.patch_all()
  50. app.run(debug = cfg.get('server', 'debug')
  51. ,use_debugger = cfg.get('server', 'debug')
  52. ,port = int(cfg.get('server', 'port'))
  53. )