From f3349ae53950cf38783e6fd14d776345d5e66556 Mon Sep 17 00:00:00 2001 From: Robbe Van Herck Date: Wed, 13 May 2020 02:22:29 +0200 Subject: [PATCH] Initial commit --- .gitignore | 5 ++++ README.md | 30 ++++++++++++++++++++++++ app.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 19 ++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa73a65 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +venv/ + +service_account.json + +config.ini diff --git a/README.md b/README.md new file mode 100644 index 0000000..1860d65 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Blokdata + +Create [blokmap](https://github.com/zeuswpi/blokmap) data from a Google sheet. + +## Installation + +1. Setup python + +```bash +virtualenv venv +. venv/bin/activate +pip install -r requirements.txt +``` + +2. Setup `config.ini` as follows + +``` +[Google] + +SHEET_ID = #FILLMEIN +RANGE_NAME = #METOO +``` + +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` + +5. Give the service account's email (read) access to the sheet + +6. `python app.py` diff --git a/app.py b/app.py new file mode 100644 index 0000000..7b7b9cf --- /dev/null +++ b/app.py @@ -0,0 +1,59 @@ +from apiclient.discovery import build +from httplib2 import Http +from google.oauth2 import service_account +import json + +import configparser +CONFIG = configparser.ConfigParser() +CONFIG.read("config.ini") + + +SPREADSHEET_ID = CONFIG["Google"]["SHEET_ID"] +RANGE_NAME = CONFIG["Google"]["RANGE_NAME"] + +def get_google_sheet(): + """ 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() + return gsheet + +def google_sheet_to_json(): + sheet = get_google_sheet() + data = sheet["values"] + return json.dumps([create_point(row) for row in data[1:]]) + +def create_point(row): + lat, lon, name, address, capacity, startdate, enddate, hours_monday, hours_tuesday, hours_wednesday, hours_thursday, hours_friday, hours_saturday, hours_sunday, extra, location_type = row + return { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [lon, lat], + }, + "properties": { + "name": name, + "address": address, + "capacity": capacity, + "period": { + "start": startdate, + "end": enddate, + }, + "hours": { + "monday": hours_monday, + "tuesday": hours_tuesday, + "wednesday": hours_wednesday, + "thursday": hours_thursday, + "friday": hours_friday, + "saturday": hours_saturday, + "sunday": hours_sunday, + }, + "extra": extra, + "type": location_type, + } + } + +print(google_sheet_to_json()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6e0e6dc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +cachetools==4.1.0 +certifi==2020.4.5.1 +chardet==3.0.4 +google-api-core==1.17.0 +google-api-python-client==1.8.2 +google-auth==1.14.3 +google-auth-httplib2==0.0.3 +googleapis-common-protos==1.51.0 +httplib2==0.17.3 +idna==2.9 +protobuf==3.11.3 +pyasn1==0.4.8 +pyasn1-modules==0.2.8 +pytz==2020.1 +requests==2.23.0 +rsa==4.0 +six==1.14.0 +uritemplate==3.0.1 +urllib3==1.25.9