Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pip install -r requirements.txt
```

2. Setup `config.ini` as follows

```
[Google]

Expand All @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions blokdata/app.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
47 changes: 35 additions & 12 deletions blokdata/sheet_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("</br>", "\n") for x in row[8:15]]
hours = [x.replace("</br>", "\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,
Expand All @@ -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}")