Browse Source

[enh] opensearch/rss support part I.

asciimoo 11 years ago
parent
commit
9cb744f440
2 changed files with 50 additions and 9 deletions
  1. 22
    0
      searx/templates/opensearch_response_rss.xml
  2. 28
    9
      searx/webapp.py

+ 22
- 0
searx/templates/opensearch_response_rss.xml View File

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>

+ 28
- 9
searx/webapp.py View File

36
 app = Flask(__name__)
36
 app = Flask(__name__)
37
 app.secret_key = settings.secret_key
37
 app.secret_key = settings.secret_key
38
 
38
 
39
+
39
 opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
40
 opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
40
 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
41
 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
41
   <ShortName>searx</ShortName>
42
   <ShortName>searx</ShortName>
48
 </OpenSearchDescription>
49
 </OpenSearchDescription>
49
 '''
50
 '''
50
 
51
 
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
+
51
 def render(template_name, **kwargs):
64
 def render(template_name, **kwargs):
52
     global categories
65
     global categories
53
     kwargs['categories'] = sorted(categories.keys())
66
     kwargs['categories'] = sorted(categories.keys())
69
         query = query.replace(query_parts[0], '', 1).strip()
82
         query = query.replace(query_parts[0], '', 1).strip()
70
     return query, query_engines
83
     return query, query_engines
71
 
84
 
72
-@app.route('/', methods=['GET', 'POST'])
85
+
86
+@APp.route('/', methods=['GET', 'POST'])
73
 def index():
87
 def index():
74
     global categories
88
     global categories
75
 
89
 
132
         response = Response(csv.stream.read(), mimetype='application/csv')
146
         response = Response(csv.stream.read(), mimetype='application/csv')
133
         response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
147
         response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
134
         return response
148
         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
+
135
 
160
 
136
     return render('results.html'
161
     return render('results.html'
137
                  ,results=results
162
                  ,results=results
187
 def opensearch():
212
 def opensearch():
188
     global opensearch_xml
213
     global opensearch_xml
189
     method = 'post'
214
     method = 'post'
190
-    scheme = 'http'
191
     # chrome/chromium only supports HTTP GET....
215
     # chrome/chromium only supports HTTP GET....
192
     if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
216
     if request.headers.get('User-Agent', '').lower().find('webkit') >= 0:
193
         method = 'get'
217
         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)
201
     resp = Response(response=ret,
220
     resp = Response(response=ret,
202
                 status=200,
221
                 status=200,
203
                 mimetype="application/xml")
222
                 mimetype="application/xml")