GPSAR_Docker / app.py
Joey889's picture
Upload 3 files
13121cc verified
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)