From 241d8326f75586b8e76c31117d42635ea98e7057 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 17 Feb 2022 18:20:30 +0700 Subject: [PATCH] add video progress API endpoints --- tubearchivist/api/README.md | 26 ++++++++++++++++++++++++ tubearchivist/api/urls.py | 6 ++++++ tubearchivist/api/views.py | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 73dd4fc..bc75eda 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -23,6 +23,32 @@ response = requests.get(url, headers=headers) ## Video Item View /api/video/\/ +## Video Progress View +/api/video/\/progress + +Progress is stored for each user. + +### Get last player position of a video +GET /api/video/\/progress +```json +{ + "youtube_id": "", + "user_id": 1, + "position": 100 +} +``` + +### Post player position of video +POST /api/video/\/progress +```json +{ + "position": 100 +} +``` + +### Delete player position of video +DELETE /api/video/\/progress + ## Channel List View /api/channel/ diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index d39dc30..8ba3dcf 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -7,6 +7,7 @@ from api.views import ( DownloadApiView, PlaylistApiView, VideoApiView, + VideoProgressView, ) from django.urls import path @@ -16,6 +17,11 @@ urlpatterns = [ VideoApiView.as_view(), name="api-video", ), + path( + "video//progress/", + VideoProgressView.as_view(), + name="api-video-progress", + ), path( "channel/", ChannelApiListView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 8ddd323..79932fa 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -4,6 +4,7 @@ import requests from home.src.download.thumbnails import ThumbManager from home.src.ta.config import AppConfig from home.src.ta.helper import UrlListParser +from home.src.ta.ta_redis import RedisArchivist from home.tasks import extrac_dl, subscribe_to from rest_framework.authentication import ( SessionAuthentication, @@ -99,6 +100,45 @@ class VideoApiView(ApiBaseView): return Response(self.response, status=self.status_code) +class VideoProgressView(APIView): + """resolves to /api/video// + handle progress status for video + """ + + @staticmethod + def get(request, video_id): + """get progress for a single video""" + user_id = request.user.id + key = f"{user_id}:progress:{video_id}" + video_progress = RedisArchivist().get_message(key) + position = video_progress.get("position", 0) + + progress = { + "youtube_id": video_id, + "user_id": user_id, + "position": position, + } + return Response(progress) + + @staticmethod + def post(request, video_id): + """set progress position in redis""" + position = request.data.get("position", 0) + key = f"{request.user.id}:progress:{video_id}" + message = {"position": position} + RedisArchivist().set_message(key, message, expire=False) + + return Response(request.data) + + @staticmethod + def delete(request, video_id): + """delete progress position""" + key = f"{request.user.id}:progress:{video_id}" + RedisArchivist().del_message(key) + + return Response({"progress-reset": video_id}) + + class ChannelApiView(ApiBaseView): """resolves to /api/channel// GET: returns metadata dict of channel