From eb6e0a179cc93cbdf140e6c13391111389ac0ef5 Mon Sep 17 00:00:00 2001 From: Andrey <50486086+DarkCat09@users.noreply.github.com> Date: Thu, 5 Aug 2021 18:31:06 +0400 Subject: [PATCH] Script for searching YouTube comments by query --- yt_commentsort.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 yt_commentsort.py diff --git a/yt_commentsort.py b/yt_commentsort.py new file mode 100644 index 0000000..df6ace2 --- /dev/null +++ b/yt_commentsort.py @@ -0,0 +1,49 @@ +import requests + +class YtComment: + def __init__(self, content, authorname, authorurl): + self._content = content + self._authorname = authorname + self._authorurl = authorurl + @property + def content(self): + return self._content + @property + def authorname(self): + return self._authorname + @property + def authorurl(self): + return self._authorurl + + +def get_comments(videoid, apikey): + comments = requests.get( + 'https://www.googleapis.com/youtube/v3/commentThreads' + \ + f'?part=id,snippet&videoId={videoid}&textFormat=plainText&key={apikey}').json() + return comments['items'] + +def search_in_comments(comments, query): + filtered = [] + for comment in comments: + info = comment['snippet']['topLevelComment']['snippet'] + text = info['textDisplay'] + author_name = info['authorDisplayName'] + author_link = info['authorChannelUrl'] + if (text.find(query.lower()) > -1): + filtered.append(YtComment(text, author_name, author_link)) + return filtered + + +if __name__ == '__main__': + apikey = '***YOUR_API_KEY***' + video_id = input('Enter video identifier:\t') + search_query = input('Enter search query:\t') + comments = get_comments(video_id, apikey) + filtered = search_in_comments(comments, search_query) + for ytcomment in filtered: + print('----------') + print(ytcomment.authorname) + print(ytcomment.authorurl) + print(ytcomment.content) + print('----------') + input()