mirror of https://github.com/searxng/searxng.git
[enh] opensearch/rss support part I.
This commit is contained in:
parent
467ab3791f
commit
9cb744f440
|
@ -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>
|
|
@ -36,6 +36,7 @@ from searx.utils import highlight_content, html_to_text
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = settings.secret_key
|
app.secret_key = settings.secret_key
|
||||||
|
|
||||||
|
|
||||||
opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
||||||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||||
<ShortName>searx</ShortName>
|
<ShortName>searx</ShortName>
|
||||||
|
@ -48,6 +49,18 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
||||||
</OpenSearchDescription>
|
</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):
|
def render(template_name, **kwargs):
|
||||||
global categories
|
global categories
|
||||||
kwargs['categories'] = sorted(categories.keys())
|
kwargs['categories'] = sorted(categories.keys())
|
||||||
|
@ -69,7 +82,8 @@ def parse_query(query):
|
||||||
query = query.replace(query_parts[0], '', 1).strip()
|
query = query.replace(query_parts[0], '', 1).strip()
|
||||||
return query, query_engines
|
return query, query_engines
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
|
||||||
|
@APp.route('/', methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
global categories
|
global categories
|
||||||
|
|
||||||
|
@ -132,6 +146,17 @@ def index():
|
||||||
response = Response(csv.stream.read(), mimetype='application/csv')
|
response = Response(csv.stream.read(), mimetype='application/csv')
|
||||||
response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
|
response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
|
||||||
return response
|
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'
|
return render('results.html'
|
||||||
,results=results
|
,results=results
|
||||||
|
@ -187,17 +212,11 @@ Disallow: /stats
|
||||||
def opensearch():
|
def opensearch():
|
||||||
global opensearch_xml
|
global opensearch_xml
|
||||||
method = 'post'
|
method = 'post'
|
||||||
scheme = 'http'
|
|
||||||
# chrome/chromium only supports HTTP GET....
|
# chrome/chromium only supports HTTP GET....
|
||||||
if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
|
if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
|
||||||
method = 'get'
|
method = 'get'
|
||||||
if request.is_secure:
|
base_url = get_base_url()
|
||||||
scheme = 'https'
|
ret = opensearch_xml.format(method=method, host=base_url)
|
||||||
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)
|
|
||||||
resp = Response(response=ret,
|
resp = Response(response=ret,
|
||||||
status=200,
|
status=200,
|
||||||
mimetype="application/xml")
|
mimetype="application/xml")
|
||||||
|
|
Loading…
Reference in New Issue