From 83c9c44b32bde228dab8d07495b65605593a9550 Mon Sep 17 00:00:00 2001 From: AurisAudentis Date: Sat, 25 Dec 2021 12:45:15 +0100 Subject: [PATCH] Reinforcing coordinate parsing, updating output to include errors --- README.md | 3 ++- blokdata/app.py | 4 ++-- blokdata/sheet_data.py | 47 +++++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7d010de..e6a42b5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ pip install -r requirements.txt ``` 2. Setup `config.ini` as follows - ``` [Google] @@ -25,6 +24,8 @@ RANGE_NAME = #METOO TIMEOUT = 10 ``` +The Sheet-id can be found in your sheet URL: https://docs.google.com/spreadsheets/d/**1coZZXZcGE4oK-ca-OGoIFizH_c4E62Ut0cktp8ppNCg**/edit#gid=0 + 3. [Request a Google service account](https://console.cloud.google.com/iam-admin/serviceaccounts) 4. Store the service account's key as `service_account.json` diff --git a/blokdata/app.py b/blokdata/app.py index 5a37b6f..586e4ce 100644 --- a/blokdata/app.py +++ b/blokdata/app.py @@ -1,6 +1,6 @@ from .sheet_data import google_sheet_to_json -from flask import Flask +from flask import Flask, Response from flask_cors import CORS, cross_origin from flask_caching import Cache @@ -28,7 +28,7 @@ @cross_origin() @cache.cached(timeout=CACHE_TIMEOUT) def data_json(): - return google_sheet_to_json(SPREADSHEET_ID, RANGE_NAME) + return Response(google_sheet_to_json(SPREADSHEET_ID, RANGE_NAME), mimetype='application/json') if __name__ == "__main__": app.run() diff --git a/blokdata/sheet_data.py b/blokdata/sheet_data.py index a2c93ae..fba8a0d 100644 --- a/blokdata/sheet_data.py +++ b/blokdata/sheet_data.py @@ -17,29 +17,38 @@ def google_sheet_to_json(spreadsheet_id, range_name): sheet = get_google_sheet(spreadsheet_id, range_name) data = sheet["values"] ret = [] - for row in data[1:]: - point = create_point(row) - if point is not None: - ret.append(point) - return json.dumps(ret) - -def create_point(row): - if (len(row) < 19): - return None + errors = [] + for i, row in enumerate(data[1:]): + try: + point = create_point(i+1, row) + if point is not None: + ret.append(point) + except Exception as e: + errors.append({ "type": "error", "row": i+1, "message": "\n".join(e.args) }) + return json.dumps({ "points": ret, "errors": errors }) + +def create_point(rowIndex, rowContent): + if (len(rowContent) < 19): + raise Exception(f"Row {rowIndex} does not have the correct format.", f"It is too short: a length of 19 was expected, but the length found was {len(rowContent)}.") # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 - active, lat, lon, name, address, capacity, startdate, enddate, _, _, _, _, _, _, _, extra, location_type, wifi, wheelchair = row[0:19] + active, lat, lon, name, address, capacity, startdate, enddate, _, _, _, _, _, _, _, extra, location_type, wifi, wheelchair = rowContent[0:19] if active == "FALSE": return None - hours = [x.replace("
", "\n") for x in row[8:15]] + hours = [x.replace("
", "\n") for x in rowContent[8:15]] + + try: + coordinates = [f"{parse_coordinate(lon)}", f"{parse_coordinate(lat)}"] + except Exception as a: + raise Exception(f"Row {rowIndex} does not have the correct format.", f"One of the coordinates failed to parse.", *a.args) return { "type": "Feature", "geometry": { "type": "Point", - "coordinates": [lon, lat], + "coordinates": coordinates, }, "properties": { "name": name, @@ -60,3 +69,17 @@ def create_point(row): "wifi": (wifi.lower() == "ja"), } } + +# Function to parse a coordinate +# Should be able to handle both English and Dutch locales +# If it can't parse the number, throw a clear error +def parse_coordinate(coord): + if coord == "": + raise Exception("Coord value is empty.") + + comma_fixed_coord = coord.replace(",", ".") + + try: + return float(comma_fixed_coord) + except: + raise Exception(f"Coord is not a float: {comma_fixed_coord}") \ No newline at end of file