3.1 KiB
3.1 KiB
Certainly! Below is the markdown representation of the Python script outline provided:
# Ollama Blog Post Generation Script
This script automates the process of generating a new blog post by integrating Trilium notes, using Ollama for content generation, and pushing the result to a Git repository. It also sends a notification via Matrix.
## Steps Involved:
1. **Retrieve Notes from Trilium**
2. **Generate Content with Ollama**
3. **Automate Git Operations**
4. **Notify Matrix**
## Python Script Outline
```python
# Import necessary libraries
import os
from trilium_api import Client
import requests
from datetime import datetime
from git import Repo, Remote, GitCommandError, InvalidGitRepositoryError
from contextlib import contextmanager
@contextmanager
def cd(new_directory):
"""Context manager for changing the current working directory"""
previous_dir = os.getcwd()
os.chdir(new_directory)
try:
yield
finally:
os.chdir(previous_dir)
# Environment variables
TRILUM_API_URL = "http://trilum-host:8080"
OLLAMA_API_URL = "http://ollama-host:3000"
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
MATRIX_NOTIFY_URL = "http://matrix-bot/notify"
# Step 1: Retrieve notes from Trilium
client = Client()
notes = client.search("title:blog AND date_modified:>2023-10-01")
selected_notes = [note for note in notes if len(note.content) > 100]
# Step 2: Generate content with Ollama
prompt = " ".join([n.content[:50] for n in selected_notes])
ollama_content = requests.get(f"{OLLAMA_API_URL}/generate?prompt={prompt}").json()['content']
# Step 3: Git operations
repo_dir = "/path/to/blog-repo"
if not os.path.exists(repo_dir):
Repo.clone_from("ssh://user@host/repo.git", repo_dir)
with cd(repo_dir):
try:
origin = Repo().remote(name='origin')
origin.pull()
branch_name = f"new_post_{datetime.now().strftime('%Y%m%d%H%M%S')}"
Repo().create_head(branch_name, origin.refs/main)
Repo().heads[branch_name].checkout()
with open("post.md", "w") as f:
f.write(ollama_content)
origin.push(branch_name)
except GitCommandError as e:
print(f"Git error: {e}")
except InvalidGitRepositoryError:
print("The specified directory is not a git repository.")
# Step 4: Notify Matrix
requests.post(MATRIX_NOTIFY_URL, data={"text": "New blog post generated!"})
Notes and Considerations:
- Ensure you have the
trilium_api
library installed. You can install it using pip if necessary. - Set up environment variables for sensitive information like API tokens.
- Handle potential errors during Git operations and ensure proper directory setup.
- This script assumes a basic understanding of Trilium, Ollama, and Git workflows.
Dependencies:
trilium_api
: For interacting with the Trilium notes application.requests
: For making HTTP requests to APIs.gitpython
: For interacting with Git repositories.- Additional libraries for handling context managers (e.g.,
contextlib
).
This script provides a starting point and can be further refined based on specific requirements and edge cases.