Todo bundle
2023-01-24
I've been using the Day planner plugin on obsidian for a while now and notised the piling problem that occurs with its auto-generated files:
So i created a python script to bundle them together.
Here is the prompt i came up with for chatGPT:
I want to create a python script that let the user to type "todo bundle" which takes in two arguments:
- a [begin date]
- a [end date]
Note: the dates will be in the format of a 8 digit number. For example, if it's 2023, january 24th, the number will be 20230124
and then performs the following actions:
- search through a [predefined path] where all the files end in 8 digit numbers, and put the file that has all the number with in the range of [begin date] and [end date] in a list.
- sort the list from small to big based on the ending number in file name
- read through these files line by line in [predefined path] in the sorted order.
- if the line starts with x number of space plus "- [ ", than put it in temp.md along with the file name. Note temp.md's content will be formated like this:
[filename 1]
[a qualified line from filename 1] [another qualified line from filename 1]
[filename 2]
[a qualified line from filename 2] [another qualified line from filename 2] 5. Finally, temp.md wil be saved as "Day Planner-[begin date]->[end date]" in [predefined path]
and here is the actual code:
import os
import sys
def todo_bundle(begin_date, end_date):
# predefined path
path = "[predefined path]"
# search through files and put in a list
file_list = [f for f in os.listdir(path) if f.endswith('.md') and (("Day Planner-"+begin_date <= f <= "Day Planner-"+end_date) or (f.endswith(end_date+".md")))]
# sort list by file name
file_list.sort()
# read through files and write to temp.md
temp_md = ""
for file_name in file_list:
with open(os.path.join(path, file_name)) as f:
in_file_name = file_name.rsplit(".",1)[0]
temp_md += "# " + in_file_name + "\n"
for line in f:
print(line)
if line.lstrip().startswith("- ["):
temp_md += line
temp_md += "\n"
# save temp.md as "Day Planner-[begin date]->[end date]"
with open(os.path.join(path, "Day Planner-{}-{}.md".format(begin_date, end_date)), "w") as f:
f.write(temp_md)
if __name__ == "__main__":
begin_date = sys.argv[1]
end_date = sys.argv[2]
todo_bundle(begin_date, end_date)