diff --git a/sunlight/service.py b/sunlight/service.py index 296c4b8..455f1d4 100644 --- a/sunlight/service.py +++ b/sunlight/service.py @@ -9,6 +9,7 @@ :class:`sunlight.services.openstates.OpenStates`) inherit from this. """ import sys +import datetime import sunlight.config import sunlight.errors @@ -111,3 +112,13 @@ class EntityDict(dict): def __init__(self, data={}, meta=None): dict.__init__(self, data) self._meta = meta + + +def datetime_parser(dct): + for k, v in dct.items(): + for dte_fmt in ("%Y-%m-%d", "%Y-%m-%dT%H:%M:%SZ"): + try: + dct[k] = datetime.datetime.strptime(v, dte_fmt) + except Exception: + pass + return dct diff --git a/sunlight/services/capitolwords.py b/sunlight/services/capitolwords.py index 57c85e5..48a9a49 100644 --- a/sunlight/services/capitolwords.py +++ b/sunlight/services/capitolwords.py @@ -3,6 +3,7 @@ from sunlight.errors import InvalidRequestException, BadRequestException +from sunlight.service import datetime_parser import sunlight.service import json @@ -104,7 +105,7 @@ def _get_url(self, pathparts, apikey, **kwargs): return ret def _decode_response(self, response): - ret = json.loads(response) + ret = json.loads(response, object_hook=datetime_parser) if "error" in ret: ex = InvalidRequestException(ret['error']) ex.response = ret diff --git a/sunlight/services/congress.py b/sunlight/services/congress.py index 6dc339f..49f48ab 100644 --- a/sunlight/services/congress.py +++ b/sunlight/services/congress.py @@ -9,7 +9,7 @@ """ import sunlight.service -from sunlight.service import EntityDict, EntityList +from sunlight.service import EntityDict, EntityList, datetime_parser from sunlight.pagination import pageable from sunlight.errors import SunlightException import json @@ -342,7 +342,7 @@ def _get_url(self, pathparts, apikey, **kwargs): def _decode_response(self, response): try: - data = json.loads(response) + data = json.loads(response, object_hook=datetime_parser) except Exception: raise SunlightException('Error parsing response! Something must be wrong with our Congress... API') results = data.pop('results', None) diff --git a/sunlight/services/opencivic.py b/sunlight/services/opencivic.py index 04a61f0..087f658 100644 --- a/sunlight/services/opencivic.py +++ b/sunlight/services/opencivic.py @@ -3,7 +3,7 @@ import sunlight.service from sunlight.errors import BadRequestException -from sunlight.service import EntityDict +from sunlight.service import EntityDict, datetime_parser import json module_name = "opencivic" @@ -77,4 +77,4 @@ def _get_url(self, objs, apikey, **kwargs): ) def _decode_response(self, response): - return json.loads(response) + return json.loads(response, object_hook=datetime_parser) diff --git a/sunlight/services/openstates.py b/sunlight/services/openstates.py index 735e8b1..4342fee 100644 --- a/sunlight/services/openstates.py +++ b/sunlight/services/openstates.py @@ -2,6 +2,7 @@ # of the LICENSE file. import sunlight.service +from sunlight.service import datetime_parser from sunlight.errors import BadRequestException import json @@ -196,4 +197,4 @@ def _get_url(self, objs, apikey, **kwargs): ) def _decode_response(self, response): - return json.loads(response) + return json.loads(response, object_hook=datetime_parser)