From 72a84282715c3dee78f65d1557003b712e3320e1 Mon Sep 17 00:00:00 2001
From: Andrew Ridgway <ar17787@gmail.com>
Date: Tue, 12 Nov 2024 15:59:59 +1000
Subject: [PATCH] playing with tril api

---
 Dockerfile               | 15 ++++++++++-----
 README.md                |  9 +++++++++
 docker-compose.yml       |  9 +++++++++
 requirements.txt         |  1 +
 src/main.py              |  8 ++++++++
 src/trilium/get_token.py |  8 --------
 src/trilium/notes.py     | 36 ++++++++++++++++++++++++++++++++++++
 7 files changed, 73 insertions(+), 13 deletions(-)
 create mode 100644 docker-compose.yml
 delete mode 100644 src/trilium/get_token.py
 create mode 100644 src/trilium/notes.py

diff --git a/Dockerfile b/Dockerfile
index cd9bfda..a220107 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,15 +1,20 @@
-FROM python:slim
+FROM debian:trixie-slim
 
 WORKDIR /blog_creator
 
-COPY requirements.txt requirements.txt
+ENV PYTHONDONTWRITEBYTECODE 1
+ENV PYTHONUNBUFFERED 1
 
 ADD src/ /blog_creator
 
+RUN apt-get update && apt-get install -y rustc cargo python-is-python3 pip python3.12-venv libmagic-dev 
+
+RUN python -m venv /opt/venv
+ENV PATH="/opt/venv/bin:$PATH"
+
+COPY requirements.txt requirements.txt
+
 RUN pip install -r requirements.txt
 
-#RUN apt-get update && apt-get install -y  make
-
-#RUN make html
 
 ENTRYPOINT ["python", "main.py"]
diff --git a/README.md b/README.md
index 63281af..833f393 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,14 @@
 ## BLOG CREATOR
 
+This creator requires you to use a working Trilium Instance and create a .env file with the following
+
+```
+TRILIUM_HOST
+TRILIUM_PORT
+TRILIUM_PROTOCOL
+TRILIUM_PASS
+```
+
 This container is going to be what I use to trigger a blog creation event
 
 To do this we will
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..fb9f900
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,9 @@
+services:
+  blog_creator:
+    build:
+      context: .
+      dockerfile: Dockerfile
+    container_name: blog_creator
+    env_file:
+      - .env
+
diff --git a/requirements.txt b/requirements.txt
index 403abba..626ee13 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1 +1,2 @@
 ollama
+trilium-py
diff --git a/src/main.py b/src/main.py
index d7f7ca9..c33162a 100644
--- a/src/main.py
+++ b/src/main.py
@@ -1,5 +1,13 @@
 from ollama import Client
 
+import trilium.notes as tn
+
+tril = tn.TrilumNotes()
+
+tril_notes = tril.get_new_notes()
+
+for note in tril_notes:
+    print(note)
 client = Client(host='http://192.168.178.45:11434')
 
 user_prompt = input("Ask mistral-nemo a question: ")
diff --git a/src/trilium/get_token.py b/src/trilium/get_token.py
deleted file mode 100644
index 707136f..0000000
--- a/src/trilium/get_token.py
+++ /dev/null
@@ -1,8 +0,0 @@
-from trilium_py.client import ETAPI
-import os
-
-server_url = f'{os.environ['TRILIUM_PROTOCOL']}://{os.environ['TRILIUM_HOST']}:{os.environ['TRILIUM_PORT']}'
-password = os.environ['TRILIUM_PASS']
-ea = ETAPI(server_url)
-token = ea.login(password)
-print(token)
diff --git a/src/trilium/notes.py b/src/trilium/notes.py
new file mode 100644
index 0000000..8351617
--- /dev/null
+++ b/src/trilium/notes.py
@@ -0,0 +1,36 @@
+from trilium_py.client import ETAPI
+import os
+
+
+class TrilumNotes:
+    def __init__(self):
+        self.protocol = os.environ.get('TRILUM_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)
+    
+    def get_token(self):
+        ea = ETAPI(self.server_url)
+        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