learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022
API INFO
Terminal 停用指令碼問題解決
Environment variable 環境變數設定
How do I access environment variables in Python?
How to set environment variables in PyCharm?
Workout Tracker
# Constant
import requests
import os
from datetime import datetime
APP_ID = os.environ['APP_ID']
API_KEY = os.environ["API_KEY"]
EXCEL_USERNAME = os.environ["USERNAME"]
EXCEL_PASSWORD = os.environ["PASSWORD"]
workout_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = os.environ["SHEET_ENDPOINT"]
# auto-calculate by Nutritionix
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}
query = input("Tell me which exercises you did: ")
user_info = {
"query": query,
"gender": "female",
"weight_kg": 65,
"height_cm": 164.5,
"age": 26
}
response = requests.post(url=workout_endpoint,
json=user_info,
headers=headers)
response.raise_for_status()
result = response.json()["exercises"][0]
# record data by Sheety
record_in_excel = {
"workout": {
"date": datetime.today().strftime("%d/%m/%Y"),
"time": datetime.today().time().strftime("%X"),
"exercise": result["name"].title(),
"duration": round(result["duration_min"], 2),
"calories": round(result["nf_calories"], 2)
}
}
excel_response = requests.post(url=sheet_endpoint,
json=record_in_excel,
auth=(EXCEL_USERNAME, EXCEL_PASSWORD))
excel_response.raise_for_status()
print(excel_response.json())