From 2818bd5d5224e0bdd1358de63c415966a299cc8b Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 18 Nov 2022 09:44:20 +0700 Subject: [PATCH] add comments to video template if available --- tubearchivist/home/src/es/index_mapping.json | 3 +++ tubearchivist/home/src/index/comments.py | 15 ++++++++++++--- tubearchivist/home/templates/home/video.html | 11 +++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tubearchivist/home/src/es/index_mapping.json b/tubearchivist/home/src/es/index_mapping.json index 4a325eae..79f1d81a 100644 --- a/tubearchivist/home/src/es/index_mapping.json +++ b/tubearchivist/home/src/es/index_mapping.json @@ -193,6 +193,9 @@ } } }, + "comment_count": { + "type": "long" + }, "stats" : { "properties" : { "average_rating" : { diff --git a/tubearchivist/home/src/index/comments.py b/tubearchivist/home/src/index/comments.py index fd5fb9c6..b7694cf6 100644 --- a/tubearchivist/home/src/index/comments.py +++ b/tubearchivist/home/src/index/comments.py @@ -20,18 +20,19 @@ class Comments: self.es_path = f"ta_comment/_doc/{youtube_id}" self.json_data = False self.config = config + self.comments_format = False def build_json(self): """build json document for es""" print(f"{self.youtube_id}: get comments") self._check_config() comments_raw = self.get_yt_comments() - comments_format = self.format_comments(comments_raw) + self.format_comments(comments_raw) self.json_data = { "youtube_id": self.youtube_id, "comment_last_refresh": int(datetime.now().strftime("%s")), - "comment_comments": comments_format, + "comment_comments": self.comments_format, } def _check_config(self): @@ -77,7 +78,7 @@ class Comments: cleaned_comment = self.clean_comment(comment) comments.append(cleaned_comment) - return comments + self.comments_format = comments def clean_comment(self, comment): """parse metadata from comment for indexing""" @@ -110,6 +111,14 @@ class Comments: """upload comments to es""" _, _ = ElasticWrap(self.es_path).put(self.json_data) + vid_path = f"ta_video/_update/{self.youtube_id}" + data = { + "doc": { + "comment_count": len(self.comments_format) + } + } + _, _ = ElasticWrap(vid_path).post(data=data) + def delete_comments(self): """delete comments from es""" _, _ = ElasticWrap(self.es_path).delete() diff --git a/tubearchivist/home/templates/home/video.html b/tubearchivist/home/templates/home/video.html index df53a71c..a5fc0444 100644 --- a/tubearchivist/home/templates/home/video.html +++ b/tubearchivist/home/templates/home/video.html @@ -123,11 +123,14 @@ {% endfor %} {% endif %} -
-

Comments

-
+ {% if video.comment_count %} +
+

Comments: {{video.comment_count}}

+
+
-
+ + {% endif %}