From ab942ca91d087c8f001d802702b1185f24b28853 Mon Sep 17 00:00:00 2001 From: Marian Steinbach Date: Sat, 4 May 2019 23:00:00 +0200 Subject: [PATCH] Fix full JSON export --- export/__init__.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/export/__init__.py b/export/__init__.py index 5e8db08..639f011 100644 --- a/export/__init__.py +++ b/export/__init__.py @@ -2,15 +2,27 @@ Exports data from the database to JSON files for use in a static webapp """ -from hashlib import md5 -import json +import datetime import logging import sys import os +from hashlib import md5 +import json import requests +class DateTimeEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, datetime.datetime): + return obj.isoformat() + elif isinstance(obj, datetime.date): + return obj.isoformat() + elif isinstance(obj, datetime.timedelta): + return (datetime.datetime.min + obj).time().isoformat() + else: + return super(DateTimeEncoder, self).default(obj) + def export_results(client, entity_kind): """ Export of the main results data @@ -31,6 +43,6 @@ def export_results(client, entity_kind): 'score': entity.get('score'), }) - output_filename = "spider_result.json" + output_filename = "/json-export/spider_result.json" with open(output_filename, 'w', encoding="utf8") as jsonfile: - json.dump(out, jsonfile, indent=2, sort_keys=True, ensure_ascii=False) + json.dump(out, jsonfile, indent=2, sort_keys=True, ensure_ascii=False, cls=DateTimeEncoder)