Add README.md and search_in_youtube_video_comments.py

This commit is contained in:
Benjamin Loison 2023-04-27 00:01:58 +02:00
commit 902d2084f1
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8
2 changed files with 52 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# Search in YouTube video comments

View File

@ -0,0 +1,51 @@
import requests, json, os, re
def getContentFromURL(url):
#url = f'https://yt.lemnoslife.com/noKey/{url}'
url = f'https://www.googleapis.com/youtube/v3/{url}&key={API_KEY}'
#print(url)
content = requests.get(url).text
data = json.loads(content)
return data
VIDEO_ID = '5PdEmeopJVQ'
API_KEY = 'AIzaSy...'
PATTERN_REGEX = '76115293'
nextPageToken = ''
pattern = re.compile(PATTERN_REGEX)
def treatComment(comment):
id = comment['id']
textOriginal = comment['snippet']['textOriginal']
if pattern.match(textOriginal):
url = f'https://www.youtube.com/watch?v={VIDEO_ID}&lc={id}'
#os.system(f"firefox -new-tab '{url}'")
print(f'{url} {textOriginal}')
while True:
data = getContentFromURL(f'commentThreads?part=snippet,replies&videoId={VIDEO_ID}&maxResults=100&pageToken={nextPageToken}')
#print(data)
for item in data['items']:
#print(item)
snippet = item['snippet']
totalReplyCount = snippet['totalReplyCount']
if totalReplyCount > 5:
parentId = item['id']
commentsNextPageToken = ''
while True:
commentsData = getContentFromURL(f'comments?part=snippet&parentId={parentId}&maxResults=100&pageToken={commentsNextPageToken}')
for item in commentsData['items']:
treatComment(item)
if not 'nextPageToken' in commentsData:
break
commentsNextPageToken = commentsData['nextPageToken']
else:
treatComment(snippet['topLevelComment'])
if totalReplyCount > 0:
for comment in item['replies']['comments']:
treatComment(comment)
if not 'nextPageToken' in data:
break
nextPageToken = data['nextPageToken']