note-processor/main.py

66 lines
2.3 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 False
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}")
# formatted_notes is now used in the logic below
except OSError as e:
logging.error(f"Error accessing directory {daily_notes_path}: {e}")
return False
# Logic for updating 'All Daily Notes.md' moved here
all_daily_notes_path = os.path.join(NOTES_DIR, 'All Daily Notes.md')
new_content = "\n\n".join(formatted_notes)
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 False
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}")
return False
else:
logging.info(f"No changes detected for {all_daily_notes_path}. File not updated.")
return True
def main():
if collect_daily_notes():
logging.info("Daily notes processing completed successfully.")
else:
logging.error("Daily notes processing failed.")
if __name__ == '__main__':
main()