Files
blog_creator/src/trilium/notes.py
T

55 lines
1.9 KiB
Python

from trilium_py.client import ETAPI
import os
class TrilumNotes:
def __init__(self):
self.protocol = os.environ.get('TRILIUM_PROTOCOL')
self.host = os.environ.get('TRILIUM_HOST')
self.port = os.environ.get('TRILIUM_PORT')
self.tril_pass = os.environ.get('TRILIUM_PASS')
self.token = os.environ.get('TRILIUM_TOKEN')
if not all([self.protocol, self.host, self.port, self.tril_pass]):
print("One or more required environment variables not found. Have you set a .env?")
self.server_url = f'{self.protocol}://{self.host}:{self.port}'
if not self.token:
print("Please run get_token and set your token")
else:
self.ea = ETAPI(self.server_url, self.token)
self.new_notes = None
self.note_content = None
def get_token(self):
ea = ETAPI(self.server_url)
if self.tril_pass == None:
raise ValueError("Trillium password can not be none")
token = ea.login(self.tril_pass)
print(token)
print("I would recomend you update the env file with this tootsweet!")
def get_new_notes(self):
notes = self.ea.search_note(
search="#ai_blog #blog_written=false",
ancestorNoteId="kYZXOUVabB7s",
fastSearch=False,
limit=10
)
self.new_notes = notes
return notes
def _get_content(self, note_id):
return self.ea.get_note_content(note_id)
def get_notes_content(self):
content_dict = {}
if self.new_notes is None:
raise ValueError("How did you do this? new_notes is None!")
for note in self.new_notes['results']:
content_dict[note['noteId']] = {"title" : f"{note['title']}",
"content" : f"{self._get_content(note['noteId'])}"
}
self.note_content = content_dict
return content_dict