Initial commit
This commit is contained in:
commit
f3349ae539
4 changed files with 113 additions and 0 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
venv/
|
||||||
|
|
||||||
|
service_account.json
|
||||||
|
|
||||||
|
config.ini
|
30
README.md
Normal file
30
README.md
Normal file
|
@ -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`
|
59
app.py
Normal file
59
app.py
Normal file
|
@ -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())
|
19
requirements.txt
Normal file
19
requirements.txt
Normal file
|
@ -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
|
Loading…
Reference in a new issue