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))
CodePudding user response:
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).