-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
570 lines (491 loc) · 19.7 KB
/
Copy pathCode.gs
File metadata and controls
570 lines (491 loc) · 19.7 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
var SPREADSHEET_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
var API_KEY = ''; // Set this to your password
var GEMINI_API_KEY = 'your-gemini-api-key-here';
var ANTHROPIC_API_KEY = 'your-anthropic-api-key-here';
function doPost(e) {
try {
var body = JSON.parse(e.postData.contents);
if (body.key !== API_KEY) {
return respond({ success: false, error: 'Unauthorized' });
}
switch (body.action) {
case 'getIngredients': return respond(handleGetIngredients());
case 'getSavedMeals': return respond(handleGetSavedMeals());
case 'saveMeal': return respond(handleSaveMeal(body));
case 'logMeal': return respond(handleLogMeal(body));
case 'getDailyMeals': return respond(handleGetDailyMeals(body));
case 'deleteDailyMeal': return respond(handleDeleteDailyMeal(body));
case 'getExercises': return respond(handleGetExercises());
case 'getSavedRoutines': return respond(handleGetSavedRoutines());
case 'saveRoutine': return respond(handleSaveRoutine(body));
case 'logWorkout': return respond(handleLogWorkout(body));
case 'getDailyWorkout': return respond(handleGetDailyWorkout(body));
case 'getLastWorkoutWeights': return respond(handleGetLastWorkoutWeights(body));
case 'getGoals': return respond(handleGetGoals());
case 'saveGoals': return respond(handleSaveGoals(body));
case 'getBodyLog': return respond(handleGetBodyLog());
case 'logBody': return respond(handleLogBody(body));
case 'deleteBodyLog': return respond(handleDeleteBodyLog(body));
case 'getMealUsageCounts': return respond(handleGetMealUsageCounts());
case 'analyzeFood': return respond(handleAnalyzeFood(body));
case 'describeMeal': return respond(handleDescribeMeal(body));
case 'analyzeFoodPaid': return respond(handleAnalyzeFoodPaid(body));
case 'describeMealPaid': return respond(handleDescribeMealPaid(body));
case 'addIngredient': return respond(handleAddIngredient(body));
default: return respond({ success: false, error: 'Unknown action: ' + body.action });
}
} catch (err) {
return respond({ success: false, error: err.message });
}
}
function respond(data) {
return ContentService
.createTextOutput(JSON.stringify(data))
.setMimeType(ContentService.MimeType.JSON);
}
// --- Helpers ---
function getSheet(name) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
}
function getSheetData(name) {
var sheet = getSheet(name);
var data = sheet.getDataRange().getValues();
if (data.length < 2) return [];
var headers = data[0];
var rows = [];
for (var i = 1; i < data.length; i++) {
var obj = {};
for (var j = 0; j < headers.length; j++) {
var val = data[i][j];
if (val && typeof val.getTime === 'function') val = normalizeDate(val);
obj[headers[j]] = val;
}
rows.push(obj);
}
return rows;
}
function normalizeDate(val) {
if (val && typeof val.getTime === 'function') {
return Utilities.formatDate(val, Session.getScriptTimeZone(), 'yyyy-MM-dd');
}
return String(val);
}
function appendRows(sheetName, rows) {
var sheet = getSheet(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
for (var i = 0; i < rows.length; i++) {
var row = [];
for (var j = 0; j < headers.length; j++) {
row.push(rows[i][headers[j]] !== undefined ? rows[i][headers[j]] : '');
}
sheet.appendRow(row);
}
}
function groupBy(rows, key) {
var groups = {};
for (var i = 0; i < rows.length; i++) {
var k = rows[i][key];
if (!groups[k]) groups[k] = [];
groups[k].push(rows[i]);
}
return groups;
}
// --- Setup (run once) ---
function setup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tabs = {
'Ingredients': ['Name', 'Calories_100g', 'Protein_100g', 'Carbs_100g', 'Fat_100g', 'Fiber_100g'],
'Saved Meals': ['Meal_ID', 'Meal_Name', 'Ingredient', 'Qty_g', 'Calories', 'Protein', 'Carbs', 'Fat', 'Fiber'],
'Daily Meals': ['Date', 'Meal_ID', 'Meal_Name', 'Ingredient', 'Qty_g', 'Calories', 'Protein', 'Carbs', 'Fat', 'Fiber'],
'Exercises': ['Name', 'Category'],
'Saved Routines': ['Routine_ID', 'Routine_Name', 'Exercise', 'Order'],
'Daily Workouts': ['Date', 'Routine_ID', 'Routine_Name', 'Exercise', 'Set_Num', 'Reps', 'Weight_kg'],
'Goals': ['Calories', 'Protein', 'Carbs', 'Fat'],
'Body Log': ['Date', 'Weight_kg', 'Fat_pct']
};
var names = Object.keys(tabs);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var sheet = ss.getSheetByName(name);
if (!sheet) sheet = ss.insertSheet(name);
var headers = tabs[name];
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
}
}
// --- Populate data (run once from script editor) ---
function populateIngredients() {
var sheet = getSheet('Ingredients');
var existing = sheet.getDataRange().getValues();
if (existing.length > 1) {
sheet.getRange(2, 1, existing.length - 1, existing[0].length).clear();
}
var data = [
['Chicken breast', 165, 31, 0, 3.6, 0],
['White rice', 130, 2.7, 28, 0.3, 0.4],
['Olive oil', 884, 0, 0, 100, 0],
['Banana', 89, 1.1, 23, 0.3, 2.6],
['Oats', 389, 17, 66, 6.9, 11],
['Whole wheat bread', 247, 13, 41, 3.4, 7],
['Eggs', 155, 13, 1.1, 11, 0],
['Milk', 42, 3.4, 5, 1, 0],
['Coconut oil', 862, 0, 0, 100, 0],
['Coffee', 2, 0.1, 0, 0, 0],
['White fish', 90, 18, 0, 1.3, 0],
['Salmon', 208, 20, 0, 13, 0],
['Shrimp', 99, 24, 0.2, 0.3, 0],
['Beef', 250, 26, 0, 15, 0],
['Lamb', 294, 25, 0, 21, 0],
['Beef hamburger meat', 254, 17, 0, 20, 0],
['Potatoes', 77, 2, 17, 0.1, 2.2],
['Chicken broth', 7, 1.1, 0.2, 0.2, 0],
['Vegetable cream', 195, 1.5, 5, 19, 0.3],
['Pumpkin', 26, 1, 6.5, 0.1, 0.5],
['Pumpkin and potato cream', 45, 1.2, 8, 1, 1],
['Ham', 145, 21, 1.5, 6, 0],
['Cheese', 402, 25, 1.3, 33, 0],
['Protein yoghurt', 70, 10, 4, 1.5, 0]
];
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
}
function populateExercises() {
var sheet = getSheet('Exercises');
var existing = sheet.getDataRange().getValues();
if (existing.length > 1) {
sheet.getRange(2, 1, existing.length - 1, existing[0].length).clear();
}
var data = [
['Dumbbell curl', 'Biceps'],
['Cable curl', 'Biceps'],
['Tricep pushdown', 'Triceps'],
['Overhead extension', 'Triceps'],
['Flat bench press', 'Chest'],
['45 degrees bench press', 'Chest'],
['Shoulder press', 'Chest'],
['Pull-up', 'Pull'],
['Row', 'Pull'],
['Squat', 'Legs'],
['Lunge', 'Legs'],
['Leg press', 'Legs'],
['Curl', 'Legs'],
['Extension', 'Legs'],
['Deadbug', 'Abs'],
['Cable lateral', 'Abs'],
['Lower back machine', 'Abs'],
['Crunch machine', 'Abs'],
['Reverse plank', 'Abs'],
['Crunch', 'Abs']
];
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
}
function populateStarterRoutines() {
var sheet = getSheet('Saved Routines');
var existing = sheet.getDataRange().getValues();
if (existing.length > 1) {
sheet.getRange(2, 1, existing.length - 1, existing[0].length).clear();
}
var data = [
['push1', 'Push Day', 'Flat bench press', 1],
['push1', 'Push Day', '45 degrees bench press', 2],
['push1', 'Push Day', 'Shoulder press', 3],
['push1', 'Push Day', 'Tricep pushdown', 4],
['push1', 'Push Day', 'Overhead extension', 5],
['pull1', 'Pull Day', 'Pull-up', 1],
['pull1', 'Pull Day', 'Row', 2],
['pull1', 'Pull Day', 'Dumbbell curl', 3],
['pull1', 'Pull Day', 'Cable curl', 4],
['legs1', 'Leg Day', 'Squat', 1],
['legs1', 'Leg Day', 'Leg press', 2],
['legs1', 'Leg Day', 'Lunge', 3],
['legs1', 'Leg Day', 'Curl', 4],
['legs1', 'Leg Day', 'Extension', 5]
];
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
}
// --- Action Handlers ---
function handleGetIngredients() {
return { success: true, data: getSheetData('Ingredients') };
}
function handleGetSavedMeals() {
var rows = getSheetData('Saved Meals');
return { success: true, data: groupBy(rows, 'Meal_ID') };
}
function handleSaveMeal(body) {
appendRows('Saved Meals', body.rows);
return { success: true, data: null };
}
function handleLogMeal(body) {
appendRows('Daily Meals', body.rows);
return { success: true, data: null };
}
function handleGetDailyMeals(body) {
var rows = getSheetData('Daily Meals');
var filtered = [];
for (var i = 0; i < rows.length; i++) {
if (normalizeDate(rows[i].Date) === body.date) {
filtered.push(rows[i]);
}
}
return { success: true, data: groupBy(filtered, 'Meal_ID') };
}
function handleDeleteDailyMeal(body) {
var sheet = getSheet('Daily Meals');
var data = sheet.getDataRange().getValues();
if (data.length < 2) return { success: true, data: null };
var headers = data[0];
var dateCol = headers.indexOf('Date');
var mealIdCol = headers.indexOf('Meal_ID');
var rowsToDelete = [];
for (var i = 1; i < data.length; i++) {
var rowDate = normalizeDate(data[i][dateCol]);
var rowMealId = String(data[i][mealIdCol]);
if (rowDate === body.date && rowMealId === String(body.mealId)) {
rowsToDelete.push(i + 1);
}
}
// Delete bottom-to-top to avoid index shifting
for (var j = rowsToDelete.length - 1; j >= 0; j--) {
sheet.deleteRow(rowsToDelete[j]);
}
return { success: true, data: null };
}
function handleGetExercises() {
return { success: true, data: getSheetData('Exercises') };
}
function handleGetSavedRoutines() {
var rows = getSheetData('Saved Routines');
return { success: true, data: groupBy(rows, 'Routine_ID') };
}
function handleSaveRoutine(body) {
appendRows('Saved Routines', body.rows);
return { success: true, data: null };
}
function handleLogWorkout(body) {
appendRows('Daily Workouts', body.rows);
return { success: true, data: null };
}
function handleGetDailyWorkout(body) {
var rows = getSheetData('Daily Workouts');
var filtered = [];
for (var i = 0; i < rows.length; i++) {
if (normalizeDate(rows[i].Date) === body.date) {
filtered.push(rows[i]);
}
}
return { success: true, data: groupBy(filtered, 'Routine_ID') };
}
function handleGetLastWorkoutWeights(body) {
var rows = getSheetData('Daily Workouts');
var matching = [];
for (var i = 0; i < rows.length; i++) {
if (String(rows[i].Routine_ID) === String(body.routineId)) {
matching.push(rows[i]);
}
}
if (matching.length === 0) return { success: true, data: [] };
// Find the most recent date
var latestDate = '';
for (var j = 0; j < matching.length; j++) {
var d = normalizeDate(matching[j].Date);
if (d > latestDate) latestDate = d;
}
var result = [];
for (var k = 0; k < matching.length; k++) {
if (normalizeDate(matching[k].Date) === latestDate) {
result.push(matching[k]);
}
}
return { success: true, data: result };
}
function handleGetGoals() {
var sheet = getSheet('Goals');
var data = sheet.getDataRange().getValues();
if (data.length < 2) return { success: true, data: null };
var headers = data[0];
var obj = {};
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = data[1][j];
}
return { success: true, data: obj };
}
function handleSaveGoals(body) {
var sheet = getSheet('Goals');
var data = sheet.getDataRange().getValues();
if (data.length > 1) {
sheet.getRange(2, 1, data.length - 1, data[0].length).clear();
}
var headers = data[0];
var row = [];
for (var j = 0; j < headers.length; j++) {
row.push(body.goals[headers[j]] !== undefined ? body.goals[headers[j]] : '');
}
sheet.appendRow(row);
return { success: true, data: null };
}
function handleGetBodyLog() {
return { success: true, data: getSheetData('Body Log') };
}
function handleLogBody(body) {
appendRows('Body Log', [body.entry]);
return { success: true, data: null };
}
function handleGetMealUsageCounts() {
var rows = getSheetData('Daily Meals');
var seen = {};
var counts = {};
for (var i = 0; i < rows.length; i++) {
var key = normalizeDate(rows[i].Date) + '|' + rows[i].Meal_ID;
if (!seen[key]) {
seen[key] = true;
var mid = String(rows[i].Meal_ID);
counts[mid] = (counts[mid] || 0) + 1;
}
}
return { success: true, data: counts };
}
function handleAnalyzeFood(body) {
if (!body.image) return { success: false, error: 'No image provided' };
var url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=' + GEMINI_API_KEY;
var payload = {
contents: [{
parts: [
{ text: 'You are a nutrition estimation assistant. Analyze this photo of food and estimate the nutritional content.\n\nRespond with ONLY a JSON object in this exact format:\n{\n "name": "Short meal name",\n "foods": [\n {\n "item": "Food name",\n "grams": <estimated total grams>,\n "calories_100g": <calories per 100g>,\n "protein_100g": <protein grams per 100g>,\n "carbs_100g": <carbs grams per 100g>,\n "fat_100g": <fat grams per 100g>\n }\n ]\n}\nList each distinct food item separately. Round all numbers to integers. Use metric units.' },
{ inline_data: { mime_type: 'image/jpeg', data: body.image } }
]
}]
};
var res = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
if (res.getResponseCode() !== 200) {
return { success: false, error: 'Gemini API error: ' + res.getContentText().substring(0, 200) };
}
var result = JSON.parse(res.getContentText());
var text = result.candidates[0].content.parts[0].text;
// Strip markdown fences if present
text = text.replace(/```json\s*/i, '').replace(/```\s*$/, '').trim();
var parsed = JSON.parse(text);
return { success: true, data: parsed };
}
function handleDescribeMeal(body) {
if (!body.text) return { success: false, error: 'No text provided' };
var url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=' + GEMINI_API_KEY;
var payload = {
contents: [{
parts: [
{ text: 'You are a nutrition estimation assistant. Estimate the nutritional content of the following meal description.\n\nIf the user specifies quantities, use those. If not, estimate reasonable portions.\n\nRespond with ONLY a JSON object in this exact format:\n{\n "name": "Short meal name",\n "foods": [\n {\n "item": "Food name",\n "grams": <estimated total grams>,\n "calories_100g": <calories per 100g>,\n "protein_100g": <protein grams per 100g>,\n "carbs_100g": <carbs grams per 100g>,\n "fat_100g": <fat grams per 100g>\n }\n ]\n}\nList each distinct food item separately. Round all numbers to integers. Use metric units.\n\nMeal description: ' + body.text }
]
}]
};
var res = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
if (res.getResponseCode() !== 200) {
return { success: false, error: 'Gemini API error: ' + res.getContentText().substring(0, 200) };
}
var result = JSON.parse(res.getContentText());
var text = result.candidates[0].content.parts[0].text;
text = text.replace(/```json\s*/i, '').replace(/```\s*$/, '').trim();
var parsed = JSON.parse(text);
return { success: true, data: parsed };
}
function handleAnalyzeFoodPaid(body) {
if (!body.image) return { success: false, error: 'No image provided' };
var url = 'https://api.anthropic.com/v1/messages';
var payload = {
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [{
role: 'user',
content: [
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/jpeg',
data: body.image
}
},
{
type: 'text',
text: 'You are a nutrition estimation assistant. Analyze this photo of food and estimate the nutritional content.\n\nRespond with ONLY a JSON object in this exact format:\n{\n "name": "Short meal name",\n "foods": [\n {\n "item": "Food name",\n "grams": <estimated total grams>,\n "calories_100g": <calories per 100g>,\n "protein_100g": <protein grams per 100g>,\n "carbs_100g": <carbs grams per 100g>,\n "fat_100g": <fat grams per 100g>\n }\n ]\n}\nList each distinct food item separately. Round all numbers to integers. Use metric units.'
}
]
}]
};
var res = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
headers: {
'x-api-key': ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
if (res.getResponseCode() !== 200) {
return { success: false, error: 'Claude API error: ' + res.getContentText().substring(0, 200) };
}
var result = JSON.parse(res.getContentText());
var text = result.content[0].text;
text = text.replace(/```json\s*/i, '').replace(/```\s*$/, '').trim();
var parsed = JSON.parse(text);
return { success: true, data: parsed };
}
function handleDescribeMealPaid(body) {
if (!body.text) return { success: false, error: 'No text provided' };
var url = 'https://api.anthropic.com/v1/messages';
var payload = {
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [{
role: 'user',
content: 'You are a nutrition estimation assistant. Estimate the nutritional content of the following meal description.\n\nIf the user specifies quantities, use those. If not, estimate reasonable portions.\n\nRespond with ONLY a JSON object in this exact format:\n{\n "name": "Short meal name",\n "foods": [\n {\n "item": "Food name",\n "grams": <estimated total grams>,\n "calories_100g": <calories per 100g>,\n "protein_100g": <protein grams per 100g>,\n "carbs_100g": <carbs grams per 100g>,\n "fat_100g": <fat grams per 100g>\n }\n ]\n}\nList each distinct food item separately. Round all numbers to integers. Use metric units.\n\nMeal description: ' + body.text
}]
};
var res = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
headers: {
'x-api-key': ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
if (res.getResponseCode() !== 200) {
return { success: false, error: 'Claude API error: ' + res.getContentText().substring(0, 200) };
}
var result = JSON.parse(res.getContentText());
var text = result.content[0].text;
text = text.replace(/```json\s*/i, '').replace(/```\s*$/, '').trim();
var parsed = JSON.parse(text);
return { success: true, data: parsed };
}
function handleAddIngredient(body) {
if (!body.ingredient || !body.ingredient.Name) return { success: false, error: 'No ingredient name provided' };
appendRows('Ingredients', [body.ingredient]);
return { success: true, data: null };
}
function handleDeleteBodyLog(body) {
var sheet = getSheet('Body Log');
var data = sheet.getDataRange().getValues();
if (data.length < 2) return { success: true, data: null };
var headers = data[0];
var dateCol = headers.indexOf('Date');
var weightCol = headers.indexOf('Weight_kg');
var fatCol = headers.indexOf('Fat_pct');
for (var i = data.length - 1; i >= 1; i--) {
var rowDate = normalizeDate(data[i][dateCol]);
if (rowDate === body.date && data[i][weightCol] == body.weight && data[i][fatCol] == body.fat) {
sheet.deleteRow(i + 1);
break;
}
}
return { success: true, data: null };
}