-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (51 loc) · 1.81 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os.path
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from flask import Flask, jsonify, make_response
from flask_cors import CORS, cross_origin
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"]
# The ID and range of your spreadsheet.
SPREADSHEET_ID = "1626eTuIBWLlKEfjqSC_2BLSHMkdXrv_l8_Y7I7ycN4E"
SPREADSHEET_RANGE = "B1:D4"
def get_credentials():
creds = service_account.Credentials.from_service_account_file(
"/etc/secrets/credentials.json", scopes=SCOPES
)
return creds
def fetch_data():
creds = get_credentials()
try:
service = build("sheets", "v4", credentials=creds)
sheet = service.spreadsheets()
result = (
sheet.values()
.get(spreadsheetId=SPREADSHEET_ID, range=SPREADSHEET_RANGE)
.execute()
)
values = result.get("values", [])
if not values:
return []
return values
except HttpError as err:
print(err)
return []
# Create a Flask app
app = Flask(__name__)
@cross_origin(origins='*',methods=['GET','OPTIONS',])
@app.route('/api/properties', methods=['GET'])
def get_properties():
data = fetch_data()
print("Data fetched from Google Sheets:", data) # Add logging to check the response
headers = data[0]
rows = data[1:]
processed_data = []
for row in rows:
obj = {headers[i]: row[i] for i in range(len(headers))}
processed_data.append(obj)
response = make_response(jsonify(processed_data))
response.headers['Content-Security-Policy'] = "default-src 'self'; script-src 'self' 'unsafe-inline';"
return response
if __name__ == '__main__':
app.run(debug=True)