Skip to content

Instantly share code, notes, and snippets.

@lezwon
Created June 6, 2021 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lezwon/431a9264c914bf898b8d0fc1e62d1d97 to your computer and use it in GitHub Desktop.
Save lezwon/431a9264c914bf898b8d0fc1e62d1d97 to your computer and use it in GitHub Desktop.
import requests
import copy
import time
from datetime import datetime
import json
API_KEY = '<API KEY>'
USER_ID = '<USER ID>'
VC = '<VC>'
headers = {
'Authorization': API_KEY,
'Content-Length': '378',
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'Keep-Alive',
'Host': 'api.healthifyme.com',
'User-Agent': 'okhttp/4.9.0',
}
params = (
('vc', VC),
('auth_user_id', USER_ID),
)
data = {"install_id":"",
"sync_token":"",
"food_logs":[]
}
FOOD_ITEMS = {
"Chicken": {
"food_id":21519,
"food_measure_to_weight_map_id":73245,
"food_name":"Cooked Chicken Breast",
"calorie": 1.19,
},
"Soya": {
"food_id":14539,
"food_measure_to_weight_map_id":49914,
"food_name":"Soya Chunks Gravy",
"calorie": 1.005
},
"Salad": {
"food_id":21580,
"food_measure_to_weight_map_id":73425,
"food_name":"Veg Salad",
"calorie": 0.35972
},
"Banana":{
"food_id":18810,
"food_measure_to_weight_map_id":63841,
"food_name":"Yelakki Banana",
"calorie": 0.72
},
"Brown Rice": {
"food_id":10135,
"food_measure_to_weight_map_id":33176,
"food_name":"Boiled Brown Rice",
"calorie": 0.88038
}
}
def create_payload(food_name, weight):
new_data = copy.deepcopy(data)
food_data = FOOD_ITEMS[food_name]
new_data['food_logs'][0].update(food_data)
meal_type = 'B'
now = datetime.now()
hour = now.hour
if 7 <= hour < 11:
meal_type = 'B'
elif 12 <= hour < 15:
meal_type = 'L'
elif 7 <= hour < 10:
meal_type = 'D'
new_data['food_logs'][0].update({
"quantity": int(weight),
"calorie_value": weight * food_data['calorie'],
"measure_weight": int(weight),
"entry_time": now.strftime("%Y-%m-%d"),
"meal_type": meal_type,
"device_log_time": int(time.time())
})
return json.dumps(new_data)
def make_request(data):
return requests.post('https://api.healthifyme.com/api/v1/foodlog/foodlog_sync/', headers=headers, params=params, data=data)
def log(food_name, weight):
data = create_payload(food_name, weight)
response = make_request(data)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment