Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3710

Unable to get all comments in a youtube video (Code gets stuck in the last 99%)

$
0
0

I'm using code that was given in How to extract all YouTube comments using YouTube API? (Python). I'm currently facing a problem with it in that it collects comments until it reaches the 99% mark then stops but doesn't crash or give any error outputs. I checked and confirmed that it was not the quota count too.

def GetVideoComments(youtube, video_id, datetime_to_check, comments=[], token_list = [], token="") :    # Stores the total reply count a comment has    totalReplyCount = 0    # Replies of the comments    replies = []    try :        request = youtube.commentThreads().list(part="snippet",                                                 videoId=video_id,                                                 maxResults=100,                                                pageToken=token)        response = request.execute()    except HttpError as error :          print("Error", error)        progress_bar.close()        return comments    progress_bar.update(len(response["items"]))    for item in response["items"] :        comment = item["snippet"]["topLevelComment"]        timestamp = comment["snippet"]["publishedAt"]        # Skip this comment if it is past the time period we want        if (timestamp > datetime_to_check) :            continue        text = comment["snippet"]["textDisplay"]        comments.append({"timestamp" : timestamp,"comment" : text,"sentiment" : {},"meaningful words" : ""})        # Get the total reply count:         totalReplyCount = item["snippet"]["totalReplyCount"]        # Check if the total reply count is greater than zero,         # if so,call GetVideoReplies        # and extend the "comments" returned list.        if (totalReplyCount > 0) :             comments += GetVideoReplies(comment["id"], datetime_to_check, replies, token_list, None)        # replies must be cleared as if GetVideoComments is called recursively,        # replies will still contain its current elements        replies = []    if "nextPageToken" in response and response["nextPageToken"] not in token_list :         token_list.append(response["nextPageToken"])        return GetVideoComments(youtube, video_id, datetime_to_check, comments, token_list, response["nextPageToken"])    else :        progress_bar.close()        return commentsdef GetVideoReplies(comment_ID, datetime_to_check, replies, token_list, token) :     try :        request = youtube.comments().list(part="snippet",                                           maxResults=100,                                           parentId=comment_ID,                                          pageToken=token)        response = request.execute()    except HttpError as error :          print("Error", error)        progress_bar.close()        return replies    progress_bar.update(len(response["items"]))    for item in response["items"] :        # Append the reply's text to replies        if (item["snippet"]["publishedAt"] > datetime_to_check) :            continue        replies.append({"timestamp" : item["snippet"]["publishedAt"],"comment" : item["snippet"]["textDisplay"],"sentiment" : {},"meaningful words" : ""})    if "nextPageToken" in response and response["nextPageToken"] not in token_list :         token_list.append(response["nextPageToken"])        return GetVideoReplies(comment_ID, datetime_to_check, replies, token_list, response["nextPageToken"])    else:        return replies

Viewing all articles
Browse latest Browse all 3710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>