瀏覽代碼

[scrobbles] Add action parsing to scrobble url parsing

This also elaborates on the web scrobbler endpoint, though it does not
actually work at this time. But I'm tired of carrying this around in
stashes, so we'll push it and fix it later.
Colin Powell 1 月之前
父節點
當前提交
29f5e2b940
共有 1 個文件被更改,包括 47 次插入10 次删除
  1. 47 10
      vrobbler/apps/scrobbles/views.py

+ 47 - 10
vrobbler/apps/scrobbles/views.py

@@ -98,8 +98,9 @@ class RecentScrobbleList(ListView):
         user = self.request.user
         if user.is_authenticated:
             if scrobble_url := self.request.GET.get("scrobble_url", ""):
+                action = self.request.GET.get("action", "")
                 scrobble = manual_scrobble_from_url(
-                    scrobble_url, self.request.user.id
+                    scrobble_url, self.request.user.id, action
                 )
                 return HttpResponseRedirect(scrobble.redirect_url(user.id))
         return super().get(*args, **kwargs)
@@ -314,28 +315,64 @@ def lastfm_import(request):
 
     return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
 
+
 @csrf_exempt
 @api_view(["POST"])
 def web_scrobbler_webhook(request):
-    post_data = request.data
+    """Note, this does not work as implemented. For some reason the web
+    scrobbler tool kicks back a bad response code and does not seem to POST data
+    as expected.
+
+    """
+    user_id = request.user.id
+
     logger.info(
         "[web_scrobbler_webhook] called",
         extra={
-            "post_data": post_data,
-            "user_id": request.user.id,
+            "post_data": request.POST,
+            "user_id": user_id,
         },
     )
-    # TODO Figure out why auth isn't working
-    scrobble = web_scrobbler_scrobble_media(post_data, 1)
+    youtube_id = request.POST.get("uniqueID")
+    if not youtube_id:
+        logger.warning(
+            "[web_scrobbler_webhook] failed",
+            extra={
+                "youtube_id": youtube_id,
+                "user_id": user_id,
+            },
+        )
+        return JsonResponse(
+            {"errors": ["No youtube ID provided"]}, status=status.HTTP_200_OK
+        )
 
+    playing_state = "started"
+    if request.POST.get("isPlaying") == "false":
+        playing_state = "stopped"
+    scrobble = web_scrobbler_scrobble_media(
+        youtube_id, user_id, status=playing_state
+    )
     if not scrobble:
-        return Response({}, status=status.HTTP_200_OK)
+        logger.warning(
+            "[web_scrobbler_webhook] failed",
+            extra={
+                "youtube_id": youtube_id,
+                "user_id": user_id,
+            },
+        )
+        return JsonResponse(
+            {"errors": ["No scrobble found for user or video"]},
+            status=status.HTTP_200_OK,
+        )
 
     logger.info(
-        "[jellyfin_webhook] finished",
+        "[web_scrobbler_webhook] finished",
         extra={"scrobble_id": scrobble.id},
     )
-    return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
+    return JsonResponse(
+        {"scrobble_id": scrobble.id}, status=status.HTTP_200_OK
+    )
+
 
 @csrf_exempt
 @permission_classes([IsAuthenticated])
@@ -412,7 +449,7 @@ def gps_webhook(request):
         extra={
             "post_data": data_dict,
             "user_id": 1,
-       }
+        },
     )
 
     # TODO Fix this so we have to authenticate!