Initial commit from agent
This commit is contained in:
68
capabilities/notes_manager.py
Normal file
68
capabilities/notes_manager.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
Google Keep notes via gkeepapi (unofficial).
|
||||
Falls back to local Markdown files if Keep is unavailable.
|
||||
|
||||
NOTE: The official Google Keep API requires Google Workspace Enterprise.
|
||||
For personal/standard accounts use gkeepapi or the Markdown fallback below.
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# ─── gkeepapi backend ──────────────────────────
|
||||
try:
|
||||
import gkeepapi
|
||||
_keep = None
|
||||
|
||||
def _init():
|
||||
global _keep
|
||||
if _keep is None:
|
||||
email = os.getenv("GOOGLE_KEEP_EMAIL")
|
||||
token = os.getenv("GOOGLE_KEEP_MASTER_TOKEN")
|
||||
if not email or not token:
|
||||
raise ValueError("Set GOOGLE_KEEP_EMAIL and GOOGLE_KEEP_MASTER_TOKEN in .env")
|
||||
_keep = gkeepapi.Keep()
|
||||
_keep.authenticate(email, token)
|
||||
return _keep
|
||||
|
||||
def create_note(title: str, content: str) -> str:
|
||||
keep = _init()
|
||||
note = keep.createNote(title, content)
|
||||
note.pinned = True
|
||||
keep.sync()
|
||||
return note.id
|
||||
|
||||
def get_all_notes() -> list[dict]:
|
||||
keep = _init()
|
||||
return [{'id': n.id, 'title': n.title, 'text': n.text} for n in keep.all()]
|
||||
|
||||
def search_notes(query: str) -> list[dict]:
|
||||
keep = _init()
|
||||
return [{'id': n.id, 'title': n.title, 'text': n.text} for n in keep.find(query=query)]
|
||||
|
||||
NOTES_BACKEND = "gkeepapi"
|
||||
|
||||
except ImportError:
|
||||
# ─── Markdown fallback ──────────────────────
|
||||
NOTES_DIR = Path.home() / "JARVIS_Notes"
|
||||
NOTES_DIR.mkdir(exist_ok=True)
|
||||
NOTES_BACKEND = "markdown"
|
||||
|
||||
def create_note(title: str, content: str) -> str:
|
||||
safe_title = "".join(c for c in title if c.isalnum() or c in " _-").strip()
|
||||
path = NOTES_DIR / f"{safe_title}.md"
|
||||
path.write_text(f"# {title}\n\n{content}\n")
|
||||
return str(path)
|
||||
|
||||
def get_all_notes() -> list[dict]:
|
||||
return [
|
||||
{'title': p.stem, 'text': p.read_text()}
|
||||
for p in NOTES_DIR.glob("*.md")
|
||||
]
|
||||
|
||||
def search_notes(query: str) -> list[dict]:
|
||||
results = []
|
||||
for p in NOTES_DIR.glob("*.md"):
|
||||
text = p.read_text()
|
||||
if query.lower() in text.lower():
|
||||
results.append({'title': p.stem, 'text': text})
|
||||
return results
|
||||
Reference in New Issue
Block a user