YouTube API v3 returns truncated watch history

2019-04-02 01:10发布

问题:

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

回答1:

Update: As of Sept 15, 2016 the watchHistory is no longer provided in any capacity so this is, sadly, now irrelevant.

It seems like this is a known bug originally reported in 2013. The exact same behavior is explained on a Google Code thread: https://code.google.com/p/gdata-issues/issues/detail?id=4642

"I get back results but like a few posters up, they are very recent videos (possibly a day's worth). I can't seem to find any documentation on a time limit for retrieval. Anybody found a workaround or figured out how long the history is available?" (recent comment on the thread)

Looks like I'm out of luck until Google decides to fix this. Hopefully someone proves me (and everone else in that Google Code thread) wrong.