Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

once you go black ⚫ #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 5 additions & 3 deletions blokdata/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
66 changes: 54 additions & 12 deletions blokdata/sheet_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand All @@ -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"),
}
},
}
40 changes: 22 additions & 18 deletions blokmap_to_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
]
)
)