Skip to content

Instantly share code, notes, and snippets.

@Martins6
Last active June 19, 2024 22:10
Show Gist options
  • Save Martins6/1d87d66a13597caa0470e373ed59ff4a to your computer and use it in GitHub Desktop.
Save Martins6/1d87d66a13597caa0470e373ed59ff4a to your computer and use it in GitHub Desktop.
Append to Day Planner daily tasks based on a CSV. This is to help the plugin of https://github.com/ivan-lednev/obsidian-day-planner.
import datetime
import os
import shutil
import pandas as pd
from loguru import logger
DATE_FORMAT = "%Y-%m-%d"
DEFAULT_FOLDER = ""
DAILY_TEMPLATE_FILE = ""
DAILY_TASKS_PATH = "daily_tasks.csv"
def create_daily_note(template_path: str = DAILY_TEMPLATE_FILE):
"""
Creates a new daily note with the current date and time.
The template file must be a simple file without any Templater plugin
references, for example.
"""
template_path = os.path.expanduser(DAILY_TEMPLATE_FILE)
date = datetime.datetime.now().strftime(DATE_FORMAT)
today_notes_path = os.path.expanduser(os.path.join(DEFAULT_FOLDER, f"{date}.md"))
if not os.path.exists(today_notes_path):
shutil.copyfile(template_path, today_notes_path)
else:
logger.info(f"Daily note already exists at {today_notes_path}")
def get_daily_tasks(daily_tasks_path: str = DAILY_TASKS_PATH) -> list:
dataframe = pd.read_csv(daily_tasks_path).sort_values("time")
# dataframe has the following columns: name,routine,time
# routine can be like "weekday" or "saturday,monday", etc.
# time is always like this format 0900-1730.
def adapt_routine(routine: str) -> str:
if routine == "weekday":
routine = ",".join(["monday", "tuesday", "wednesday", "thursday", "friday"])
return routine
today = datetime.date.today().strftime("%A").lower()
today_taks = []
for _, task in dataframe.iterrows():
routine = adapt_routine(task["routine"])
if today in routine:
task = "- [ ] " + task["time"] + " " + task["name"] + "\n"
logger.info(f"TASK: {task}")
today_taks.append(task)
return today_taks
def get_daily_file(
daily_notes_folder: str,
) -> str | None:
today_note_path = None
for file in os.listdir(daily_notes_folder):
# find the today's note in `DATE_FORMAT` format.
if datetime.datetime.now().strftime(DATE_FORMAT) == file[:-3]:
today_note_path = os.path.join(daily_notes_folder, file)
return today_note_path
def organize_obsidian_routine(daily_notes_folder: str = DEFAULT_FOLDER) -> None:
logger.info(f"Daily notes folder: {daily_notes_folder}")
# find target note
today_note_path = get_daily_file(daily_notes_folder)
logger.info(f"Today's Note path: {today_note_path}")
if not today_note_path:
logger.info(f"Creating the daily note from {DAILY_TEMPLATE_FILE}")
create_daily_note(DAILY_TEMPLATE_FILE)
today_note_path = get_daily_file(daily_notes_folder)
# read the content of the note
with open(today_note_path, "r") as file:
content = file.readlines()
# create task list
today_tasks = get_daily_tasks(DAILY_TASKS_PATH)
# TODO: grab the tasks not completed in yesterday's daily note
# and write it too.
# write new content after `keyword`
keyword = "# Day planner"
index_of_keyword = None
for index, line in enumerate(content):
if line.rstrip() == keyword:
index_of_keyword = index
break
# append in the middle of the list the tasks
new_content = (
content[: index_of_keyword + 1]
+ today_tasks
+ ["\n"]
+ content[index_of_keyword + 1 :]
)
# write the new content to the note
with open(today_note_path, "w") as file:
for line in new_content:
file.write(line)
logger.info("done!")
@logger.catch
def main():
# set up loguru
logger.add(
os.path.expanduser("~/.cronjobs/organize_obsidian_routine.log"),
level="INFO",
format="{time:YYYY-MM-DD HH:mm:ss} {level} {message}",
rotation="1 week",
)
logger.info(f"New run. Date: {datetime.date.today().strftime('%Y-%m-%d')}")
obsidian_folder = os.path.expanduser(DEFAULT_FOLDER)
organize_obsidian_routine(obsidian_folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment