Skip to content

Instantly share code, notes, and snippets.

@ucyo
Created January 28, 2024 09:39
Show Gist options
  • Select an option

  • Save ucyo/a3b89a56faac385a8c4d70ef196846ac to your computer and use it in GitHub Desktop.

Select an option

Save ucyo/a3b89a56faac385a8c4d70ef196846ac to your computer and use it in GitHub Desktop.
Journal
"""Journaling application for daily notes, tasks and information blocks from meetings, calls and self-study.
There are 5 categories: Context, Information, Decision, Todo/Action for others, Todo/Action for me.
The categories are represented by the first letter of the category name with A for Actions for others and T for my Todos.
All notes are stored in a single file with the name Journal-<YEAR>.md. The file is created if it does not exist.
The file is stored in the same directory as the script.
The file contains a YAML metadata block with the title, description and usage information.
"""
import os
import datetime
METADATA = """---
Title: Journal
Description: One file for all notes, tasks and information blocks from meetings
Usage: Context [C], Information [I], Decision [D], Action/Todo for others [A], Todo/Action for me [ ]
---"""
create_filepath = lambda x: os.path.join(os.path.dirname(os.path.abspath(__file__)), x)
create_screenshotpath = lambda x: os.path.join(os.path.dirname(os.path.abspath(__file__)), "Screenshots", x)
YEAR = 2024
FILENAME = create_filepath(f"Journal-{YEAR}.md")
CATEGORIES = {
"C": "Context",
"I": "Information",
"D": "Decision",
"T": "Todo",
"A": "Action",
"S": "Screenshot",
}
def print_colors(msg, type="INFO"):
"""Prints the message in the specified color."""
if type == "INFO":
color = bcolors.OKCYAN
elif type == "WARNING":
color = bcolors.WARNING
elif type == "ERROR":
color = bcolors.FAIL
print(f"{color}{msg}{bcolors.ENDC}")
class bcolors:
"""Colors for colored output on terminal."""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def create():
"""Creates the journal file if it does not exist."""
if os.path.exists(FILENAME):
print_colors(f"File {FILENAME} already exists!")
else:
with open(FILENAME, "w") as f:
f.write(METADATA)
f.write("\n")
f.write(f"# Journal {YEAR}\n")
print_colors(f"Created file {FILENAME}", "WARNING")
def add_date():
"""Adds the current date to the journal file if it does not exist."""
today = datetime.date.today()
today_str = today.strftime("- **%a %b %d**")
if check_date(today_str):
with open(FILENAME, "a") as f:
f.write("\n")
f.write(today_str)
f.write("\n")
print_colors(f"Added date {today_str}", "WARNING")
def check_date(date_str):
"""Checks if the date is already in the journal file."""
with open(FILENAME, "r") as f:
lines = f.readlines()
for line in lines:
if line.startswith(date_str):
print_colors(f"Date {date_str} already exists!")
return False
print_colors(f"Date {date_str} does not exist!", "WARNING")
return True
def take_screenshot(format: str = "png"):
"""Takes a screenshot and adds it to the journal file."""
from PIL import ImageGrab
screenshot = ImageGrab.grab()
today = datetime.datetime.now()
screenshot_basename = today.strftime(f"%Y%m%d-%H%M%S.{format}")
filepath = create_screenshotpath(screenshot_basename)
screenshot.save(filepath)
return filepath
def add_screenshot_in_journal(fileobj, screenshot_filepath: str, info: str = "Took a screenshot!"):
"""Adds the screenshot to the journal file."""
fileobj.write(f" - ![Screenshot]({screenshot_filepath})\n")
def main():
""""Main routine of the journal application."""
os.system("clear")
create()
add_date()
try:
while True:
input_str = input(f"Enter category {[f'[{x[0]}]{x[1:]}' for x in CATEGORIES.values()]}: ")
if input_str == "exit":
break
elif input_str and len(input_str) == 1 and input_str[0].upper() in CATEGORIES.keys():
if input_str.upper() == "S":
print_colors("Taking a screenshot ...", "INFO")
screenshot_filepath = take_screenshot()
input_note = input("Enter note: ")
with open(FILENAME, "a") as f:
if input_str.upper() == "T":
input_str = " "
f.write(f"- [{input_str.upper()}] {input_note}\n")
if input_str.upper() == "S":
add_screenshot_in_journal(f, screenshot_filepath)
os.system("clear")
if input_str.upper() == " ":
input_str = "t"
print_colors(f"Added note: [{CATEGORIES[input_str.upper()]}] {input_note}")
else:
print_colors("Invalid input!", "ERROR")
continue
except KeyboardInterrupt:
print("\n")
print_colors("Exiting...", "WARNING")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment