Skip to content

Commit 0ab1f2e

Browse files
authored
Add files via upload
1 parent 2775ee8 commit 0ab1f2e

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

day-38/ui_workout_tracker.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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?\nExamples:\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+
)

day-38/workout_tracker.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
import os
3+
from datetime import datetime
4+
5+
x_app_id = os.environ.get("x_app_id")
6+
x_app_key = os.environ.get("x_app_key")
7+
nuti_url = os.environ.get("nuti_url")
8+
header = {
9+
'x-app-id':x_app_id,
10+
'x-app-key':x_app_key,
11+
}
12+
query = {
13+
'gender':'male',
14+
'weight_kg': 85,
15+
'height_cm':180.33,
16+
'age':25,
17+
'query':input("How much exercise did you do today?")
18+
}
19+
response = requests.post(url=nuti_url, json=query, headers=header,)
20+
nutri_data = response.json()
21+
print(nutri_data)
22+
23+
Sheety_url = os.environ.get("Sheety_url")
24+
response_sheety = requests.get(url=Sheety_url)
25+
26+
27+
today = datetime.now()
28+
date = today.strftime("%Y/%m/%d")
29+
time = today.strftime("%H:%M:%S")
30+
for exercise in nutri_data['exercises']:
31+
data = {
32+
"sheet1": {
33+
"date": date,
34+
"time": time,
35+
"exercise": exercise['name'],
36+
"duration": exercise['duration_min'],
37+
"calories": exercise['nf_calories'],
38+
}
39+
}
40+
header_json = {
41+
'Content-Type': 'application/json'
42+
}
43+
44+
response_sheety = requests.post(url=Sheety_url, headers=header_json, json=data, auth=('iron_man','ironman422'))
45+
print(response_sheety.status_code)
46+
print(response_sheety.text)

0 commit comments

Comments
 (0)