-
-
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) |
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...
Did you get your problem fixed?
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 helped me. Thank you so much
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
Yes it helped
Thankyou
Thank you for helping
Why do we need to use "workout" when our sheet is named "workouts" in the json we use in our PUT. Can someone explain why this is?
@Calab
Because that's what Sheety want you to do. Their API, their rules!
There is some logic behind it though - you are only adding (or modifying) one workout at a time to the sheet.
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:
- In the google sheet that you are using, go to: File>Make copy
- Save as new
- When the new window opens, copy the URL of google sheet
- Create a new project in Sheety.co (Using the new google sheet URL)
i did the input directly in the params
params = {
"query": input("What exercise did you do today? ")
}
i get an error message saying the caller does not have permission to access when ".post" is about to run to place the strings into the spreadsheet
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:
- In the google sheet that you are using, go to: File>Make copy
- Save as new
- When the new window opens, copy the URL of google sheet
- Create a new project in Sheety.co (Using the new google sheet URL)
Thank you for this: it solved my issue. Why did i have to copy the google sheet, though - why didn't the original one work? was the original one not located properly in my own google account?
error updating sheety 400 Client Error: Bad Request for url: https://api.sheety.co/***********************/myWorkout/workouts
I am getting this error, i don't know where I am doing wrong I have tried everything
I am getting {
"code": 404,
"message": "Not Found. Endpoint doesn't exist."
} as response to my API POST request.
I have connected and reconnected sheety to google account, have given permission.
I am stuck here and need help from the community what I have done wrong. below is the code I have tried.
import requests
from datetime import datetime
import os
GENDER = "male"
WEIGHT_KG = 78.5
HEIGHT_CM = 173
AGE = 40
SHEET_USERNAME = os.environ.get("SHEET_USERNAME")
SHEET_PASSWORD = os.environ.get("SHEET_PASSWORD")
API_KEY = "efdd54e40157a71cc5497420eabd476d"
APP_ID = "cff4af8e"
date_format = "%d/%m/%Y"
time_format = "%X"
current_date = datetime.now()
date = current_date.strftime(date_format)
time = current_date.strftime(time_format)
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = "https://api.sheety.co/e6f13bd13d2faee38457979b24689524/myWorkouts/workouts"
exercise_text = input("Tell me what exercise you did today?")
params = {"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE}
headers = {"x-app-id": APP_ID,
"x-app-key": API_KEY}
response = requests.post(exercise_endpoint, json=params, headers=headers)
results = response.json()
for exercise in results['exercises']:
sheet_inputs = {
"workout": {
"date": date,
"time": time,
"exercise": exercise['name'].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
print(sheet_inputs["workout"])
sheet_response = requests.put(sheet_endpoint, json=sheet_inputs, auth=(SHEET_USERNAME, SHEET_PASSWORD))
print(sheet_response.text)
@abhishekdconviction
I think you may need to use POST rather than PUT. PUT is idempotent and is usually used for updating. POST is used for creating.
See 1. Difference between PUT and POST
thank you so much, it is running fine.
"exercise": exercise["name"].title(),
I don't understand why we need to use title() here and why we were told it was needed?
exercise["name"].title() - this gets us the content, for which the casing doesn't matter (correct me if I'm wrong).
the API needs camel case for the keys: date, time, exercise, duration and calories. As there is no second word in these names, all are OK kept as lowercase.
I'm sure I'm missing something here.
@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.
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
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 youIt does help. Thanks
I am still having this problem after trying out your suggestion
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)
{
"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)
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.
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.
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
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?
I need Help with this as well
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:
- In the google sheet that you are using, go to: File>Make copy
- Save as new
- When the new window opens, copy the URL of google sheet
- 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 !!!
showing duration time wrong in sheets it showing correctly in console [output}
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.
Maybe because you're not looping through the exercises the nutritionix api returns