Edoruin commited on
Commit
e2bfcae
·
1 Parent(s): 75007fd

Implement Google Sheets synchronization forwarding

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -43,16 +43,38 @@ def create_capture():
43
 
44
  @app.route('/api/sync', methods=['POST'])
45
  def sync_captures():
 
46
  try:
47
  data = request.get_json()
48
- captures = data.get('captures', [])
49
- synced_ids = []
50
- for c_data in captures:
 
51
  capture = Capture.from_dict(c_data)
52
  captures_storage.append(capture)
53
- synced_ids.append(capture.id)
54
- return jsonify({'success': True, 'synced': synced_ids, 'total': len(synced_ids)}), 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
 
56
  return jsonify({'success': False, 'error': str(e)}), 400
57
 
58
  @app.route('/debug/files')
 
43
 
44
  @app.route('/api/sync', methods=['POST'])
45
  def sync_captures():
46
+ """Batch synchronization endpoint with Google Sheets forwarding."""
47
  try:
48
  data = request.get_json()
49
+ captures_data = data.get('captures', [])
50
+
51
+ # Store locally (in-memory for now)
52
+ for c_data in captures_data:
53
  capture = Capture.from_dict(c_data)
54
  captures_storage.append(capture)
55
+
56
+ # Forward to Google Sheets if configured
57
+ gas_url = os.environ.get('GAS_DEPLOYMENT_URL')
58
+ if gas_url and captures_data:
59
+ print(f"Forwarding {len(captures_data)} captures to Google Sheets...")
60
+ try:
61
+ import requests
62
+ # Forward the exact payload expected by the GAS script
63
+ response = requests.post(gas_url, json={'captures': captures_data}, timeout=10)
64
+ if not response.ok:
65
+ print(f"Warning: Google Sheets sync failed with status {response.status_code}")
66
+ except Exception as e:
67
+ print(f"Warning: Error forwarding to Google Sheets: {str(e)}")
68
+
69
+ synced_ids = [c.get('id') for c in captures_data]
70
+ return jsonify({
71
+ 'success': True,
72
+ 'synced': synced_ids,
73
+ 'total': len(synced_ids)
74
+ }), 200
75
+
76
  except Exception as e:
77
+ print(f"Sync error: {str(e)}")
78
  return jsonify({'success': False, 'error': str(e)}), 400
79
 
80
  @app.route('/debug/files')