[API] implement similar video endpoint

This commit is contained in:
simon 2022-11-21 15:13:15 +07:00
parent abfd109203
commit aa1d0b759e
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 40 additions and 4 deletions

View File

@ -20,6 +20,7 @@ from api.views import (
VideoApiView, VideoApiView,
VideoCommentView, VideoCommentView,
VideoProgressView, VideoProgressView,
VideoSimilarView,
VideoSponsorView, VideoSponsorView,
) )
from django.urls import path from django.urls import path
@ -47,6 +48,11 @@ urlpatterns = [
VideoCommentView.as_view(), VideoCommentView.as_view(),
name="api-video-comment", name="api-video-comment",
), ),
path(
"video/<slug:video_id>/similar/",
VideoSimilarView.as_view(),
name="api-video-similar",
),
path( path(
"video/<slug:video_id>/sponsor/", "video/<slug:video_id>/sponsor/",
VideoSponsorView.as_view(), VideoSponsorView.as_view(),

View File

@ -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""" """get a list of results"""
print(self.search_base) print(self.search_base)
self.initiate_pagination(request)
if pagination:
self.initiate_pagination(request)
es_handler = ElasticWrap(self.search_base) es_handler = ElasticWrap(self.search_base)
response, status_code = es_handler.get(data=self.data) response, status_code = es_handler.get(data=self.data)
self.response["data"] = SearchProcess(response).process() self.response["data"] = SearchProcess(response).process()
@ -74,8 +77,11 @@ class ApiBaseView(APIView):
else: else:
self.status_code = 404 self.status_code = 404
self.pagination_handler.validate(response["hits"]["total"]["value"]) if pagination:
self.response["paginate"] = self.pagination_handler.pagination self.pagination_handler.validate(
response["hits"]["total"]["value"]
)
self.response["paginate"] = self.pagination_handler.pagination
class VideoApiView(ApiBaseView): class VideoApiView(ApiBaseView):
@ -161,6 +167,30 @@ class VideoCommentView(ApiBaseView):
return Response(self.response, status=self.status_code) return Response(self.response, status=self.status_code)
class VideoSimilarView(ApiBaseView):
"""resolves to /api/video/<video-id>/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): class VideoSponsorView(ApiBaseView):
"""resolves to /api/video/<video_id>/sponsor/ """resolves to /api/video/<video_id>/sponsor/
handle sponsor block integration handle sponsor block integration