note-processor/main.py

63 lines
2.2 KiB
Python

import os, logging
DEBUG = os.environ.get('DEBUG')
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO)
NOTES_DIR = '/home/tanner/notes-git/notes'
def collect_daily_notes():
daily_notes_path = os.path.join(NOTES_DIR, 'Daily Notes')
if not os.path.isdir(daily_notes_path):
logging.error(f"Directory not found: {daily_notes_path}")
return []
try:
all_files = os.listdir(daily_notes_path)
md_files = [f for f in all_files if os.path.isfile(os.path.join(daily_notes_path, f)) and f.endswith('.md')]
# Sort in reverse alphabetical order
md_files.sort(reverse=True)
# Transform to the desired format
formatted_notes = [f"![[{f[:-3]}]]" for f in md_files]
logging.debug(f"Formatted notes: {formatted_notes}")
return formatted_notes
except OSError as e:
logging.error(f"Error accessing directory {daily_notes_path}: {e}")
return []
def main():
daily_notes_list = collect_daily_notes()
if not daily_notes_list:
logging.info("No daily notes found or an error occurred. Skipping update of 'All Daily Notes.md'.")
return
all_daily_notes_path = os.path.join(NOTES_DIR, 'All Daily Notes.md')
new_content = "\n\n".join(daily_notes_list)
try:
with open(all_daily_notes_path, 'r') as f:
existing_content = f.read()
except FileNotFoundError:
existing_content = ""
logging.info(f"File not found: {all_daily_notes_path}. A new file will be created.")
except OSError as e:
logging.error(f"Error reading file {all_daily_notes_path}: {e}")
return
if new_content.strip() != existing_content.strip():
try:
with open(all_daily_notes_path, 'w') as f:
f.write(new_content)
logging.info(f"Updated {all_daily_notes_path}")
except OSError as e:
logging.error(f"Error writing to file {all_daily_notes_path}: {e}")
else:
logging.info(f"No changes detected for {all_daily_notes_path}. File not updated.")
if __name__ == '__main__':
main()