Day 38 - Environment variable & Workout Tracker


Posted by pei_______ on 2022-05-19

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


API INFO

OpenAI API

nutritionix API

Sheety API


Terminal 停用指令碼問題解決

Set-ExecutionPolicy 設置說明


Environment variable 環境變數設定

Environment variable - wiki

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())

#Python #課堂筆記 #100 Days of Code







Related Posts

謂詞邏輯(Predicate Logic)

謂詞邏輯(Predicate Logic)

GitHub Actions CI:導入與優化 ESLint 檢查

GitHub Actions CI:導入與優化 ESLint 檢查

Express

Express


Comments