mirror of
https://github.com/netzbegruenung/green-spider-api.git
synced 2024-04-27 22:34:51 +02:00
commit
dbdb053ac1
|
@ -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
|
||||
[
|
||||
{
|
||||
|
|
21
main.py
21
main.py
|
@ -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
|
||||
|
@ -129,6 +131,22 @@ class TableResults(object):
|
|||
|
||||
maxage = 48 * 60 * 60 # two days
|
||||
resp.cache_control = ["max_age=%d" % maxage]
|
||||
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
|
||||
|
||||
|
||||
|
@ -263,6 +281,7 @@ class Index(object):
|
|||
|
||||
handlers = media.Handlers({
|
||||
'application/json': jsonhandler.JSONHandler(),
|
||||
'text/csv': media.BaseHandler,
|
||||
})
|
||||
|
||||
app = falcon.API()
|
||||
|
|
Loading…
Reference in a new issue