modern_lisp-machine/misc/scripts/recent_journal.py

72 lines
2.7 KiB
Python

#!/usr/bin/env python3
import os
import re
# Path to the folder containing Org mode files
org_folder_path = '/mnt/Data/Documents/org-files/org-roam/daily/'
# Regular expression pattern to match timestamped headers
pattern = r'^\* ([0-9]{2}:[0-9]{2})$'
# Function to extract the most recent entry content from an Org mode file
def extract_most_recent_entry_content(org_file_path):
recent_entry_content = []
recent_entry_timestamp = None
in_properties_section = False
with open(org_file_path, 'r') as file:
for line in file:
line = line.strip()
match = re.match(pattern, line)
if match:
# Capture the timestamp of the most recent entry
recent_entry_timestamp = match.group(1)
recent_entry_content = []
in_properties_section = False # Reset flag for properties section
elif line.startswith(':properties:'):
in_properties_section = True
elif line.startswith(':end:') and in_properties_section:
in_properties_section = False
elif not in_properties_section and line:
# Append non-empty lines outside properties section to recent entry content
recent_entry_content.append(line)
return recent_entry_timestamp, recent_entry_content
# Function to find the most recent Org mode file in a folder
def find_most_recent_org_file(folder_path):
# Get a list of all files in the folder
files = os.listdir(folder_path)
org_files = [f for f in files if f.endswith('.org')]
if not org_files:
print("No Org mode files found in the specified folder.")
return None
# Sort files by modification time (most recent first)
org_files.sort(key=lambda f: os.path.getmtime(os.path.join(folder_path, f)), reverse=True)
# Choose the most recent Org mode file
most_recent_org_file = os.path.join(folder_path, org_files[0])
return most_recent_org_file
# Find the most recent Org mode file in the specified folder
most_recent_file_path = find_most_recent_org_file(org_folder_path)
# If a recent file is found, extract and display the most recent entry content
if most_recent_file_path:
recent_entry_timestamp, recent_entry_content = extract_most_recent_entry_content(most_recent_file_path)
if recent_entry_content:
# Print the header with the timestamp of the most recent entry
if recent_entry_timestamp:
print(f"Most Recent Entry ({recent_entry_timestamp}):")
# Print the content of the most recent entry
print('\n'.join(recent_entry_content))
else:
print("No recent entry found in the Org mode file.")
else:
print("No Org mode files found in the specified folder.")