Reputation: 5016
I am getting some weird results using url to retrieve youtube playlist items. First of all, youtube playlist can contain max of 200 playlist items.
When I run this I get correct results (50 items returned, total results 200, results per page 50, nextPageToken: "CDIQAA")
Then I keep running new request always with last nextPageToken:
100 results, nextPageToken: "CGQQAA",
150 results so far, nextPageToken: "CDIQAA"
Now this nextPageToken keeps repeating its the same at first nextPageToken, why, I still havent retrieved all 200 results?
Upvotes: 0
Views: 2101
Reputation: 61
I guess there some logical issue in your code, I've got CJYBEAA token after third request. Here function that works fine with your playlist ID and returns whole 200 video IDs:
def getPlaylistVideosIDs(playlist_id):
videos_IDs = []
search = YOUR_YOUTUBE_KEY.playlistItems().list(part='snippet', playlistId=playlist_id,
maxResults=50).execute()
try:
nextPageToken = search['nextPageToken']
except KeyError:
nextPageToken = None
for item in search['items']:
videos_IDs.append(item['snippet']['resourceId']['videoId'])
while (nextPageToken):
search = YOUR_YOUTUBE_KEY.playlistItems().list(pageToken=nextPageToken, part='snippet',
playlistId=playlist_id,
maxResults=50).execute()
for item in search['items']:
videos_IDs.append(item['snippet']['resourceId']['videoId'])
try:
nextPageToken = search['nextPageToken']
except KeyError:
break
return videos_IDs
Upvotes: 2