on master help sign in
[enh] opensearch/rss support part I.
commit9cb744f440f72e3a4a5c1eb3e4b8a3daf88299a7 authorasciimoo <asciimoo@gmail.com> dateTue 14 Jan 2014, 18:17 (13 years ago) parent467ab37
2 files changed+50−9
searx/templates/opensearch_response_rss.xml+22 searx/webapp.py+28−9
searx/templates/opensearch_response_rss.xmlnew file+22
@@ -0,0 +1,22 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<rss version="2.0"
3+ xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
4+ xmlns:atom="http://www.w3.org/2005/Atom">
5+ <channel>
6+ <title>Searx search: {{ q }}</title>
7+ <link>{{ base_url }}?q={{ q }}</link>
8+ <description>Search results for "{{ q }}" - searx</description>
9+ <opensearch:totalResults>{{ number_of_results }}</opensearch:totalResults>
10+ <opensearch:startIndex>1</opensearch:startIndex>
11+ <opensearch:itemsPerPage>{{ number_of_results }}</opensearch:itemsPerPage>
12+ <atom:link rel="search" type="application/opensearchdescription+xml" href="{{ base_url }}opensearch.xml"/>
13+ <opensearch:Query role="request" searchTerms="{{ q }}" startPage="1" />
14+ {% for r in results %}
15+ <item>
16+ <title>{{ r.title }}</title>
17+ <link>{{ r.url }}</link>
18+ <description>{{ r.content }}</description>
19+ </item>
20+ {% endfor %}
21+ </channel>
22+</rss>
searx/webapp.py+28−9
@@ -36,6 +36,7 @@ from searx.utils import highlight_content, html_to_text
3636app = Flask(__name__)
3737app.secret_key = settings.secret_key
3838
39+
3940opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
4041<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
4142 <ShortName>searx</ShortName>
@@ -48,6 +49,18 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
4849</OpenSearchDescription>
4950'''
5051
52+
53+def get_base_url():
54+ if settings.base_url:
55+ hostname = settings.base_url
56+ else:
57+ scheme = 'http'
58+ if request.is_secure:
59+ scheme = 'https'
60+ hostname = url_for('index', _external=True, _scheme=scheme)
61+ return hostname
62+
63+
5164def render(template_name, **kwargs):
5265 global categories
5366 kwargs['categories'] = sorted(categories.keys())
@@ -69,7 +82,8 @@ def parse_query(query):
6982 query = query.replace(query_parts[0], '', 1).strip()
7083 return query, query_engines
7184
72−@app.route('/', methods=['GET', 'POST'])
85+
86+@APp.route('/', methods=['GET', 'POST'])
7387def index():
7488 global categories
7589
@@ -132,6 +146,17 @@ def index():
132146 response = Response(csv.stream.read(), mimetype='application/csv')
133147 response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
134148 return response
149+ elif request_data.get('format') == 'rss':
150+ response_rss = render('opensearch_response_rss.xml'
151+ ,results=results
152+ ,q=request_data['q']
153+ ,number_of_results=len(results)
154+ ,base_url=get_base_url()
155+ )
156+ response = Response(response_rss, mimetype='application/xml')
157+ response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.xml'.format('_'.join(query.split())))
158+ return response
159+
135160
136161 return render('results.html'
137162 ,results=results
@@ -187,17 +212,11 @@ Disallow: /stats
187212def opensearch():
188213 global opensearch_xml
189214 method = 'post'
190− scheme = 'http'
191215 # chrome/chromium only supports HTTP GET....
192216 if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
193217 method = 'get'
194− if request.is_secure:
195− scheme = 'https'
196− if settings.base_url:
197− hostname = settings.base_url
198− else:
199− hostname = url_for('index', _external=True, _scheme=scheme)
200− ret = opensearch_xml.format(method=method, host=hostname)
218+ base_url = get_base_url()
219+ ret = opensearch_xml.format(method=method, host=base_url)
201220 resp = Response(response=ret,
202221 status=200,
203222 mimetype="application/xml")