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 (script_path / 'habits').read_text().strip().split('\n')]) words = (script_path / 'godword').read_text().strip().split('\n') godword = '\n'.join(' '.join(random.choice(words) for a 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'] )) notifications_rendered = '\n'.join( f'[[{entry}]] {message}' for entry, message in notifications ) target_page.write_text(f'''\ # {target_page.stem} Godword: {godword} Habits: {habits} Notifications: {notifications_rendered} ''') with open(target_page, 'a') as fp: fp.write(f'\n{current_date} {current_time} ') run(['nvim', str(target_page), '+'])