[task] Try to fix scrobbling
This commit is contained in:
@ -346,7 +346,12 @@ def todoist_scrobble_update_task(
|
|||||||
return scrobble
|
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 = ""
|
prefix = ""
|
||||||
suffix = ""
|
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()))
|
timestamp = pendulum.parse(todoist_task.get("updated_at", timezone.now()))
|
||||||
in_progress_scrobble = Scrobble.objects.filter(
|
in_progress_scrobble = Scrobble.objects.filter(
|
||||||
|
user_id=user_id,
|
||||||
in_progress=True,
|
in_progress=True,
|
||||||
log__todoist_id=todoist_task.get("todoist_id"),
|
log__todoist_id=todoist_task.get("todoist_id"),
|
||||||
task=task,
|
task=task,
|
||||||
).last()
|
).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 stopped:
|
||||||
if not in_progress_scrobble and not in_progress_in_todoist:
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"[todoist_scrobble_task] noop",
|
"[todoist_scrobble_task] cannot stop already stopped task",
|
||||||
extra={
|
extra={
|
||||||
"todoist_type": todoist_task["todoist_type"],
|
"todoist_type": todoist_task["todoist_type"],
|
||||||
"todoist_event": todoist_task["todoist_event"],
|
"todoist_event": todoist_task["todoist_event"],
|
||||||
@ -385,8 +390,19 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
|
|||||||
)
|
)
|
||||||
return
|
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
|
# Finish an in-progress scrobble
|
||||||
if in_progress_scrobble and not in_progress_in_todoist:
|
if in_progress_scrobble and stopped:
|
||||||
logger.info(
|
logger.info(
|
||||||
"[todoist_scrobble_task] finishing",
|
"[todoist_scrobble_task] finishing",
|
||||||
extra={
|
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)
|
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"
|
# 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
|
# TODO Should use updated_at from TOdoist, but parsing isn't working
|
||||||
scrobble_dict = {
|
scrobble_dict = {
|
||||||
|
|||||||
@ -15,6 +15,7 @@ TODOIST_TASK_URL = "https://app.todoist.com/app/task/{id}"
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class TaskLogData(LongPlayLogData):
|
class TaskLogData(LongPlayLogData):
|
||||||
|
details: Optional[str] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
title: Optional[str] = None
|
title: Optional[str] = None
|
||||||
project: Optional[str] = None
|
project: Optional[str] = None
|
||||||
@ -24,6 +25,7 @@ class TaskLogData(LongPlayLogData):
|
|||||||
serial_scrobble_id: Optional[int] = None
|
serial_scrobble_id: Optional[int] = None
|
||||||
long_play_complete: Optional[bool] = None
|
long_play_complete: Optional[bool] = None
|
||||||
timestamp_utc: Optional[datetime] = None
|
timestamp_utc: Optional[datetime] = None
|
||||||
|
updated_at: Optional[datetime] = None
|
||||||
notes: Optional[list[dict]] = None
|
notes: Optional[list[dict]] = None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -40,13 +40,14 @@ def todoist_webhook(request):
|
|||||||
is_updated = todoist_event in ["updated"]
|
is_updated = todoist_event in ["updated"]
|
||||||
is_added = todoist_event in ["added"]
|
is_added = todoist_event in ["added"]
|
||||||
|
|
||||||
state_changed = False
|
task_started = (
|
||||||
if ("inprogress" in new_labels and "inprogress" not in old_labels) or (
|
"inprogress" in new_labels and "inprogress" not in old_labels
|
||||||
|
)
|
||||||
|
task_stopped = (
|
||||||
"inprogress" not in new_labels and "inprogress" in old_labels
|
"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_task = {
|
||||||
"todoist_id": event_data.get("id"),
|
"todoist_id": event_data.get("id"),
|
||||||
"todoist_label_list": event_data.get("labels"),
|
"todoist_label_list": event_data.get("labels"),
|
||||||
@ -78,7 +79,8 @@ def todoist_webhook(request):
|
|||||||
extra={
|
extra={
|
||||||
"todoist_type": todoist_type,
|
"todoist_type": todoist_type,
|
||||||
"todoist_event": todoist_event,
|
"todoist_event": todoist_event,
|
||||||
"state_changed": state_changed,
|
"task_started": task_started,
|
||||||
|
"task_stopped": task_stopped,
|
||||||
"new_labels": new_labels,
|
"new_labels": new_labels,
|
||||||
"old_labels": old_labels,
|
"old_labels": old_labels,
|
||||||
},
|
},
|
||||||
@ -92,7 +94,9 @@ def todoist_webhook(request):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if todoist_task:
|
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:
|
if todoist_note:
|
||||||
scrobble = todoist_scrobble_update_task(todoist_note, user_id)
|
scrobble = todoist_scrobble_update_task(todoist_note, user_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user