From 2e8ab33259c285f71625fff546cf1fd3022f959d Mon Sep 17 00:00:00 2001 From: pluja Date: Wed, 7 Oct 2020 16:22:16 +0200 Subject: [PATCH] Adds support for Youtube comments --- app/routes.py | 14 +++- app/templates/_video_comment.html | 25 ++++++ app/templates/video.html | 13 +++- youtube_data/comments.py | 123 ++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 app/templates/_video_comment.html create mode 100644 youtube_data/comments.py diff --git a/app/routes.py b/app/routes.py index 42106d6..3d503ec 100644 --- a/app/routes.py +++ b/app/routes.py @@ -30,6 +30,7 @@ import os ######################################### from youtube_data import videos as ytvids from youtube_data import search as yts +from youtube_data import comments as ytcomments ######################################### cache = Cache(config={'CACHE_TYPE': 'simple'}) @@ -437,6 +438,16 @@ def watch(): else: vid_urls = get_best_urls(info['video']['urls']) + # Get comments + try: + comments = ytcomments.video_comments(id) + if comments: + comments.sort(key=lambda x: x['likes'], reverse=True) + else: + comments = False + except: + comments = False + video={ 'title':info['video']['title'], 'description':Markup(markupString(info['video']['description'])), @@ -451,7 +462,8 @@ def watch(): 'isUpcoming': info['video']['isUpcoming'], 'thumbnail': info['video']['thumbnail'], 'nginxAudioUrl': audioUrl, - 'premieres': info['video']['premieres'] + 'premieres': info['video']['premieres'], + 'comments': comments } return render_template("video.html", video=video, title='{}'.format(video['title']), config=config, urls=vid_urls) diff --git a/app/templates/_video_comment.html b/app/templates/_video_comment.html new file mode 100644 index 0000000..ee10a7a --- /dev/null +++ b/app/templates/_video_comment.html @@ -0,0 +1,25 @@ +
+ +
+ {% if comment.authorIsChannelOwner %} + + {{comment.author}} + {% else %} + {{comment.author}} + {% endif %} + +
+ {{comment.text}} +
+
+
\ No newline at end of file diff --git a/app/templates/video.html b/app/templates/video.html index 9294895..228c27f 100644 --- a/app/templates/video.html +++ b/app/templates/video.html @@ -88,8 +88,17 @@

{{video.description}}

- - + + + {% if comments != False %} +
+

Comments

+ {% for comment in video.comments %} + {% include '_video_comment.html' %} + {% endfor %} +
+ {%endif%} +