add comments to video template if available

This commit is contained in:
simon 2022-11-18 09:44:20 +07:00
parent 939cd7fb62
commit 2818bd5d52
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 22 additions and 7 deletions

View File

@ -193,6 +193,9 @@
}
}
},
"comment_count": {
"type": "long"
},
"stats" : {
"properties" : {
"average_rating" : {

View File

@ -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()

View File

@ -123,11 +123,14 @@
</div>
{% endfor %}
{% endif %}
{% if video.comment_count %}
<div class="comments-section">
<h3>Comments</h3>
<h3>Comments: {{video.comment_count}}</h3>
<div id="comments-list" class="comments-list">
</div>
</div>
<script>getComments('{{ video.youtube_id }}')</script>
{% endif %}
</div>
<script>
var videoData = getVideoData('{{ video.youtube_id }}');