152 lines
4.0 KiB
Python
152 lines
4.0 KiB
Python
from pathlib import Path
|
|
from datetime import datetime
|
|
from collections import Counter
|
|
from functools import reduce
|
|
import re
|
|
import string
|
|
|
|
import sys
|
|
|
|
def parse_foods_file():
|
|
path = Path.home() / 'projects' / 'open-journal' / 'foods'
|
|
text = path.read_text()
|
|
foods, recipes = text.split('---')
|
|
|
|
def parse_macro(macro):
|
|
if macro == '...':
|
|
return ('INVALID', 0.0)
|
|
|
|
name, value = macro.split()
|
|
value = float(value.removesuffix('g').removesuffix('kcal'))
|
|
return (name, value)
|
|
|
|
foods = {
|
|
macros[0]: dict(parse_macro(macro) for macro in macros[1:])
|
|
for macros in [food.split('\n') for food in foods.strip().split('\n\n')]
|
|
}
|
|
|
|
def combine_values(fst, snd):
|
|
result = fst.copy()
|
|
for k,v in snd.items():
|
|
if k in fst:
|
|
result[k] += v
|
|
else:
|
|
result[k] = v
|
|
return result
|
|
|
|
def evaluate_ingredients(ingredients):
|
|
result = {}
|
|
|
|
total_weight = 0.0
|
|
for ingredient in ingredients:
|
|
k,v = parse_macro(ingredient)
|
|
if k == 'TOTAL':
|
|
result[k] = v
|
|
break
|
|
else:
|
|
total_weight += v
|
|
|
|
|
|
food = foods[k]
|
|
|
|
for kk,vv in food.items():
|
|
if kk not in result:
|
|
result[kk] = 0.0
|
|
|
|
result[kk] += vv * (v/100.0)
|
|
|
|
if 'TOTAL' not in result:
|
|
result['TOTAL'] = total_weight
|
|
|
|
return result
|
|
|
|
recipes = {
|
|
ingredients[0]: evaluate_ingredients(ingredients[1:])
|
|
for ingredients in [
|
|
recipe.split('\n') for recipe in recipes.strip().split('\n\n')
|
|
]
|
|
}
|
|
|
|
def get_calories_from_macros(mm):
|
|
calories = 0.0
|
|
for k,v in mm.items():
|
|
calories += v * {
|
|
'Carbs': 4,
|
|
'Fat': 9,
|
|
'Protein': 4
|
|
}.get(k, 0.0)
|
|
return calories
|
|
|
|
#for k,v in foods.items():
|
|
# print(round(v.get('Energy') - get_calories_from_macros(v)), k)
|
|
|
|
return foods, recipes
|
|
|
|
foods, recipes = parse_foods_file()
|
|
|
|
entry_re = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ', re.MULTILINE)
|
|
diet_re = re.compile(r'@diet (\d+g) ([a-zA-Z]+)')
|
|
|
|
current_day = list(sorted((Path.home() / 'workspace' /
|
|
'journal').glob('*.md')))[-1]
|
|
|
|
header, *tmp = entry_re.split(current_day.read_text())
|
|
entries = list(zip(tmp[::2], tmp[1::2]))
|
|
|
|
daily_grams = 0.0
|
|
daily_calories = 0.0
|
|
daily_protein = 0.0
|
|
|
|
for (timestamp, content) in sorted(entries, key=lambda x: x[0]):
|
|
content = '\n'.join(
|
|
part.replace('\n', ' ')
|
|
for part in content.split('\n\n')
|
|
)
|
|
|
|
has_printed = False
|
|
entry_calories = 0.0
|
|
entry_protein = 0.0
|
|
for diet in diet_re.finditer(content):
|
|
if not has_printed:
|
|
print(f'-- {timestamp}')
|
|
has_printed = True
|
|
|
|
value, name = diet.groups()
|
|
value = float(value.removesuffix('g'))
|
|
|
|
if name in recipes:
|
|
food = recipes[name]
|
|
|
|
if value == 0.0:
|
|
value = food['TOTAL']
|
|
|
|
food = {k: v*(value/food['TOTAL']) for k,v in food.items()}
|
|
elif name in foods:
|
|
if value == 0.0:
|
|
value = 100
|
|
|
|
food = {k: v*(value/100.0) for k,v in foods[name].items()}
|
|
else:
|
|
breakpoint()
|
|
print(f'ERROR: Invalid diet entry: {content}')
|
|
continue
|
|
|
|
protein = round(food.get('Protein', 0.0), 2)
|
|
calories = round(food.get('Energy', 0.0), 2)
|
|
|
|
entry_calories += calories
|
|
entry_protein += protein
|
|
|
|
print(f'{name:<20} {value:<6}g, {calories:<6}kcal, {protein:<6}g protein')
|
|
|
|
if has_printed:
|
|
entry_calories = round(entry_calories, 2)
|
|
entry_protein = round(entry_protein, 2)
|
|
print(f'-- TOTAL: {entry_calories}kcal, {entry_protein}g protein')
|
|
print()
|
|
|
|
daily_calories += entry_calories
|
|
daily_protein += entry_protein
|
|
|
|
print(f'-- DAILY TOTAL ({daily_calories}kcal, {daily_protein}g protein)')
|