Script for searching YouTube comments by query

This commit is contained in:
Andrey 2021-08-05 18:31:06 +04:00 committed by GitHub
parent 240f149683
commit eb6e0a179c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

49
yt_commentsort.py Normal file
View file

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