Add README.md
and search_in_youtube_video_comments.py
This commit is contained in:
commit
902d2084f1
51
search_in_youtube_video_comments.py
Normal file
51
search_in_youtube_video_comments.py
Normal 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']
|
||||
|
Reference in New Issue
Block a user