-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_note.py
More file actions
executable file
·122 lines (94 loc) · 3.96 KB
/
create_note.py
File metadata and controls
executable file
·122 lines (94 loc) · 3.96 KB
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
from datetime import datetime, timedelta, timezone
from gcsa.google_calendar import GoogleCalendar
from os import path
from random import choice
from requests import get
# https://github.com/jamietr1/obsidian-automation/blob/main/obsidian-make-daily.py#L9-L19
def get_config_value(key):
try:
with open(home_root + config_file, 'r') as f:
for line in f:
if key + "=" in line:
(_, value) = line.split("=", 1)
return value.replace('"', "").strip()
except:
print("An error occurred reading the config file. Does it exist?")
quit()
return None
# https://github.com/chubin/wttr.in
def get_weather(location):
formatting = "sky: %m %c %p\ntemp: %t, feels %f\nlight: [%S,%s]\n"
params = {"format": formatting}
r = get("http://v2.wttr.in/" + location, params)
weat = r.text.strip()
print("Weather info extracted.")
return weat
# https://github.com/kuzmoyev/google-calendar-simple-api
def get_events(calendars, creds, today):
td = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
tm = td + timedelta(2) # with one-day buffer due to timezone
ev_list = []
for cal in calendars:
cal_id = calendars[cal]
gc = GoogleCalendar(credentials_path=creds, calendar=cal_id)
for event in gc.get_events(td, tm, single_events=True):
# using this: https://google-calendar-simple-api.readthedocs.io/en/latest/code/event.html
if today in str(event):
title = event.summary
start = str(event.start).replace(today, '').strip()
if start == "":
time = "00:00"
else:
time = ':'.join(start.split(':')[0:2])
ev_str = f"{time}: {title} ({cal})"
ev_list.append(ev_str)
ev_list.sort()
print(f"{len(ev_list)} calendar event(s) extracted.")
return ev_list
## https://www.marcandangel.com/2011/03/14/365-thought-provoking-questions-to-ask-yourself-this-year/
def get_question(file_365):
with open(home_root + file_365, 'r') as f:
l = f.readlines()
q = choice(l).replace('\n', '')
print("Question selected.")
return q
# Put it all together
if __name__ == "__main__":
home_root = path.dirname(path.dirname(path.abspath(__file__))) # https://stackoverflow.com/a/3430395
config_file = "/utilities/config"
notes_root = "/_notes"
daily_root = "/_1-journal/daily"
today = datetime.today().strftime("%Y-%m-%d")
daily_path = home_root + notes_root + daily_root + "/" + today + ".md"
day = "day: " + datetime.today().strftime("%A")
yesterday = (datetime.today() + timedelta(-1)).strftime("%Y-%m-%d")
tomorrow = (datetime.today() + timedelta(1)).strftime("%Y-%m-%d")
location = get_config_value("location")
weather = get_weather(location)
calendar_names = ["mk", "birthdays", "education", "mmm", "namedays",
"social", "sport", "work", "holidays", "slido",
"slido_personal"]
calendars = {}
for c in calendar_names:
calendars[c] = get_config_value(c)
creds = home_root + "/utilities/google-credentials.json"
events = get_events(calendars, creds, today)
file_365 = "/utilities/365.txt"
question = get_question(file_365)
if path.exists(daily_path):
print("File already exists, hence not overwriting.")
else:
print("Generating daily file.")
with open(daily_path, 'w') as f:
f.write("---\n")
f.write(f"{day}\n")
f.write(f"{weather}\n")
f.write("---\n")
f.write("\n## Calendar\n")
f.write(f"[[{yesterday}|yesterday <<]] [[{tomorrow}|>> tomorrow]]\n\n")
for e in events:
f.write(f"{e}\n")
f.write("\n## Journal Entry\n")
f.write("\n## Question a Day\n")
f.write(f"**{question}**\n")
print("Creating of note is done.")