【发布时间】:2015-04-12 09:03:15
【问题描述】:
我可以通过 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
【问题讨论】:
-
许多在其 API 中实现分页的网站仍然施加了限制。 Twitter 只允许您在某些端点返回 1,000 个帖子。其他人可能是日期驱动的(这可能解释了为什么您只能看到一些历史记录并且数量不定)。
标签: python youtube-api youtube-data-api