Files
journal/summary.py
2021-06-18 03:50:32 +03:00

64 lines
1.7 KiB
Python

import json
import sys
from common import parse_foods_file, format_timestamp
foods, recipes = parse_foods_file()
do_yesterday = len(sys.argv) > 1
day_index = -2 if do_yesterday else -1
journal = json.load(open('journal.json'))
current_day = list(journal)[day_index]
daily_grams = 0.0
daily_calories = 0.0
daily_protein = 0.0
for entry in journal[current_day]['entries']:
has_printed = False
entry_calories = 0.0
entry_protein = 0.0
for diet in (b for b in entry['blocks'] if type(b) != str and b['type'] == 'diet'):
if not has_printed:
print(f'-- {format_timestamp(entry["timestamp"])}')
has_printed = True
value = diet['amount']
name = diet['food']
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:
print(f'ERROR: Invalid diet entry: {diet}')
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)')