I'm able to access my watch history via the YouTube v3 data API, but it only returns my most recent 30 videos (though I see many more when I view the Watch History on YouTube.com).
And then when I watch another video, it returns 31. When I watch another, 32. If it can return more than 30, why didn't it return more originally? I understand that the API might have a limit, but why start at 30 then grow? And with paging, there really shouldn't be a limit, right?
I must be doing something wrong. Here's my code:
def getWatchHistory(youtube):
playlistId = getWatchHistoryPlaylistId(youtube)
videos = retrieveVideos(youtube, playlistId);
return videos # Only returns 30, 31, 32 videos, etc. though I have many more in my History
def getWatchHistoryPlaylistId(youtube):
channels_response = youtube.channels().list(
part="contentDetails",
mine=True,
).execute()
channel = channels_response["items"][0]
playlistId = channel["contentDetails"]["relatedPlaylists"]["watchHistory"]
return playlistId
def retrieveVideos(youtube, playlistId, nextPageToken=None):
# Search the specified playlist and list all videos
playlistItems_response = youtube.playlistItems().list(
part="snippet,contentDetails",
playlistId=playlistId,
maxResults=50,
pageToken=nextPageToken
).execute()
results = []
for x in playlistItems_response["items"]:
videoTitle = x["snippet"]["title"]
videoId = x["contentDetails"]["videoId"]
videoSpec = videoId + ": " + videoTitle
print 'adding to results: ' + videoSpec
results.append(videoSpec)
if ("nextPageToken" in playlistItems_response):
pageToken = playlistItems_response["nextPageToken"]
results.extend(retrieveVideos(youtube, playlistId, pageToken));
return results
else:
return results