Skip to content

Instantly share code, notes, and snippets.

@nsuvorov83
Created September 26, 2022 14:38
Show Gist options
  • Select an option

  • Save nsuvorov83/6a66e4904a8b7e2bc441d4296a0ca164 to your computer and use it in GitHub Desktop.

Select an option

Save nsuvorov83/6a66e4904a8b7e2bc441d4296a0ca164 to your computer and use it in GitHub Desktop.
Creates a note in an obsidian vault with attachments.
import os
import datetime
import shutil
def writer(folder_to_save, date, time, tags = [], note_name = "", note_text = "", attachments = [], resource_folder = "_resources"):
#Forming a dictionary
props = {}
props['folder_to_save'] = folder_to_save
props['date'] = date
props['time'] = time
props['tags'] = tags
props['note_name'] = note_name
props['note_text'] = note_text
props['attachments'] = attachments
#Checking the existence of the folder
true_folder_to_save = os.path.isdir(folder_to_save)
if not true_folder_to_save: raise Exception("true_folder_to_save: false")
##Check for a file with the same name of the note
#We put underscores instead of spaces and limit the number of characters to 45
note_name_no_spaces_tr = note_name[:45].replace(' ', '_')
#Check if this is already in the folder
true_note_name_created = os.path.isfile(folder_to_save + os.path.sep + note_name_no_spaces_tr + '.md')
#If there is - add the date and time to the title of the note
if true_note_name_created: note_name_no_spaces_tr += "_" + str(datetime.datetime.now()).replace(" ", "").replace("-", ""). replace(":","")
print(folder_to_save + os.path.sep + note_name_no_spaces_tr + '.md')
attachments_saved = []
#See if there are attachments
if len(attachments) > 0:
#Checking the existence of the attachment folder
true_resource_folder = os.path.isdir(folder_to_save + os.path.sep + "_resources")
if not true_resource_folder: raise Exception("true_resource_folder: false")
#Copy attachments to the resource_folder
for attach in attachments:
attach_file_new_name = os.path.basename(attach).replace(" ", "")
shutil.copyfile(attach, folder_to_save + os.path.sep + resource_folder + os.path.sep + attach_file_new_name)
attachments_saved.append(attach_file_new_name)
#Forming the contents of the file
with open(folder_to_save + os.path.sep + note_name_no_spaces_tr + '.md', 'w', encoding="utf-8") as f:
f.write("---\n")
f.write(f'tags: ["#{date[-4:]} #из_telegram"]\n')
f.write(f'created_at: "{date} {time}"\n')
f.write("---\n")
f.write("\n\n")
f.write(f"# {note_name}\n")
f.write(note_text+"\n\n")
#Adding a list of attachments with a link to the file in the subfolder
for attach in attachments_saved:
f.write(f"![[{attach}]]" + "\n\n")
f.write(f"created_at: {date} {time}\n")
f.close()
return 0
text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
💡 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
💡 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
writer(f'/Users/user/obsidian/vault',
'23.09.2022',
'00:03',
["2022"],
"Test note blah blah blah",
text,
["/Users/user/tmp/1.jpg", "/Users/user/tmp/2.pptx"],
"_resources"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment