The piece of code below retrieves comments from a YouTube video.
YouTube’s API has a quota limit of 10,000 units. So is it possible to display the used and the remaining units in the same program?
def getAllTopLevelCommentReplies(topCommentId, replies, token):
replies_response=youtube.comments().list(part='snippet',
maxResults=100,
parentId=topCommentId,
pageToken=token).execute()
for item in replies_response['items']:
replies.append(item['snippet']['textDisplay'])
if "nextPageToken" in replies_response:
return getAllTopLevelCommentReplies(topCommentId, replies, replies_response['nextPageToken'])
else:
return replies
def get_comments(youtube, video_id, comments=[], token=''):
totalReplyCount = 0
replies=[]
video_response=youtube.commentThreads().list(part='snippet',
videoId=video_id,
pageToken=token).execute()
for item in video_response['items']:
comment = item['snippet']['topLevelComment']
text = comment['snippet']['textDisplay']
totalReplyCount = item['snippet']['totalReplyCount']
if (totalReplyCount > 0):
comments.extend(getAllTopLevelCommentReplies(comment['id'], replies, None))
else:
comments.append(text)
replies = []
if "nextPageToken" in video_response:
return get_comments(youtube, video_id, comments, video_response['nextPageToken'])
else:
return comments
youtube = build('youtube', 'v3',developerKey=api_key)
comments = get_comments(youtube,video_id)
print(len(comments))
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
According to Youtube’s API reference https://developers.google.com/youtube/v3/docs there’s no way to retrieve the remaining quota using the API itself.
But you can find out your daily quota usage and limit in your Google Developer Console (https://console.developers.google.com).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0