-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood_logger.py
161 lines (134 loc) · 3.81 KB
/
food_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import copy
import time
from datetime import datetime
import json
import os
from dotenv import load_dotenv
load_dotenv()
headers = {
"Authorization": os.environ['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", os.environ['VC']),
("auth_user_id", os.environ['USER_ID']),
)
data = {
"install_id": "162079231866135970",
"sync_token": "1.621529742997383E9",
"food_logs": [
{
"local_id": 5052,
"server_id": -1,
"food_id": 21519,
"quantity": 200,
"calorie_value": 238,
"measure_weight": 200,
"entry_time": "2021-05-20",
"meal_type": "D",
"food_measure_to_weight_map_id": 73245,
"food_name": "Cooked Chicken Breast",
"isdeleted": 0,
"source": "N",
"log_source": "0",
"device_log_time": 1621529874,
}
],
}
try:
with open("foods.json") as file:
FOOD_ITEMS = json.load(file)
except:
FOOD_ITEMS = {}
def update_food_dict(food_name, data):
with open("foods.json", "w+") as file:
for measure in data["food_measures"]:
if measure["measure_name_weight"] == "grams (1 gms)":
FOOD_ITEMS[food_name] = {
"food_id": measure["food_id"],
"food_measure_to_weight_map_id": measure["measure_id"],
"food_name": food_name,
"calorie": measure["calorie"],
}
json.dump(FOOD_ITEMS, file)
def create_payload(food_name, weight):
new_data = copy.deepcopy(data)
try:
food_data = FOOD_ITEMS[food_name]
except KeyError:
food_id = search_food(food_name)
if food_id:
food_data = fetch_details(food_id)
if food_data:
update_food_dict(food_name, food_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 19 <= hour < 22:
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
def search_food(food_name):
params = (
("query", food_name),
("meal_type", "D"),
("country_code", "US"),
("vc", "816"),
("auth_user_id", "17794667"),
)
response = requests.get(
"https://api.healthifyme.com/search-food/api/v1/",
headers=headers,
params=params,
)
data = response.json()
if data:
return data[0]["food_id"]
else:
return None
def fetch_details(food_id):
params = (
("food_id", food_id),
("vc", "816"),
("auth_user_id", "17794667"),
)
response = requests.get(
"https://api.healthifyme.com/api/v2/food/details",
headers=headers,
params=params,
)
data = response.json()
return data
if __name__ == "__main__":
log("Veg Salad", 187)