forked from jamietr1/obsidian-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsidian-make-daily.py
executable file
·211 lines (171 loc) · 6.76 KB
/
obsidian-make-daily.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
import os
import arrow
import re
import subprocess
import requests
def get_config_value(key):
try:
print(os.path.join(home_root, config_file))
with open(os.path.join(home_root, config_file), 'r') as fh_:
for line in fh_:
if key + ":" in line:
(_, value) = line.strip().split(":", 1)
return value.strip()
except:
print('An error occurred reading your .notes file. Does it exist?')
quit()
return ""
def get_link_for_file(file, link_text=""):
if link_text != "":
return "[[" + file.replace(".md", "") + "|" + link_text + "]]"
else:
return "[[" + file.replace(".md", "") + "]]"
def get_weather(location):
payload = {
'format': '1',
}
r = requests.get("https://wttr.in/" + location, params=payload)
return r.text.strip()
def read_file(file_name):
file_content = ""
with open(file_name, 'r') as file_obj:
for line in file_obj:
file_content += line
return file_content
def get_humanize_date_from_daily_note(file_name):
daily_note = re.search("\d{4}\.\d{2}\.\d{2}\.[A-Za-z]{3}", os.path.basename(file_name).replace(".md", ""))
if daily_note:
(year, mon, day, _) = os.path.basename(file_name).replace(".md", "").split(".")
todo_date = arrow.get(year + '-' + mon + '-' + day)
return " (from " + todo_date.humanize() + ")"
else:
return ""
def get_daily_notes_filename(offset=0):
file_date = arrow.now()
if offset != 0:
file_date = file_date.shift(days=offset)
return file_date.format('YYYY-MM-DD') + ".md"
def find_todos_in_file(file_name, pattern):
# Search a file for incomplete todos: [ ]
# Strip the item down to its bare essentials
# Hash it to eliminate duplicates
matches = {}
with open(file_name, 'r') as file_obj:
for line in file_obj:
result = re.search(pattern, line.strip())
if result:
if result.group(1):
todo = result.group(1).strip()
if " (from " in todo:
pos = todo.find("(from ")
todo = todo[:pos].strip()
else:
todo = todo.strip()
matches[todo] = file_name
return matches
def search_in_file(file_name, search_for):
# Searches file_name for search_for and returns boolean result
with open(file_name, 'r') as file_obj:
for line in file_obj:
if search_for in line:
return True
return False
def get_done_todos():
done_task_pattern = "\[x\](.*)"
done = {}
for root, dirs, files in os.walk(daily_notes):
for fi in files:
if fi.endswith(".md"):
fi_done = find_todos_in_file(os.path.join(root, fi), done_task_pattern)
for m in fi_done:
if m in done.keys():
continue
else:
done[m] = fi_done[m]
return done
def get_open_todos():
open_task_pattern = "\[\s\](.*)"
open = {}
for root, dirs, files in os.walk(daily_notes):
for fi in files:
if fi.endswith(".md"):
fi_open = find_todos_in_file(os.path.join(root, fi), open_task_pattern)
for m in fi_open:
if m in open.keys():
continue
else:
open[m] = fi_open[m]
return open
def get_agenda():
# for this to work with icalBuddy (as of 1/31/21) a special version of the
# binary is needed (see this thread
# https://forum.keyboardmaestro.com/t/icalbuddy-doesnt-work-within-keyboard-maestro-mojave-calendar-permissions/15446/5)
# I replaced the existing version in /opt/homebrew/cellar/bin moving the old
# version to icalBuddy.old. The fixed version is in my Downloads folder
# Also, you need to run it from Finder the first time using right-click Open so
# that you can okay the binary to run on the local machine.
result = subprocess.getoutput(ical_buddy)
return result
def get_agenda_3():
result = subprocess.getoutput(ical_buddy_3)
return result
# Put it all together
home_root = os.path.expanduser('~')
config_file = "Codes/PyCharmProjects/Utilities/obsidian-automation/.notes"
daily_notes = get_config_value("daily_notes_root")
ical_buddy = get_config_value("ical_buddy_cmd")
ical_buddy_3 = get_config_value("ical_buddy_3_cmd")
weather_location = get_config_value("weather_zip")
daily_notes_file = os.path.join(daily_notes, get_daily_notes_filename())
# if os.path.exists(daily_notes_file):
# print("File already exists. Not overwriting...")
# else:
print("Generating daily notes file " + os.path.basename(daily_notes_file) + "...")
with open(daily_notes_file, 'a') as fh:
# Make note navi-gation
nav_bar = get_link_for_file(get_daily_notes_filename(offset=-1))
nav_bar += " | " + get_link_for_file(get_daily_notes_filename(offset=1))
nav_bar += " | " + get_weather(weather_location)
fh.write(nav_bar + "\n")
fh.write("\n## Mood\n")
tasks = {}
unfinished= []
agenda = get_agenda().split("\n")
with open(daily_notes + '/' + get_daily_notes_filename(offset=-1), 'r') as yesterday:
for line in yesterday:
if line.startswith("- [ ]"):
unfinished.append(line.strip('\n'))
if len(unfinished) != 0:
fh.write("\n## Unfinished\n")
for item in unfinished:
fh.write(item + "\n")
fh.write("\n## Agenda\n")
if len(agenda) == 1:
fh.write("Nothing in today's calendar\n")
else:
for item in agenda:
fh.write("- [ ] " + item.strip('- ').strip('• ') + "\n")
done_todos = get_done_todos()
open_todos = get_open_todos()
for task in open_todos.keys():
if task in done_todos.keys():
continue
else:
tasks[task] = open_todos[task]
# fh.write("\n## To-Do\n")
# if len(tasks) == 0:
# fh.write("No open to-do items\n")
# else:
# for item in tasks:
# fh.write("- [ ] " + item + get_humanize_date_from_daily_note(tasks[item]) + "\n")
agenda_3 = get_agenda_3().split('\n')
fh.write("\n## Agenda in the coming 3 days\n")
if len(agenda_3) == 1:
fh.write("Nothing in the coming 3 day's calendar\n")
else:
for item in agenda_3:
item = item.strip('- ').strip('• ')
if not item.startswith("today"):
fh.write("- [ ] " + item + "\n")
fh.write("\n## Reading\n")