def init(): db.connect() db.create_tables([Report]) if not db.is_closed(): db.close() # Use DateAwareJSONEncoder to dump JSON strings # From http://stackoverflow.com/questions/21282040/bottle-framework-how-to-return-datetime-in-json-response#comment55718456_21282666. pylint: disable=locally-disabled,line-too-long bottle.install( bottle.JSONPlugin(json_dumps=functools.partial( json.dumps, cls=DateAwareJSONEncoder)))
# Add the report to the db logging.info('Adding record %s to the database.', item['recordid']) Report.create(type=report_type, expiration_datetime=expiration_datetime, lat=lat, lng=lng, source=item['source'], shape_geojson=json.dumps(mapping(geo_shape))) except KeyError as exc: logging.warning('Invalid record %s in %s, missing key: %s', item.get('recordid', '?'), name, exc) if __name__ == '__main__': db.connect() for name, item in OPENDATA_URLS.items(): logging.info('Processing opendata from %s', name) try: r = requests.get(item['url']) data = r.json() except (requests.RequestException, ValueError) as exc: logging.warning('Error while fetching data for %s: %s.', name, exc) continue if item['preprocess']: data = item['preprocess'](data) process_opendata(name, data)