From 8a010266a050b5a6c1cf2c42050e2b4d7a70a103 Mon Sep 17 00:00:00 2001 From: maxime Date: Wed, 1 Jun 2022 17:07:26 +0200 Subject: [PATCH] =?UTF-8?q?once=20you=20go=20black=20=E2=9A=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blokdata/app.py | 8 +++-- blokdata/sheet_data.py | 66 ++++++++++++++++++++++++++++++++++-------- blokmap_to_sheet.py | 40 +++++++++++++------------ 3 files changed, 81 insertions(+), 33 deletions(-) diff --git a/blokdata/app.py b/blokdata/app.py index 5a37b6f..dc0bef8 100644 --- a/blokdata/app.py +++ b/blokdata/app.py @@ -11,10 +11,10 @@ # Add CORS cors = CORS(app) -app.config['CORS_HEADERS'] = 'Content-Type' +app.config["CORS_HEADERS"] = "Content-Type" # Add caching -cache = Cache(app, config={'CACHE_TYPE': 'simple'}) +cache = Cache(app, config={"CACHE_TYPE": "simple"}) # Read the config file CONFIG = configparser.ConfigParser() @@ -24,11 +24,13 @@ RANGE_NAME = CONFIG["Google"]["RANGE_NAME"] CACHE_TIMEOUT = int(CONFIG["Cache"]["TIMEOUT"]) -@app.route('/data.json') + +@app.route("/data.json") @cross_origin() @cache.cached(timeout=CACHE_TIMEOUT) def data_json(): return google_sheet_to_json(SPREADSHEET_ID, RANGE_NAME) + if __name__ == "__main__": app.run() diff --git a/blokdata/sheet_data.py b/blokdata/sheet_data.py index a2c93ae..f887144 100644 --- a/blokdata/sheet_data.py +++ b/blokdata/sheet_data.py @@ -3,16 +3,27 @@ from google.oauth2 import service_account import json + def get_google_sheet(spreadsheet_id, range_name): - """ Retrieve sheet data using OAuth credentials and Google Python API. """ - credentials = service_account.Credentials.from_service_account_file('service_account.json') - scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/spreadsheets.readonly']) - service = build('sheets', 'v4', credentials=scoped_credentials) + """Retrieve sheet data using OAuth credentials and Google Python API.""" + credentials = service_account.Credentials.from_service_account_file( + "service_account.json" + ) + scoped_credentials = credentials.with_scopes( + ["https://www.googleapis.com/auth/spreadsheets.readonly"] + ) + service = build("sheets", "v4", credentials=scoped_credentials) # Call the Sheets API - gsheet = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute() + gsheet = ( + service.spreadsheets() + .values() + .get(spreadsheetId=spreadsheet_id, range=range_name) + .execute() + ) return gsheet + def google_sheet_to_json(spreadsheet_id, range_name): sheet = get_google_sheet(spreadsheet_id, range_name) data = sheet["values"] @@ -23,12 +34,33 @@ def google_sheet_to_json(spreadsheet_id, range_name): ret.append(point) return json.dumps(ret) + def create_point(row): - if (len(row) < 19): + if len(row) < 19: return None # 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, + ) = row[0:19] if active == "FALSE": return None @@ -50,13 +82,23 @@ def create_point(row): "end": enddate, }, # We give dict an iterable of pairs, dict gives us a dict - "hours": dict(zip( - ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"], - hours - )), + "hours": dict( + zip( + [ + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + "sunday", + ], + hours, + ) + ), "extra": extra, "type": location_type, "wheelchair": (wheelchair.lower() == "ja"), "wifi": (wifi.lower() == "ja"), - } + }, } diff --git a/blokmap_to_sheet.py b/blokmap_to_sheet.py index 254a791..438f1cf 100755 --- a/blokmap_to_sheet.py +++ b/blokmap_to_sheet.py @@ -6,21 +6,25 @@ data = response.json() for location in data: - print("\t".join([ - str(location["geometry"]["coordinates"][1]), - str(location["geometry"]["coordinates"][0]), - str(location["properties"]["name"]), - str(location["properties"]["address"]), - str(location["properties"]["capacity"]), - str(location["properties"]["period"]["start"]), - str(location["properties"]["period"]["end"]), - str(location["properties"]["hours"]["monday"]), - str(location["properties"]["hours"]["tuesday"]), - str(location["properties"]["hours"]["wednesday"]), - str(location["properties"]["hours"]["thursday"]), - str(location["properties"]["hours"]["friday"]), - str(location["properties"]["hours"]["saturday"]), - str(location["properties"]["hours"]["sunday"]), - str(location["properties"]["extra"]), - str(location["properties"]["type"]) - ])) + print( + "\t".join( + [ + str(location["geometry"]["coordinates"][1]), + str(location["geometry"]["coordinates"][0]), + str(location["properties"]["name"]), + str(location["properties"]["address"]), + str(location["properties"]["capacity"]), + str(location["properties"]["period"]["start"]), + str(location["properties"]["period"]["end"]), + str(location["properties"]["hours"]["monday"]), + str(location["properties"]["hours"]["tuesday"]), + str(location["properties"]["hours"]["wednesday"]), + str(location["properties"]["hours"]["thursday"]), + str(location["properties"]["hours"]["friday"]), + str(location["properties"]["hours"]["saturday"]), + str(location["properties"]["hours"]["sunday"]), + str(location["properties"]["extra"]), + str(location["properties"]["type"]), + ] + ) + )