Files
journal/open-journal.py
2021-06-19 11:54:25 +03:00

67 lines
1.9 KiB
Python

from subprocess import run, Popen, DEVNULL
from datetime import datetime
from pathlib import Path
import random
import sys
import json
from common import format_timestamp
current_date = datetime.now().strftime('%Y-%m-%d')
current_time = datetime.now().strftime('%H:%M:%S')
journal_path = Path.home() / 'workspace' / 'journal'
target_page = journal_path / f'{current_date}.md'
script_path = Path(__file__).parent
habits = '\n'.join([f'[-] {x}'
for x in (journal_path / 'habits').read_text().strip().split('\n')])
words = (journal_path / 'godword').read_text().strip().split('\n')
godword = '\n'.join(' '.join(random.choice(words)
for __ in range(10)) for _ in range(2))
if not target_page.exists():
Popen(
['bash', str(script_path / 'backup-script.sh'), current_date+'.zip'],
cwd=str(journal_path), stdout=DEVNULL, stderr=DEVNULL
)
journal = json.load(open(script_path / 'journal.json'))
notifications = []
for day, v in journal.items():
for entry in v.get('entries'):
for block in entry['blocks']:
if not isinstance(block, str) and block['type'] == 'notify':
if block['day'] == current_date:
notifications.append((
format_timestamp(entry['timestamp']),
block['message']
))
parts = [
f'# {target_page.stem}',
f'Godword:\n{godword}',
f'Habits:\n{habits}',
]
if notifications:
notifications_rendered = '\n'.join(
f'[[{entry}]] {message}'
for entry, message in notifications
)
parts.append(f'Notifications:\n{notifications_rendered}')
header = '\n\n'.join(parts) + '\n'
target_page.write_text(header)
with open(target_page, 'a') as fp:
fp.write(f'\n{current_date} {current_time} ')
run(['nvim', str(target_page), '+'])