add video progress API endpoints

This commit is contained in:
simon 2022-02-17 18:20:30 +07:00
parent 4d83af7c14
commit 241d8326f7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 72 additions and 0 deletions

View File

@ -23,6 +23,32 @@ response = requests.get(url, headers=headers)
## Video Item View ## Video Item View
/api/video/\<video_id>/ /api/video/\<video_id>/
## Video Progress View
/api/video/\<video_id>/progress
Progress is stored for each user.
### Get last player position of a video
GET /api/video/\<video_id>/progress
```json
{
"youtube_id": "<video_id>",
"user_id": 1,
"position": 100
}
```
### Post player position of video
POST /api/video/\<video_id>/progress
```json
{
"position": 100
}
```
### Delete player position of video
DELETE /api/video/\<video_id>/progress
## Channel List View ## Channel List View
/api/channel/ /api/channel/

View File

@ -7,6 +7,7 @@ from api.views import (
DownloadApiView, DownloadApiView,
PlaylistApiView, PlaylistApiView,
VideoApiView, VideoApiView,
VideoProgressView,
) )
from django.urls import path from django.urls import path
@ -16,6 +17,11 @@ urlpatterns = [
VideoApiView.as_view(), VideoApiView.as_view(),
name="api-video", name="api-video",
), ),
path(
"video/<slug:video_id>/progress/",
VideoProgressView.as_view(),
name="api-video-progress",
),
path( path(
"channel/", "channel/",
ChannelApiListView.as_view(), ChannelApiListView.as_view(),

View File

@ -4,6 +4,7 @@ import requests
from home.src.download.thumbnails import ThumbManager from home.src.download.thumbnails import ThumbManager
from home.src.ta.config import AppConfig from home.src.ta.config import AppConfig
from home.src.ta.helper import UrlListParser from home.src.ta.helper import UrlListParser
from home.src.ta.ta_redis import RedisArchivist
from home.tasks import extrac_dl, subscribe_to from home.tasks import extrac_dl, subscribe_to
from rest_framework.authentication import ( from rest_framework.authentication import (
SessionAuthentication, SessionAuthentication,
@ -99,6 +100,45 @@ class VideoApiView(ApiBaseView):
return Response(self.response, status=self.status_code) return Response(self.response, status=self.status_code)
class VideoProgressView(APIView):
"""resolves to /api/video/<video_id>/
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): class ChannelApiView(ApiBaseView):
"""resolves to /api/channel/<channel_id>/ """resolves to /api/channel/<channel_id>/
GET: returns metadata dict of channel GET: returns metadata dict of channel