[enh][fix] csv output updates

This commit is contained in:
asciimoo 2013-12-02 21:36:09 +01:00
parent 3c8a5f2cd3
commit ba131c842d

View file

@ -113,14 +113,15 @@ def index():
return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json') return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json')
elif request_data.get('format') == 'csv': elif request_data.get('format') == 'csv':
csv = UnicodeWriter(cStringIO.StringIO()) csv = UnicodeWriter(cStringIO.StringIO())
keys = ('title', 'url', 'content', 'host', 'engine', 'score')
if len(results): if len(results):
keys = results[0].keys()
csv.writerow(keys) csv.writerow(keys)
for row in results: for row in results:
csv.writerow([row[key] for key in keys]) row['host'] = row['parsed_url'].netloc
csv.writerow([row.get(key, '') for key in keys])
csv.stream.seek(0) csv.stream.seek(0)
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(query)) response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
return response return response
template = render('results.html' template = render('results.html'