1
+ import requests
2
+ import os
3
+ import tkinter as tk
4
+ from tkinter import simpledialog
5
+ from datetime import datetime
6
+
7
+ # ---------------------------- NLP-POWERED WORKOUT TRACKER ------------------------------- #
8
+ def get_exercise_description ():
9
+ root = tk .Tk ()
10
+ root .withdraw () # Hide main window
11
+
12
+ # Create custom dialog with better prompting
13
+ user_input = simpledialog .askstring (
14
+ "Workout Tracker 🤖" ,
15
+ "What did you do today?\n Examples:\n - 'Ran 5k in 30 minutes'\n - 'Swam 20 laps'\n - '45 minute weight training session'" ,
16
+ parent = root
17
+ )
18
+ root .destroy ()
19
+ return user_input
20
+
21
+ # ---------------------------- API CONFIGURATION ------------------------------- #
22
+ # Environment variables for security
23
+ NUTRITIONIX_APP_ID = os .environ .get ("x_app_id" )
24
+ NUTRITIONIX_API_KEY = os .environ .get ("x_app_key" )
25
+ NUTRITIONIX_ENDPOINT = os .environ .get ("nuti_url" )
26
+ SHEETY_ENDPOINT = os .environ .get ("Sheety_url" )
27
+
28
+ headers = {
29
+ 'x-app-id' : NUTRITIONIX_APP_ID ,
30
+ 'x-app-key' : NUTRITIONIX_API_KEY ,
31
+ }
32
+
33
+ # ---------------------------- NLP PROCESSING ------------------------------- #
34
+ # Get natural language input through GUI
35
+ exercise_query = get_exercise_description ()
36
+
37
+ if exercise_query : # Only proceed if user provided input
38
+ # Nutritionix's NLP parses natural language into structured data
39
+ nutritionix_params = {
40
+ 'gender' : 'male' ,
41
+ 'weight_kg' : 85 ,
42
+ 'height_cm' : 180.33 ,
43
+ 'age' : 25 ,
44
+ 'query' : exercise_query
45
+ }
46
+
47
+ # Get exercise data from Nutritionix API
48
+ response = requests .post (
49
+ url = NUTRITIONIX_ENDPOINT ,
50
+ json = nutritionix_params ,
51
+ headers = headers
52
+ )
53
+ exercise_data = response .json ()
54
+
55
+ # ---------------------------- DATA LOGGING ------------------------------- #
56
+ # Prepare timestamp
57
+ today = datetime .now ()
58
+
59
+ # Log to Google Sheets via Sheety
60
+ for exercise in exercise_data ['exercises' ]:
61
+ log_entry = {
62
+ "sheet1" : {
63
+ "date" : today .strftime ("%Y/%m/%d" ),
64
+ "time" : today .strftime ("%H:%M:%S" ),
65
+ "exercise" : exercise ['name' ],
66
+ "duration" : exercise ['duration_min' ],
67
+ "calories" : exercise ['nf_calories' ]
68
+ }
69
+ }
70
+
71
+ response = requests .post (
72
+ url = SHEETY_ENDPOINT ,
73
+ json = log_entry ,
74
+ headers = {'Content-Type' : 'application/json' },
75
+ auth = ('iron_man' , 'ironman422' )
76
+ )
0 commit comments