summaryrefslogtreecommitdiff
path: root/chimere/utils.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2015-11-22 13:16:54 +0100
committerÉtienne Loks <etienne.loks@proxience.com>2015-11-22 13:16:54 +0100
commitea17ebad02a419da21c4512954c32dfb5ca7b650 (patch)
tree61a60e8bc2093e3cd13a66a0d85e92846fc75977 /chimere/utils.py
parentd2ca6b5ea37832ba7477b3d02425b83ca0938f49 (diff)
downloadChimère-ea17ebad02a419da21c4512954c32dfb5ca7b650.tar.bz2
Chimère-ea17ebad02a419da21c4512954c32dfb5ca7b650.zip
Manage JSON imports
Diffstat (limited to 'chimere/utils.py')
-rw-r--r--chimere/utils.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/chimere/utils.py b/chimere/utils.py
index e017762..5289600 100644
--- a/chimere/utils.py
+++ b/chimere/utils.py
@@ -27,6 +27,7 @@ import feedparser
import os
import re
import StringIO
+import json
import tempfile
import urllib2
import unicodedata
@@ -658,6 +659,77 @@ class GeoRSSManager(ImportManager):
new_item += 1
return (new_item, updated_item, msg)
+
+class JsonManager(ImportManager):
+ u"""
+ Json importer.
+ This manager only gets and do not produce Json feed
+ """
+
+ def get(self):
+ u"""
+ Get data from a json simple source
+
+ Return a tuple with:
+ - number of new item ;
+ - number of item updated ;
+ - error detail on error
+ """
+ from models import Marker
+ new_item, updated_item, msg = 0, 0, ''
+ source, msg = self.get_source_file(['.json'])
+ if msg:
+ return (0, 0, msg)
+
+ vals = source.read().replace('\n', ' ')
+ try:
+ values = json.JSONDecoder().decode(vals)
+ except ValueError as e:
+ return (new_item, updated_item,
+ _(u"JSON file is not well formed: " + e.message))
+ # configuration in filtr
+ try:
+ filtr = json.JSONDecoder().decode(self.importer_instance.filtr)
+ except ValueError:
+ return (
+ new_item, updated_item,
+ _(u"Bad configuration: filter field must be a valid "
+ u"JSON string"))
+
+ vls = filtr.values()
+ for k in ('name', 'id', 'description'):
+ if k not in vls:
+ return (
+ new_item, updated_item,
+ _(u"A key must be associated to \"%s\" in the "
+ u"filter.") % k)
+
+ for item in values:
+ dct = {'origin': self.importer_instance.origin,
+ 'license': self.importer_instance.license}
+ for k in filtr:
+ if k in item and item[k]:
+ dct[filtr[k]] = item[k]
+ if 'point' in item:
+ x, y = item['point'].split(",")
+ dct['point'] = 'SRID=4326;POINT(%s %s)' % (x, y)
+ elif 'lat' in item and item['lat']:
+ dct['point'] = 'SRID=4326;POINT(%s %s)' % (item['lon'],
+ item['lat'])
+ else:
+ dct['point'] = self.importer_instance.default_localisation
+ if not dct['point']:
+ continue
+ cls = Marker
+ pl_id = (dct.pop('id') if 'id' in dct else dct['name']) \
+ + "-" + unicode(self.importer_instance.pk)
+ it, updated, created = self.create_or_update_item(cls, dct, pl_id)
+ if updated:
+ updated_item += 1
+ if created:
+ new_item += 1
+ return (new_item, updated_item, msg)
+
RE_HOOK = re.compile('\[([^\]]*)\]')
# TODO: manage deleted item from OSM