[task] Try to fix scrobbling

This commit is contained in:
2024-11-01 12:35:11 -04:00
parent 86bcdef13d
commit 29b92d89b2
3 changed files with 35 additions and 26 deletions

View File

@ -346,7 +346,12 @@ def todoist_scrobble_update_task(
return scrobble
def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
def todoist_scrobble_task(
todoist_task: dict,
user_id: int,
started: bool = False,
stopped: bool = False,
) -> Scrobble:
prefix = ""
suffix = ""
@ -368,15 +373,15 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
timestamp = pendulum.parse(todoist_task.get("updated_at", timezone.now()))
in_progress_scrobble = Scrobble.objects.filter(
user_id=user_id,
in_progress=True,
log__todoist_id=todoist_task.get("todoist_id"),
task=task,
).last()
in_progress_in_todoist = "inprogress" in todoist_task["todoist_label_list"]
# We need either an in-progress scrobble OR an in-progress todoist task
if not in_progress_scrobble and not in_progress_in_todoist:
if not in_progress_scrobble and stopped:
logger.info(
"[todoist_scrobble_task] noop",
"[todoist_scrobble_task] cannot stop already stopped task",
extra={
"todoist_type": todoist_task["todoist_type"],
"todoist_event": todoist_task["todoist_event"],
@ -385,8 +390,19 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
)
return
if in_progress_scrobble and started:
logger.info(
"[todoist_scrobble_task] cannot start already started task",
extra={
"todoist_type": todoist_task["todoist_type"],
"todoist_event": todoist_task["todoist_event"],
"todoist_id": todoist_task["todoist_id"],
},
)
return in_progress_scrobble
# Finish an in-progress scrobble
if in_progress_scrobble and not in_progress_in_todoist:
if in_progress_scrobble and stopped:
logger.info(
"[todoist_scrobble_task] finishing",
extra={
@ -397,19 +413,6 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
)
return todoist_scrobble_task_finish(todoist_task, user_id)
# Ignore an already in progress scrobble
if in_progress_scrobble:
logger.info(
"[todoist_scrobble_task] continuing",
extra={
"todoist_type": todoist_task["todoist_type"],
"todoist_event": todoist_task["todoist_event"],
"todoist_id": todoist_task["todoist_id"],
"scrobble_id": in_progress_scrobble.id,
},
)
return in_progress_scrobble
# Default to create new scrobble "if not in_progress_scrobble and in_progress_in_todoist"
# TODO Should use updated_at from TOdoist, but parsing isn't working
scrobble_dict = {

View File

@ -15,6 +15,7 @@ TODOIST_TASK_URL = "https://app.todoist.com/app/task/{id}"
@dataclass
class TaskLogData(LongPlayLogData):
details: Optional[str] = None
description: Optional[str] = None
title: Optional[str] = None
project: Optional[str] = None
@ -24,6 +25,7 @@ class TaskLogData(LongPlayLogData):
serial_scrobble_id: Optional[int] = None
long_play_complete: Optional[bool] = None
timestamp_utc: Optional[datetime] = None
updated_at: Optional[datetime] = None
notes: Optional[list[dict]] = None

View File

@ -40,13 +40,14 @@ def todoist_webhook(request):
is_updated = todoist_event in ["updated"]
is_added = todoist_event in ["added"]
state_changed = False
if ("inprogress" in new_labels and "inprogress" not in old_labels) or (
task_started = (
"inprogress" in new_labels and "inprogress" not in old_labels
)
task_stopped = (
"inprogress" not in new_labels and "inprogress" in old_labels
):
state_changed = True
)
if is_item_type and is_updated and state_changed:
if is_item_type and is_updated and (task_started or task_stopped):
todoist_task = {
"todoist_id": event_data.get("id"),
"todoist_label_list": event_data.get("labels"),
@ -78,7 +79,8 @@ def todoist_webhook(request):
extra={
"todoist_type": todoist_type,
"todoist_event": todoist_event,
"state_changed": state_changed,
"task_started": task_started,
"task_stopped": task_stopped,
"new_labels": new_labels,
"old_labels": old_labels,
},
@ -92,7 +94,9 @@ def todoist_webhook(request):
)
if todoist_task:
scrobble = todoist_scrobble_task(todoist_task, user_id)
scrobble = todoist_scrobble_task(
todoist_task, user_id, stopped=task_stopped
)
if todoist_note:
scrobble = todoist_scrobble_update_task(todoist_note, user_id)