[enh] opensearch/rss support part I.

This commit is contained in:
asciimoo 2014-01-14 18:17:19 +01:00
parent 467ab3791f
commit 9cb744f440
2 changed files with 50 additions and 9 deletions

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Searx search: {{ q }}</title>
<link>{{ base_url }}?q={{ q }}</link>
<description>Search results for "{{ q }}" - searx</description>
<opensearch:totalResults>{{ number_of_results }}</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>{{ number_of_results }}</opensearch:itemsPerPage>
<atom:link rel="search" type="application/opensearchdescription+xml" href="{{ base_url }}opensearch.xml"/>
<opensearch:Query role="request" searchTerms="{{ q }}" startPage="1" />
{% for r in results %}
<item>
<title>{{ r.title }}</title>
<link>{{ r.url }}</link>
<description>{{ r.content }}</description>
</item>
{% endfor %}
</channel>
</rss>

View file

@ -36,6 +36,7 @@ from searx.utils import highlight_content, html_to_text
app = Flask(__name__)
app.secret_key = settings.secret_key
opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>searx</ShortName>
@ -48,6 +49,18 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
</OpenSearchDescription>
'''
def get_base_url():
if settings.base_url:
hostname = settings.base_url
else:
scheme = 'http'
if request.is_secure:
scheme = 'https'
hostname = url_for('index', _external=True, _scheme=scheme)
return hostname
def render(template_name, **kwargs):
global categories
kwargs['categories'] = sorted(categories.keys())
@ -69,7 +82,8 @@ def parse_query(query):
query = query.replace(query_parts[0], '', 1).strip()
return query, query_engines
@app.route('/', methods=['GET', 'POST'])
@APp.route('/', methods=['GET', 'POST'])
def index():
global categories
@ -132,6 +146,17 @@ def index():
response = Response(csv.stream.read(), mimetype='application/csv')
response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
return response
elif request_data.get('format') == 'rss':
response_rss = render('opensearch_response_rss.xml'
,results=results
,q=request_data['q']
,number_of_results=len(results)
,base_url=get_base_url()
)
response = Response(response_rss, mimetype='application/xml')
response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.xml'.format('_'.join(query.split())))
return response
return render('results.html'
,results=results
@ -187,17 +212,11 @@ Disallow: /stats
def opensearch():
global opensearch_xml
method = 'post'
scheme = 'http'
# chrome/chromium only supports HTTP GET....
if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
method = 'get'
if request.is_secure:
scheme = 'https'
if settings.base_url:
hostname = settings.base_url
else:
hostname = url_for('index', _external=True, _scheme=scheme)
ret = opensearch_xml.format(method=method, host=hostname)
base_url = get_base_url()
ret = opensearch_xml.format(method=method, host=base_url)
resp = Response(response=ret,
status=200,
mimetype="application/xml")