Skip to content

Commit

Permalink
Add CSV support
Browse files Browse the repository at this point in the history
  • Loading branch information
marians committed Jul 15, 2019
1 parent 8c6ecfc commit 96dd3aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Gibt den Zeitpunkt der letzten Aktualisierung der Spider-Ergebnisse zurück.

Gibt Ergebnisse für alle Sites in einem tabellenfreundlichen Format aus.

Wenn per `Accept`-Header der Typ `text/csv` angefordert wird, erfolgt die Ausgabe
im CSV-Format. Ansonsten wird JSON ausgegeben.

```json
[
{
Expand Down
23 changes: 21 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import collections
import csv
import io
import sys
from datetime import datetime
from os import getenv
import sys
from wsgiref import simple_server

import falcon
Expand Down Expand Up @@ -129,7 +131,23 @@ def on_get(self, req, resp):

maxage = 48 * 60 * 60 # two days
resp.cache_control = ["max_age=%d" % maxage]
resp.media = out
if req.accept == 'text/csv':
# return CSV
headers = sorted(out[0].keys())

with io.StringIO(newline='\n') as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
writer.writerow(headers)
for row in out:
o = []
for f in headers:
o.append(str(row[f]))
writer.writerow(o)
resp.body = csvfile.getvalue()
resp.content_type = 'text/csv'
resp.status = falcon.HTTP_200
else:
resp.media = out


class SpiderResultsQuery(object):
Expand Down Expand Up @@ -263,6 +281,7 @@ def on_get(self, req, resp):

handlers = media.Handlers({
'application/json': jsonhandler.JSONHandler(),
'text/csv': media.BaseHandler,
})

app = falcon.API()
Expand Down

0 comments on commit 96dd3aa

Please sign in to comment.