Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comments list pagination is not working as it is expected to work #496

Closed
Mojiboi opened this issue Sep 8, 2023 · 1 comment
Closed

Comments

@Mojiboi
Copy link

Mojiboi commented Sep 8, 2023

i am trying fetch all comments against a video id but i am unable to get them all even if they are only 54 comments here is code i am using for this purpose

`next_page_token = None # Initialize the next page token

            while True:
                # Make a request to retrieve comments with pagination
                comments_response = youtube.comments.list(
                    part='snippet',
                    videoId=video_id,
                    textFormat='plainText',
                    maxResults=min(max_results_per_file, 100),  # Maximum 100 per request
                    pageToken=next_page_token, # Use the next page token for pagination
                    order='time'  # Sort comments by time  
                ).execute()
                print(f"Comments retrieved in this request: {video_id} : {len(comments_response['items'])}")
                comments = []
                # print(f"{comments_response['items']}")
                print(f"{comments_response['nextPageToken']}")
                
                for comment in comments_response['items']:
                    comment_data = {
                        'Youtube ID': video_id,
                        'Cid': comment['id'],
                        'Author': comment['snippet']['topLevelComment']['snippet']['authorDisplayName'],
                        'Like Count': comment['snippet']['topLevelComment']['snippet']['likeCount'],
                        'Time': comment['snippet']['topLevelComment']['snippet']['publishedAt'],
                        'Text': comment['snippet']['topLevelComment']['snippet']['textDisplay']
                    }
                    comments.append(comment_data)
                print(f"Next page token: {next_page_token}")

                # Add comments to the overall list
                all_comments.extend(comments)
                print(f"{len(all_comments)} comments collected for yt id: {video_id}")

                if 'nextPageToken' in comments_response:
                    next_page_token = comments_response['nextPageToken']
                else:
                    break  # No more pages`
@Mojiboi
Copy link
Author

Mojiboi commented Sep 8, 2023

i am trying fetch all comments against a video id but i am unable to get them all even if they are only 54 comments here is code i am using for this purpose

`next_page_token = None # Initialize the next page token

            while True:
                # Make a request to retrieve comments with pagination
                comments_response = youtube.comments.list(
                    part='snippet',
                    videoId=video_id,
                    textFormat='plainText',
                    maxResults=min(max_results_per_file, 100),  # Maximum 100 per request
                    pageToken=next_page_token, # Use the next page token for pagination
                    order='time'  # Sort comments by time  
                ).execute()
                print(f"Comments retrieved in this request: {video_id} : {len(comments_response['items'])}")
                comments = []
                # print(f"{comments_response['items']}")
                print(f"{comments_response['nextPageToken']}")
                
                for comment in comments_response['items']:
                    comment_data = {
                        'Youtube ID': video_id,
                        'Cid': comment['id'],
                        'Author': comment['snippet']['topLevelComment']['snippet']['authorDisplayName'],
                        'Like Count': comment['snippet']['topLevelComment']['snippet']['likeCount'],
                        'Time': comment['snippet']['topLevelComment']['snippet']['publishedAt'],
                        'Text': comment['snippet']['topLevelComment']['snippet']['textDisplay']
                    }
                    comments.append(comment_data)
                print(f"Next page token: {next_page_token}")

                # Add comments to the overall list
                all_comments.extend(comments)
                print(f"{len(all_comments)} comments collected for yt id: {video_id}")

                if 'nextPageToken' in comments_response:
                    next_page_token = comments_response['nextPageToken']
                else:
                    break  # No more pages`

its because i was not getting the comment replies. youtube shows the comments count including replies.

@Mojiboi Mojiboi closed this as completed Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant