#!/usr/bin/env python3 """Google Tasks helper — google_api.py has no `tasks` subcommand, but the Hermes OAuth token may carry the `https://www.googleapis.com/auth/tasks` scope (check with `setup.py --check`; it lists missing scopes without printing the token). If that scope is missing, re-run setup.py with --services including tasks (or 'all') and redo the OAuth flow before using this script. Usage: python3 gtasks.py lists python3 gtasks.py list-tasks python3 gtasks.py add "" ["<notes>"] ["<due ISO date, e.g. 2026-10-30T00:00:00.000Z>"] """ import json, sys, os from pathlib import Path from google.oauth2.credentials import Credentials from google.auth.transport.requests import Request from googleapiclient.discovery import build sys.path.insert(0, str(Path(__file__).resolve().parent)) from _hermes_home import get_hermes_home TOKEN_PATH = os.environ.get("GOOGLE_TOKEN_PATH", str(get_hermes_home() / "google_token.json")) def _write_private_json(path, data): """Write JSON atomically with 0600 permissions (temp file + replace).""" tmp = str(path) + ".tmp" fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, "w", encoding="utf-8") as f: json.dump(data, f) os.chmod(tmp, 0o600) os.replace(tmp, path) def get_service(): with open(TOKEN_PATH, encoding="utf-8") as f: d = json.load(f) creds = Credentials( token=d.get("token"), refresh_token=d.get("refresh_token"), token_uri=d.get("token_uri", "https://oauth2.googleapis.com/token"), client_id=d.get("client_id"), client_secret=d.get("client_secret"), scopes=d.get("scopes") or d.get("scope"), ) if creds.expired and creds.refresh_token: creds.refresh(Request()) d["token"] = creds.token _write_private_json(TOKEN_PATH, d) return build("tasks", "v1", credentials=creds) if __name__ == "__main__": svc = get_service() cmd = sys.argv[1] if len(sys.argv) > 1 else "lists" if cmd == "lists": print(json.dumps(svc.tasklists().list().execute(), indent=2, ensure_ascii=False)) elif cmd == "list-tasks": tl = sys.argv[2] print(json.dumps(svc.tasks().list(tasklist=tl, showCompleted=False).execute(), indent=2, ensure_ascii=False)) elif cmd == "add": tl, title = sys.argv[2], sys.argv[3] notes = sys.argv[4] if len(sys.argv) > 4 else None due = sys.argv[5] if len(sys.argv) > 5 else None body = {"title": title} if notes: body["notes"] = notes if due: body["due"] = due print(json.dumps(svc.tasks().insert(tasklist=tl, body=body).execute(), indent=2, ensure_ascii=False)) else: print(__doc__)