summaryrefslogtreecommitdiff
path: root/searx
diff options
context:
space:
mode:
authorasciimoo <asciimoo@gmail.com>2013-10-14 23:09:13 +0200
committerasciimoo <asciimoo@gmail.com>2013-10-14 23:09:13 +0200
commitae9fb1d7dccf061032b5d4b167086838feb1463d (patch)
tree76cc836e07f9e14fd9f661660ea96bafcf92fa9c /searx
downloadsearxng-ae9fb1d7dccf061032b5d4b167086838feb1463d.tar.gz
searxng-ae9fb1d7dccf061032b5d4b167086838feb1463d.zip
[enh] initial commit
Diffstat (limited to 'searx')
-rw-r--r--searx/__init__.py0
-rw-r--r--searx/engines/__init__.py15
-rw-r--r--searx/engines/duckduckgo.py14
-rw-r--r--searx/static/css/style.css0
-rw-r--r--searx/templates/base.html17
-rw-r--r--searx/templates/index.html8
-rw-r--r--searx/templates/results.html7
-rw-r--r--searx/webapp.py69
8 files changed, 130 insertions, 0 deletions
diff --git a/searx/__init__.py b/searx/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/searx/__init__.py
diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py
new file mode 100644
index 000000000..34d88b749
--- /dev/null
+++ b/searx/engines/__init__.py
@@ -0,0 +1,15 @@
+
+from os.path import realpath, dirname, splitext, join
+from os import listdir
+from imp import load_source
+
+engine_dir = dirname(realpath(__file__))
+
+engines = []
+
+for filename in listdir(engine_dir):
+ modname = splitext(filename)[0]
+ if filename.startswith('_') or not filename.endswith('.py'):
+ continue
+ filepath = join(engine_dir, filename)
+ engines.append(load_source(modname, filepath))
diff --git a/searx/engines/duckduckgo.py b/searx/engines/duckduckgo.py
new file mode 100644
index 000000000..ed93829ba
--- /dev/null
+++ b/searx/engines/duckduckgo.py
@@ -0,0 +1,14 @@
+from lxml import html
+
+
+def request(query, params):
+ params['method'] = 'POST'
+ params['url'] = 'https://duckduckgo.com/html'
+ params['data']['q'] = query
+ return params
+
+
+def response(resp):
+ dom = html.fromstring(resp.text)
+ results = dom.xpath('//div[@class="results_links results_links_deep web-result"]')
+ return [html.tostring(x) for x in results]
diff --git a/searx/static/css/style.css b/searx/static/css/style.css
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/searx/static/css/style.css
diff --git a/searx/templates/base.html b/searx/templates/base.html
new file mode 100644
index 000000000..b1b051fad
--- /dev/null
+++ b/searx/templates/base.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <meta http-equiv="content-language" content="en" />
+ <meta name="description" content="" />
+ <meta name="keywords" content="" />
+ <title>searx {% block title %}{% endblock %}</title>
+ <link rel="stylesheet" href="/static/css/style.css" type="text/css" media="screen" charset="utf-8" />
+ {% block styles %}
+ {% endblock %}
+</head>
+<body>
+{% block content %}
+{% endblock %}
+</body>
+</html>
diff --git a/searx/templates/index.html b/searx/templates/index.html
new file mode 100644
index 000000000..2905cbce2
--- /dev/null
+++ b/searx/templates/index.html
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+{% block content %}
+<h1>searx</h1>
+<form method="post" action="">
+ <input type="text" name="q" />
+ <input type="submit" value="search" />
+</form>
+{% endblock %}
diff --git a/searx/templates/results.html b/searx/templates/results.html
new file mode 100644
index 000000000..c7851461a
--- /dev/null
+++ b/searx/templates/results.html
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+{% block content %}
+<h1>searx</h1>
+{% for result in results %}
+ <p>{{ result|safe }}</p>
+{% endfor %}
+{% endblock %}
diff --git a/searx/webapp.py b/searx/webapp.py
new file mode 100644
index 000000000..7c859d5d4
--- /dev/null
+++ b/searx/webapp.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+
+if __name__ == "__main__":
+ from sys import path
+ from os.path import realpath, dirname
+ path.append(realpath(dirname(realpath(__file__))+'/../'))
+
+from flask import Flask, request, flash, render_template
+import ConfigParser
+from os import getenv
+from searx.engines import engines
+import grequests
+
+cfg = ConfigParser.SafeConfigParser()
+cfg.read('/etc/searx.conf')
+cfg.read(getenv('HOME')+'/.searxrc')
+cfg.read(getenv('HOME')+'/.config/searx/searx.conf')
+cfg.read('searx.conf')
+
+
+app = Flask(__name__)
+app.secret_key = cfg.get('app', 'secret_key')
+
+def default_request_params():
+ return {'method': 'GET', 'headers': {}, 'data': {}, 'url': ''}
+
+def make_callback(results, callback):
+ def process_callback(response, **kwargs):
+ results.extend(callback(response))
+ return process_callback
+
+@app.route('/', methods=['GET', 'POST'])
+def index():
+ if request.method=='POST':
+ if not request.form.get('q'):
+ flash('Wrong post data')
+ return render_template('index.html')
+ query = request.form['q']
+ requests = []
+ results = []
+ for engine in engines:
+ request_params = engine.request(query, default_request_params())
+ callback = make_callback(results, engine.response)
+ if request_params['method'] == 'GET':
+ req = grequests.get(request_params['url']
+ ,headers=request_params['headers']
+ ,hooks=dict(response=callback)
+ )
+ else:
+ req = grequests.post(request_params['url']
+ ,data=request_params['data']
+ ,headers=request_params['headers']
+ ,hooks=dict(response=callback)
+ )
+ requests.append(req)
+ grequests.map(requests)
+ return render_template('results.html', results=results)
+
+
+ return render_template('index.html')
+
+if __name__ == "__main__":
+ from gevent import monkey
+ monkey.patch_all()
+
+ app.run(debug = cfg.get('server', 'debug')
+ ,use_debugger = cfg.get('server', 'debug')
+ ,port = int(cfg.get('server', 'port'))
+ )