File size: 1,334 Bytes
44a875f
 
e2b2795
 
 
44a875f
 
 
e2b2795
 
 
 
 
 
 
 
 
 
44a875f
 
 
 
e2b2795
 
13121cc
 
 
 
 
 
 
 
 
 
 
44a875f
 
 
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

from flask import Flask, render_template, request, jsonify
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime

app = Flask(__name__)

GOOGLE_SHEET_URL = "https://docs.google.com/spreadsheets/d/1BECroPktjZll8jT9o1WjG-6WgUJzBKY3_yKeAcVPTcA/edit#gid=0"

def write_to_google_sheet(name, lat, lon, ua):
    scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
    creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
    client = gspread.authorize(creds)
    sheet = client.open_by_url(GOOGLE_SHEET_URL).sheet1
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    sheet.append_row([timestamp, name, lat, lon, ua])

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/submit", methods=["POST"])
def submit():
    try:
        data = request.json
        name = data.get("name")
        lat = data.get("lat")
        lon = data.get("lon")
        ua = data.get("ua")
        write_to_google_sheet(name, lat, lon, ua)
        return jsonify({"status": "ok"})
    except Exception as e:
        print("🔥 錯誤訊息:", str(e))
        return jsonify({"status": "error", "message": str(e)}), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)