From 03dd25cff4a589a2d76ebd912a7815b09d001af8 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 5 Apr 2022 22:25:40 +0700 Subject: [PATCH] implement vote on sponsorblock segments api --- tubearchivist/api/views.py | 32 ++++++++++++++++++++++----- tubearchivist/home/src/index/video.py | 14 ++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index f590d43..d938b2f 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -146,7 +146,7 @@ class VideoProgressView(ApiBaseView): class VideoSponsorView(ApiBaseView): - """resolves to /api/video// + """resolves to /api/video//sponsor/ handle sponsor block integration """ @@ -161,17 +161,37 @@ class VideoSponsorView(ApiBaseView): return Response(sponsorblock) - @staticmethod - def post(request, video_id): + def post(self, request, video_id): """post verification and timestamps""" - start_time = request.data.get("startTime") - end_time = request.data.get("endTime") + if "segment" in request.data: + response, status_code = self._create_segment(request, video_id) + elif "vote" in request.data: + response, status_code = self._vote_on_segment(request) + return Response(response, status=status_code) + + @staticmethod + def _create_segment(request, video_id): + """create segment in API""" + start_time = request.data["segment"]["startTime"] + end_time = request.data["segment"]["endTime"] response, status_code = SponsorBlock(request.user.id).post_timestamps( video_id, start_time, end_time ) - return Response(response, status=status_code) + return response, status_code + + @staticmethod + def _vote_on_segment(request): + """validate on existing segment""" + user_id = request.user.id + uuid = request.data["vote"]["uuid"] + vote = request.data["vote"]["yourVote"] + response, status_code = SponsorBlock(user_id).vote_on_segment( + uuid, vote + ) + + return response, status_code class ChannelApiView(ApiBaseView): diff --git a/tubearchivist/home/src/index/video.py b/tubearchivist/home/src/index/video.py index 108a3b7..411b4af 100644 --- a/tubearchivist/home/src/index/video.py +++ b/tubearchivist/home/src/index/video.py @@ -335,6 +335,20 @@ class SponsorBlock: return {"success": True}, 200 + def vote_on_segment(self, uuid, vote): + """send vote on existing segment""" + user_id = self.get_sb_id().get("status") + data = { + "UUID": uuid, + "userID": user_id, + "type": vote, + } + url = f"{self.API}/api/voteOnSponsorTime" + print(f"post: {data}") + print(f"to: {url}") + + return {"success": True}, 200 + class YoutubeVideo(YouTubeItem, YoutubeSubtitle): """represents a single youtube video"""