This commit is contained in:
olari
2021-06-01 12:53:48 +03:00
parent 6e07d120a2
commit 557df1bb20
6 changed files with 101 additions and 1199 deletions

View File

@@ -1,49 +1,32 @@
from pathlib import Path
from datetime import datetime
from collections import Counter
from functools import reduce
import re
import string
import json
import sys
do_yesterday = len(sys.argv) > 1
from common import parse_foods_file
from common import parse_foods_file, format_timestamp
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]+)')
do_yesterday = len(sys.argv) > 1
day_index = -2 if do_yesterday else -1
current_day = list(sorted((Path.home() / 'workspace' /
'journal').glob('*.md')))[day_index]
journal = json.load(open('journal.json'))
header, *tmp = entry_re.split(current_day.read_text())
entries = list(zip(tmp[::2], tmp[1::2]))
current_day = list(journal)[day_index]
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')
)
for entry in journal[current_day]['entries']:
has_printed = False
entry_calories = 0.0
entry_protein = 0.0
for diet in diet_re.finditer(content):
for diet in (b for b in entry['blocks'] if type(b) != str and b['type'] == 'diet'):
if not has_printed:
print(f'-- {timestamp}')
print(f'-- {format_timestamp(entry["timestamp"])}')
has_printed = True
value, name = diet.groups()
value = float(value.removesuffix('g'))
value = diet['amount']
name = diet['food']
if name in recipes:
food = recipes[name]
@@ -58,8 +41,7 @@ for (timestamp, content) in sorted(entries, key=lambda x: x[0]):
food = {k: v*(value/100.0) for k,v in foods[name].items()}
else:
breakpoint()
print(f'ERROR: Invalid diet entry: {content}')
print(f'ERROR: Invalid diet entry: {diet}')
continue
protein = round(food.get('Protein', 0.0), 2)