所以我有这个脚本,可以提取用户喜欢的视频并创建一个包含所有喜欢的视频的公共播放列表,但由于存在配额限制,我每天无法添加超过 200 个视频(playlistItems().insert() 成本为 50 配额/10,000) .
有什么方法可以批量添加 50 个视频或其他视频以避免配额用完吗?
我当前的代码:
youtube = googleapiclient.discovery.build(
"youtube", "v3", credentials=credentials)
#code for fetching video id's in a list...
video_ids= [id1,id2,id3,id4,...]
playlist_id= "<playlist id>"
for id in video_ids:
youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlist_id,
"resourceId": {
"kind": "youtube#video",
"videoId": id
}
}
}
).execute()
我听说有
batch = youtube.new_batch_http_request()
但这似乎对我不起作用,只添加 1 个视频,我认为这仍然与上面的代码花费相同数量的配额。
编辑:感谢本杰明,找到了上述的另一种方法,但现在坚持这个功能只返回 991 个视频 ID,但喜欢的播放列表有 1900 个视频。
def get_song_ids(youtube:object):
video_ids,songs_names,songs_ids = [],[],[]
next_page_token = None
while True:
try:
# Getting the IDs of the user's liked videos.
liked_videos_response = youtube.videos().list(
part="id,snippet,contentDetails,status",
myRating="like",
maxResults=50,
pageToken=next_page_token
).execute()
#filtering song id's and extracting other info
for item in liked_videos_response["items"]:
video_ids.append(item["id"])
title = item["snippet"]["title"]
category_id = item["snippet"].get("categoryId")
if category_id == "10":
songs_names.append(title)
songs_ids.append(item["id"])
# Checking if there's more videos in playlist
next_page_token = liked_videos_response.get("nextPageToken")
if not next_page_token:
break
except HttpError as error:
print(error)
print("Total: ",len(video_ids),len(songs_ids))
return video_ids,songs_ids
JavaScript 的解决方案如下
/**
* A recursive function that accepts a single track and makes repeated calls to the endpoint until the response is successful.
* @param {String} accessToken The Google Auth Access Token required to make POST requests.
* @param {Object} resourceId The Youtube track to POST into the Youtube playlist.
* @param {String} playlistId The playlistId of what playlist to POST the tracks to.
*/
const postYoutubeTrack = async (accessToken, singleResourceId, playlistId) => {
const request = await fetch(`https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet`, {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
snippet: {
playlistId: playlistId,
resourceId: {
kind: singleResourceId.kind,
videoId: singleResourceId.videoId,
},
},
}),
});
if (request.ok) {
const response = await request.json();
return response;
}
postYoutubeTrack(accessToken, singleResourceId, playlistId);
};
export default postYoutubeTrack;