"""Vikunja safe task updater — preserves existing fields when updating tasks. Usage: python vikunja_helper.py done 75 # Mark task #75 as done python vikunja_helper.py done 71 77 78 # Mark multiple tasks done python vikunja_helper.py undone 75 # Mark task #75 as not done python vikunja_helper.py comment 75 "text" # Add comment to task #75 python vikunja_helper.py desc 75 "text" # Set description (appends if exists) python vikunja_helper.py create "title" "description" # Create a new task python vikunja_helper.py create "title" "description" --done # Create and mark done python vikunja_helper.py list # List all tasks python vikunja_helper.py list todo # List TODO only python vikunja_helper.py list done # List DONE only """ import sys import json import urllib.request import urllib.error import io # Fix Windows console encoding (cp949 → utf-8) if sys.stdout.encoding != "utf-8": sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") API_BASE = "https://plan.variet.net/api/v1" TOKEN = "tk_070f8e0b715e818bb7178c3815ed5389040eddca" PROJECT_ID = 7 HEADERS = { "Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json", } def api_get(path: str): req = urllib.request.Request(f"{API_BASE}{path}", headers=HEADERS) with urllib.request.urlopen(req) as resp: return json.loads(resp.read().decode("utf-8")) def api_post(path: str, data: dict): body = json.dumps(data).encode("utf-8") req = urllib.request.Request(f"{API_BASE}{path}", data=body, headers=HEADERS, method="POST") with urllib.request.urlopen(req) as resp: return json.loads(resp.read().decode("utf-8")) def api_put(path: str, data: dict): body = json.dumps(data).encode("utf-8") req = urllib.request.Request(f"{API_BASE}{path}", data=body, headers=HEADERS, method="PUT") with urllib.request.urlopen(req) as resp: return json.loads(resp.read().decode("utf-8")) def get_task(task_id: int) -> dict: """GET full task object — preserves all fields.""" return api_get(f"/tasks/{task_id}") def safe_update_task(task_id: int, updates: dict) -> dict: """Safely update task: GET first, then POST with preserved fields.""" task = get_task(task_id) safe_body = { "title": task.get("title", ""), "description": task.get("description", ""), "priority": task.get("priority", 0), "done": task.get("done", False), } safe_body.update(updates) return api_post(f"/tasks/{task_id}", safe_body) def mark_done(task_ids: list[int]): for tid in task_ids: result = safe_update_task(tid, {"done": True}) title = result.get("title", "?") print(f" ✅ #{tid} → done=True [{title}]") def mark_undone(task_ids: list[int]): for tid in task_ids: result = safe_update_task(tid, {"done": False}) title = result.get("title", "?") print(f" ⬜ #{tid} → done=False [{title}]") def add_comment(task_id: int, comment: str): result = api_put(f"/tasks/{task_id}/comments", {"comment": comment}) print(f" 💬 #{task_id} comment added (id={result.get('id', '?')})") def set_description(task_id: int, desc: str, append: bool = True): task = get_task(task_id) existing = task.get("description", "") or "" if append and existing: new_desc = existing.rstrip() + "\n\n" + desc else: new_desc = desc result = safe_update_task(task_id, {"description": new_desc}) print(f" 📝 #{task_id} description updated [{result.get('title', '?')}]") def list_tasks(filter_: str = "all"): tasks = api_get(f"/projects/{PROJECT_ID}/tasks?per_page=100") if filter_ == "todo": tasks = [t for t in tasks if not t["done"]] elif filter_ == "done": tasks = [t for t in tasks if t["done"]] tasks.sort(key=lambda t: t["id"]) for t in tasks: status = "✅" if t["done"] else "⬜" desc = (t.get("description") or "")[:50].replace("\n", " ") labels = ", ".join(l["title"] for l in (t.get("labels") or [])) print(f" {status} #{t['id']:3d} {t['title'][:40]:<40} [{labels}] {desc}") print(f"\n Total: {len(tasks)} tasks") def create_task(title: str, description: str = "", done: bool = False): """Create a new task in the project.""" payload = {"title": title, "description": description} result = api_put(f"/projects/{PROJECT_ID}/tasks", payload) task_id = result["id"] print(f" ✨ #{task_id} created: {result.get('title', '?')}") if done: result = safe_update_task(task_id, {"done": True}) print(f" ✅ #{task_id} → done=True") return result def main(): if len(sys.argv) < 2: print(__doc__) return cmd = sys.argv[1].lower() if cmd == "done": ids = [int(x) for x in sys.argv[2:]] mark_done(ids) elif cmd == "undone": ids = [int(x) for x in sys.argv[2:]] mark_undone(ids) elif cmd == "comment": tid = int(sys.argv[2]) comment = sys.argv[3] add_comment(tid, comment) elif cmd == "desc": tid = int(sys.argv[2]) desc = sys.argv[3] set_description(tid, desc) elif cmd == "list": f = sys.argv[2] if len(sys.argv) > 2 else "all" list_tasks(f) elif cmd == "create": title = sys.argv[2] if len(sys.argv) > 2 else "" desc = sys.argv[3] if len(sys.argv) > 3 else "" is_done = "--done" in sys.argv if not title: print("Error: title is required") print(' Usage: vikunja_helper.py create "title" "description" [--done]') return create_task(title, desc, done=is_done) else: print(f"Unknown command: {cmd}") print(__doc__) if __name__ == "__main__": main()