Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:34
Show Gist options
  • Save angelabauer/164864b78175bb1ecd3d3fd7f4ee39b7 to your computer and use it in GitHub Desktop.
Save angelabauer/164864b78175bb1ecd3d3fd7f4ee39b7 to your computer and use it in GitHub Desktop.
import requests
from datetime import datetime
GENDER = YOUR GENDER
WEIGHT_KG = YOUR WEIGHT
HEIGHT_CM = YOUR HEIGHT
AGE = YOUR AGE
APP_ID = YOUR NUTRITIONIX APP ID
API_KEY = YOUR NUTRITIONIX API KEY
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = YOUR SHEETY ENDPOINT
exercise_text = input("Tell me which exercises you did: ")
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}
response = requests.post(exercise_endpoint, json=parameters, headers=headers)
result = response.json()
################### Start of Step 4 Solution ######################
today_date = datetime.now().strftime("%d/%m/%Y")
now_time = datetime.now().strftime("%X")
for exercise in result["exercises"]:
sheet_inputs = {
"workout": {
"date": today_date,
"time": now_time,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
sheet_response = requests.post(sheet_endpoint, json=sheet_inputs)
print(sheet_response.text)
@matijacrcic
Copy link

@designrevolutions It is just a stylistic choice. The title method only impacts the exercise value (exercise["name"]) and has no impact on the key value ( "exercise") as you noted. At the end of the day it is a personal preference, do you want the value of your exercise to start with lower or upper case in your spreadsheet.

@Simonmmr
Copy link

Simonmmr commented Jul 16, 2024

can somebody help me with the issue below error code ?

"detail": "Bad Request. The JSON payload should be inside a root property called 'sheet1'

edited-- problem solved

@SouvikSaha449
Copy link

I was having the same problem but I realized that I Capitalized the first letter on the variable in the json input for my sheet. It shoul be: "sheet1": { "date": today_date, (# date instead of Date) "time": time_now, (# time instead of Time) "exercise": exercise["name"].title(), (# exercise instead of Exercise) "duration": exercise["duration_min"], (# duration instead of Duration) "calories": exercise["nf_calories"] (# calories instead of Calories ) }
I hope it can help you

It does help. Thanks

duration_problem
I am still having this problem after trying out your suggestion

@Surajjoshi2004
Copy link

import requests
from datetime import datetime

Constants

APP_ID = "c99c6cf"
API_KEY = "3dfaae2e9eac16f7ed4f1af651c03a4"
WEIGHT_KG = "73"
HEIGHT = "173.736"
AGE = "20"
GENDER = "male"

Get current date and time

today_date = datetime.now()
date_t = today_date.strftime("%d/%m/%Y")
time_t = today_date.strftime("%X")

Nutritionix API endpoint

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_text = "ran for 3 hour"

User parameters for API request

user_params = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT,
"age": AGE,
}

Headers for API request

headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}

Make the POST request to Nutritionix API

response = requests.post(url=exercise_endpoint, json=user_params, headers=headers)
data = response.json()

Sheety API endpoint

sheet_endpoint = "https://api.sheety.co/009b49043cda1f3da49c0f9e38dd8748/workoutsheety/workouts"

Loop through the exercises and log each to the sheet

for exercise in data["exercises"]:
sheet_params = {
"workout":
{
"date": date_t,
"time": time_t,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories":exercise["nf_calories"],
}
}

sheet_response = requests.post(url=sheet_endpoint,json=sheet_params)
print(sheet_response.text)

@Surajjoshi2004
Copy link

{
"errors": [
{
"detail": "Cannot read property '0' of undefined"
}
]
help me resolve at this point it is frustrating. my api keys and all the things are all right

import requests
from datetime import datetime

Constants

APP_ID = "c99cc2f"
API_KEY = "3dfaae2e9ea16f7ed4f1af65c1c03a4"
WEIGHT_KG = "73"
HEIGHT = "173.736"
AGE = "20"
GENDER = "male"

Get current date and time

today_date = datetime.now()
date_t = today_date.strftime("%d/%m/%Y")
time_t = today_date.strftime("%X")

Nutritionix API endpoint

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_text = "ran for 3 hour"

User parameters for API request

user_params = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT,
"age": AGE,
}

Headers for API request

headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}

Make the POST request to Nutritionix API

response = requests.post(url=exercise_endpoint, json=user_params, headers=headers)
data = response.json()

Sheety API endpoint

sheet_endpoint = "https://api.sheety.co/009b49043cda1f3da49c0f9e38dd8748/workoutsheety/workouts"

Loop through the exercises and log each to the sheet

for exercise in data["exercises"]:
sheet_params = {
"workout":
{
"date": date_t,
"time": time_t,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories":exercise["nf_calories"],
}
}

sheet_response = requests.post(url=sheet_endpoint,json=sheet_params)
print(sheet_response.text)

@zamranhussaini
Copy link

can anyone explain why we used a loop here -

for exercise in data["exercises"]:

I don't really get it. Why wouldn't it work without a loop. I initially coded it without a loop and everything was running fine but my data wasn't getting uploaded to the sheet.

I was also using constants like these -

NAME = exercise_dict["exercises"][0]["name"]
DURATION = exercise_dict["exercises"][0]["duration_min"]
CALORIES = exercise_dict["exercises"][0]["nf_calories"]

so that might've also caused an error.

@FabioERodrigues
Copy link

Tell me which exercises you did: Running 5k { "errors": [ { "detail": "The caller does not have permission" } ] }

Process finished with exit code 0

Please help me...

This is because u need to make a copy of the google sheets provided and link that to the sheety project. It doesn't work because u tried to link her google sheet.

@Prathap0703
Copy link

can somebody help me with the issue below error code ?

"detail": "Bad Request. The JSON payload should be inside a root property called 'sheet1'

edited-- problem solved

Bro I'm having the same problem . Can you help me to sort it out please

@ilsologheo00
Copy link

Hi everyone, has anyone ever experienced this bug?
I did everything as intended, I get a 200 code response with the ID of the new workout added.
The only problem is that my sheet is missing the row with the new exercise, it is only showing the first row.
Permissions are fine, at least from a sheety point of view, am I missing something?

@VasilisNeokosmidis
Copy link

Surajjoshi2004

I need Help with this as well

@NOVAVENoM007
Copy link

I am getting this error when running my code. Can someone help me please? I enabled the POST behavior and anyone with a link should have access to the spreadsheet. I get the same error when running my own code and also when running angela's code. Anyone? { "errors": [ { "detail": "The caller does not have permission" } ] }

If you are having this problem, is because you are trying to connect to Angela´s Google Sheets, you need to create your own from your google account, what you need to is:

  1. In the google sheet that you are using, go to: File>Make copy
  2. Save as new
  3. When the new window opens, copy the URL of google sheet
  4. Create a new project in Sheety.co (Using the new google sheet URL)

It worked for me also,
thanx buddy, you saved me from the verge of quitting coding !!!

@benzwy
Copy link

benzwy commented Dec 18, 2024

showing duration time wrong in sheets it showing correctly in console [output} Screenshot (390)

You need to have atleast one entry without "mins" just number for the duration.

I am getting this exact same error, it lists all durations in the spreadsheet as 01min, no matter what length they are in the input and console. inputting entries with just numbers (e.g. 40 pushups) doesn't help. I am completely lost as to why this is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment