webapp.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 engines
  23. import grequests
  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. def default_request_params():
  32. return {'method': 'GET', 'headers': {}, 'data': {}, 'url': ''}
  33. def make_callback(results, callback):
  34. def process_callback(response, **kwargs):
  35. results.extend(callback(response))
  36. return process_callback
  37. @app.route('/', methods=['GET', 'POST'])
  38. def index():
  39. if request.method=='POST':
  40. if not request.form.get('q'):
  41. flash('Wrong post data')
  42. return render_template('index.html')
  43. query = request.form['q']
  44. requests = []
  45. results = []
  46. user_agent = request.headers.get('User-Agent', '')
  47. for engine in engines:
  48. headers = default_request_params()
  49. headers['User-Agent'] = user_agent
  50. request_params = engine.request(query, headers)
  51. callback = make_callback(results, engine.response)
  52. if request_params['method'] == 'GET':
  53. req = grequests.get(request_params['url']
  54. ,headers=headers
  55. ,hooks=dict(response=callback)
  56. )
  57. else:
  58. req = grequests.post(request_params['url']
  59. ,data=request_params['data']
  60. ,headers=headers
  61. ,hooks=dict(response=callback)
  62. )
  63. requests.append(req)
  64. grequests.map(requests)
  65. return render_template('results.html', results=results, q=query)
  66. return render_template('index.html')
  67. if __name__ == "__main__":
  68. from gevent import monkey
  69. monkey.patch_all()
  70. app.run(debug = cfg.get('server', 'debug')
  71. ,use_debugger = cfg.get('server', 'debug')
  72. ,port = int(cfg.get('server', 'port'))
  73. )