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)) def parse_tasks_file(): result = [] tasks = (journal_path / 'tasks').read_text().splitlines() for task in tasks: days, name = task.split(':') days = days.split(',') result.append((days, name)) return result tasks_file = parse_tasks_file() 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'] )) tasks = [] curr_day = { 0: 'mo', 1: 'tu', 2: 'we', 3: 'th', 4: 'fr', 5: 'sa', 6: 'su', }[datetime.now().weekday()] for days, name in tasks_file: if curr_day in days: tasks.append(name) 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}') if tasks: tasks_rendered = '\n'.join( f'[-] {task}' for task in tasks ) parts.append(f'Tasks:\n{tasks_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), '+'])