From fb046bed5bd1f897eddde5e4a4db2a5b2c59d6bd Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 12 Nov 2022 12:42:08 +0700 Subject: [PATCH] [API] implement get comments API view --- tubearchivist/api/README.md | 4 ++++ tubearchivist/api/src/search_processor.py | 16 ++++++++++++++++ tubearchivist/api/urls.py | 6 ++++++ tubearchivist/api/views.py | 16 ++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 6e5efb4b..593089d1 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -12,6 +12,7 @@ Note: **Video** - [Video List](#video-list-view) - [Video Single](#video-item-view) +- [Video Comments](#video-comment-view) - [Video Single Progress](#video-progress-view) - [Video Single Sponsorblock](#sponsor-block-view) WIP @@ -78,6 +79,9 @@ Pass page number as a query parameter: `page=2`. Defaults to *0*, `page=1` is re ## Video Item View /api/video/\/ +## Video Comment View +/api/video/\/comment/ + ## Video Progress View /api/video/\/progress diff --git a/tubearchivist/api/src/search_processor.py b/tubearchivist/api/src/search_processor.py index 7a41163c..7b594f6a 100644 --- a/tubearchivist/api/src/search_processor.py +++ b/tubearchivist/api/src/search_processor.py @@ -48,6 +48,8 @@ class SearchProcess: processed = self._process_playlist(result["_source"]) if index == "ta_download": processed = self._process_download(result["_source"]) + if index == "ta_comment": + processed = self._process_comment(result["_source"]) return processed @@ -123,3 +125,17 @@ class SearchProcess: } ) return dict(sorted(download_dict.items())) + + def _process_comment(self, comment_dict): + """run on all comments, create reply thread""" + all_comments = comment_dict["comment_comments"] + processed_comments = [] + + for comment in all_comments: + if comment["comment_parent"] == "root": + comment.update({"comment_replies": []}) + processed_comments.append(comment) + else: + processed_comments[-1]["comment_replies"].append(comment) + + return processed_comments diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index e84ec052..7fb48a09 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -18,6 +18,7 @@ from api.views import ( TaskApiView, VideoApiListView, VideoApiView, + VideoCommentView, VideoProgressView, VideoSponsorView, ) @@ -41,6 +42,11 @@ urlpatterns = [ VideoProgressView.as_view(), name="api-video-progress", ), + path( + "video//comment/", + VideoCommentView.as_view(), + name="api-video-comment", + ), path( "video//sponsor/", VideoSponsorView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 2574db65..dab62e37 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -145,6 +145,22 @@ class VideoProgressView(ApiBaseView): return Response(self.response) +class VideoCommentView(ApiBaseView): + """resolves to /api/video//comment/ + handle video comments + GET: return all comments from video with reply threads + """ + + search_base = "ta_comment/_doc/" + + def get(self, request, video_id): + """get video comments""" + # pylint: disable=unused-argument + self.get_document(video_id) + + return Response(self.response, status=self.status_code) + + class VideoSponsorView(ApiBaseView): """resolves to /api/video//sponsor/ handle sponsor block integration