Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, jsonify | |
| import gspread | |
| from oauth2client.service_account import ServiceAccountCredentials | |
| import json | |
| import os | |
| from io import StringIO | |
| from datetime import datetime, timezone, timedelta | |
| 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_raw = os.environ.get("GOOGLE_CREDS") | |
| creds_dict = json.load(StringIO(creds_raw)) | |
| creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope) | |
| client = gspread.authorize(creds) | |
| sheet = client.open_by_url(GOOGLE_SHEET_URL).sheet1 | |
| tz = timezone(timedelta(hours=8)) # ε°εζι | |
| timestamp = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| sheet.append_row([timestamp, name, lat, lon, ua]) | |
| def index(): | |
| return render_template("index.html") | |
| 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: | |
| return jsonify({"status": "error", "message": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |