瀏覽代碼

[scrobblers] Use udpated_at key for Todoist scrobbles

Colin Powell 6 月之前
父節點
當前提交
f2bbb7f5d0
共有 2 個文件被更改,包括 32 次插入12 次删除
  1. 27 6
      vrobbler/apps/scrobbles/scrobblers.py
  2. 5 6
      vrobbler/apps/tasks/webhooks.py

+ 27 - 6
vrobbler/apps/scrobbles/scrobblers.py

@@ -339,6 +339,7 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
 
     task = Task.find_or_create(title)
 
+    timestamp = pendulum.parse(todoist_task.get("updated_at", timezone.now()))
     in_progress_scrobble = Scrobble.objects.filter(
         in_progress=True,
         log__todoist_id=todoist_task.get("todoist_id"),
@@ -347,9 +348,10 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
     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:
         logger.info(
-            "[todoist_scrobble_task] no task in progress, and no inprogress label found",
+            "[todoist_scrobble_task] noop",
             extra={
                 "todoist_type": todoist_task["todoist_type"],
                 "todoist_event": todoist_task["todoist_event"],
@@ -358,24 +360,43 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
         )
         return 
 
+    # Finish an in-progress scrobble
     if in_progress_scrobble and not in_progress_in_todoist:
-        scrobble = todoist_scrobble_task_finish(todoist_task, user_id)
+        logger.info(
+            "[todoist_scrobble_task] finishing",
+            extra={
+                "todoist_type": todoist_task["todoist_type"],
+                "todoist_event": todoist_task["todoist_event"],
+                "todoist_id": todoist_task["todoist_id"],
+            },
+        )
+        return todoist_scrobble_task_finish(todoist_task, user_id)
 
-    # TODO this logic probably belongs in create_or_update
+    # Ignore an already in progress scrobble
     if in_progress_scrobble:
-        return 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 = {
         "user_id": user_id,
-        "timestamp": timezone.now(),
+        "timestamp": timestamp,
         "playback_position_seconds": 0,
         "source": "Todoist",
         "log": todoist_task,
     }
 
     logger.info(
-        "[todoist_scrobble_task] task scrobble request received",
+        "[todoist_scrobble_task] creating",
         extra={
             "task_id": task.id,
             "user_id": user_id,

+ 5 - 6
vrobbler/apps/tasks/webhooks.py

@@ -31,17 +31,16 @@ def todoist_webhook(request):
         "todoist_label_list": event_data.get("labels"),
         "todoist_type": todoist_type,
         "todoist_event": todoist_event,
+        "updated_at": event_data.get("updated_at"),
         "todoist_project_id": event_data.get("project_id"),
         "description": event_data.get("content"),
         "details": event_data.get("description"),
     }
 
-    if todoist_task["todoist_type"] != "item" or todoist_task[
-        "todoist_event"
-    ] not in [
-        "updated",
-        "completed",
-    ]:
+    is_not_item_type = todoist_task["todoist_type"] != "item"
+    is_not_updated = todoist_task["todoist_event"] not in ["updated"]
+
+    if is_not_item_type or is_not_updated:
         logger.info(
             "[todoist_webhook] ignoring wrong todoist type or event",
             extra={