diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index 7fb48a09..8662ad0a 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -20,6 +20,7 @@ from api.views import ( VideoApiView, VideoCommentView, VideoProgressView, + VideoSimilarView, VideoSponsorView, ) from django.urls import path @@ -47,6 +48,11 @@ urlpatterns = [ VideoCommentView.as_view(), name="api-video-comment", ), + path( + "video//similar/", + VideoSimilarView.as_view(), + name="api-video-similar", + ), path( "video//sponsor/", VideoSponsorView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index dab62e37..ea771f09 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -62,10 +62,13 @@ class ApiBaseView(APIView): } ) - def get_document_list(self, request): + def get_document_list(self, request, pagination=True): """get a list of results""" print(self.search_base) - self.initiate_pagination(request) + + if pagination: + self.initiate_pagination(request) + es_handler = ElasticWrap(self.search_base) response, status_code = es_handler.get(data=self.data) self.response["data"] = SearchProcess(response).process() @@ -74,8 +77,11 @@ class ApiBaseView(APIView): else: self.status_code = 404 - self.pagination_handler.validate(response["hits"]["total"]["value"]) - self.response["paginate"] = self.pagination_handler.pagination + if pagination: + self.pagination_handler.validate( + response["hits"]["total"]["value"] + ) + self.response["paginate"] = self.pagination_handler.pagination class VideoApiView(ApiBaseView): @@ -161,6 +167,30 @@ class VideoCommentView(ApiBaseView): return Response(self.response, status=self.status_code) +class VideoSimilarView(ApiBaseView): + """resolves to /api/video//similar/ + GET: return max 3 videos similar to this + """ + + search_base = "ta_video/_search/" + + def get(self, request, video_id): + """get similar videos""" + self.data = { + "size": 6, + "query": { + "more_like_this": { + "fields": ["tags", "title"], + "like": {"_id": video_id}, + "min_term_freq": 1, + "max_query_terms": 25, + } + }, + } + self.get_document_list(request, pagination=False) + return Response(self.response, status=self.status_code) + + class VideoSponsorView(ApiBaseView): """resolves to /api/video//sponsor/ handle sponsor block integration