我可以通过 YouTube v3 数据 API 访问我的观看历史记录,但它只返回我最近的 30 个视频(虽然我在 YouTube.com 上查看观看历史记录时看到更多)。
然后我再看一个视频,它返回31。再看一个视频,32。如果它能返回30多个,为什么它原来不返回更多?我知道 API 可能有限制,但为什么从 30 开始然后增长?对于分页,真的不应该有限制,对吧?
我一定是做错了什么。这是我的代码:
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
请您参考如下方法:
更新:自 2016 年 9 月 15 日起,watchHistory 不再以任何身份提供,因此遗憾的是,它现在已无关紧要。
这似乎是一个最初于 2013 年报告的已知错误。在 Google 代码线程中解释了完全相同的行为:https://code.google.com/p/gdata-issues/issues/detail?id=4642
“我得到了结果,但就像一些海报一样,它们是最近的视频(可能是一天的值(value))。我似乎找不到任何关于检索时间限制的文档。有人找到了解决方法或想出了办法历史有多长?” (最近对线程的评论)
在 Google 决定修复此问题之前,我似乎运气不好。希望有人证明我(以及该 Google 代码线程中的其他人)是错误的。
