From b8ca324aaf75189a12dec97f10a3edd1cf6e0aa7 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 09:58:18 +0700 Subject: [PATCH 1/2] add playlist-video and channel-video api views --- tubearchivist/api/README.md | 6 +++++ tubearchivist/api/urls.py | 18 +++++++++++--- tubearchivist/api/views.py | 49 ++++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 65cab1f..f6e10f4 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -123,12 +123,18 @@ POST /api/channel/ ## Channel Item View /api/channel/\/ +## Channel Videos View +/api/channel/\/video/ + ## Playlist List View /api/playlist/ ## Playlists Item View /api/playlist/\/ +## Playlist Videos View +/api/playlist/\/video/ + ## Download Queue List View /api/download/ diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index cb70e4c..b19f5c7 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -2,12 +2,14 @@ from api.views import ( ChannelApiListView, + ChannelApiVideoView, ChannelApiView, DownloadApiListView, DownloadApiView, LoginApiView, PingView, PlaylistApiListView, + PlaylistApiVideoView, PlaylistApiView, VideoApiListView, VideoApiView, @@ -50,15 +52,25 @@ urlpatterns = [ name="api-channel", ), path( - "playlist//", - PlaylistApiView.as_view(), - name="api-playlist", + "channel//video/", + ChannelApiVideoView.as_view(), + name="api-channel-video", ), path( "playlist/", PlaylistApiListView.as_view(), name="api-playlist-list", ), + path( + "playlist//", + PlaylistApiView.as_view(), + name="api-playlist", + ), + path( + "playlist//video/", + PlaylistApiVideoView.as_view(), + name="api-playlist-video", + ), path( "download/", DownloadApiListView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 60f2ba3..72dfeaa 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -222,6 +222,41 @@ class ChannelApiListView(ApiBaseView): return Response(data) +class ChannelApiVideoView(ApiBaseView): + """resolves to /api/channel//video + GET: returns a list of videos of channel + """ + + search_base = "ta_video/_search/" + + def get(self, request, channel_id): + # pylint: disable=unused-argument + """handle get request""" + data = { + "query": {"term": {"channel.channel_id": {"value": channel_id}}} + } + self.get_document_list(data) + self.get_paginate() + + return Response(self.response) + + +class PlaylistApiListView(ApiBaseView): + """resolves to /api/playlist/ + GET: returns list of indexed playlists + """ + + search_base = "ta_playlist/_search/" + + def get(self, request): + # pylint: disable=unused-argument + """handle get request""" + data = {"query": {"match_all": {}}} + self.get_document_list(data) + self.get_paginate() + return Response(self.response) + + class PlaylistApiView(ApiBaseView): """resolves to /api/playlist// GET: returns metadata dict of playlist @@ -236,17 +271,19 @@ class PlaylistApiView(ApiBaseView): return Response(self.response, status=self.status_code) -class PlaylistApiListView(ApiBaseView): - """resolves to /api/playlist/ - GET: returns list of indexed playlists +class PlaylistApiVideoView(ApiBaseView): + """resolves to /api/playlist//video + GET: returns list of videos in playlist """ - search_base = "ta_playlist/_search/" + search_base = "ta_video/_search/" - def get(self, request): + def get(self, request, playlist_id): # pylint: disable=unused-argument """handle get request""" - data = {"query": {"match_all": {}}} + data = { + "query": {"term": {"playlist.keyword": {"value": playlist_id}}} + } self.get_document_list(data) self.get_paginate() return Response(self.response) From eb6d6be3b96360cb554120c7c97997e02b4e33c2 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 10:08:24 +0700 Subject: [PATCH 2/2] handle 404 in channel-video and playlist-video api view --- tubearchivist/api/views.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 72dfeaa..7cd7a2a 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -52,7 +52,10 @@ class ApiBaseView(APIView): print(self.search_base) response, status_code = ElasticWrap(self.search_base).get(data=data) self.response["data"] = SearchProcess(response).process() - self.status_code = status_code + if self.response["data"]: + self.status_code = status_code + else: + self.status_code = 404 class VideoApiView(ApiBaseView): @@ -238,7 +241,7 @@ class ChannelApiVideoView(ApiBaseView): self.get_document_list(data) self.get_paginate() - return Response(self.response) + return Response(self.response, status=self.status_code) class PlaylistApiListView(ApiBaseView): @@ -286,7 +289,7 @@ class PlaylistApiVideoView(ApiBaseView): } self.get_document_list(data) self.get_paginate() - return Response(self.response) + return Response(self.response, status=self.status_code) class DownloadApiView(ApiBaseView):