57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""
|
|
Google Tasks API integration.
|
|
List, create, and complete tasks across all task lists.
|
|
"""
|
|
from googleapiclient.discovery import build
|
|
from core.google_auth import get_credentials
|
|
|
|
def _service():
|
|
return build('tasks', 'v1', credentials=get_credentials())
|
|
|
|
def _get_default_list_id() -> str:
|
|
svc = _service()
|
|
lists = svc.tasklists().list(maxResults=1).execute()
|
|
return lists['items'][0]['id']
|
|
|
|
def get_pending_tasks() -> list[dict]:
|
|
"""Return all incomplete tasks across all task lists."""
|
|
svc = _service()
|
|
tasklists = svc.tasklists().list(maxResults=10).execute()
|
|
all_tasks = []
|
|
for tl in tasklists.get('items', []):
|
|
tasks = svc.tasks().list(
|
|
tasklist=tl['id'], showCompleted=False, maxResults=50
|
|
).execute()
|
|
for t in tasks.get('items', []):
|
|
all_tasks.append({
|
|
'id': t['id'],
|
|
'tasklist_id': tl['id'],
|
|
'title': t.get('title', ''),
|
|
'due': t.get('due', ''),
|
|
'notes': t.get('notes', ''),
|
|
'list': tl['title'],
|
|
})
|
|
return all_tasks
|
|
|
|
def create_task(title: str, notes: str = None, due: str = None) -> dict:
|
|
"""Create a new task in the default task list."""
|
|
svc = _service()
|
|
list_id = _get_default_list_id()
|
|
body = {'title': title}
|
|
if notes:
|
|
body['notes'] = notes
|
|
if due:
|
|
body['due'] = due
|
|
return svc.tasks().insert(tasklist=list_id, body=body).execute()
|
|
|
|
def complete_task(tasklist_id: str, task_id: str) -> dict:
|
|
"""Mark a task as completed."""
|
|
svc = _service()
|
|
task = svc.tasks().get(tasklist=tasklist_id, task=task_id).execute()
|
|
task['status'] = 'completed'
|
|
return svc.tasks().update(tasklist=tasklist_id, task=task_id, body=task).execute()
|
|
|
|
def delete_task(tasklist_id: str, task_id: str):
|
|
"""Delete a task permanently."""
|
|
_service().tasks().delete(tasklist=tasklist_id, task=task_id).execute()
|