T
T
Topsky2017-04-22 00:14:25
YouTube
Topsky, 2017-04-22 00:14:25

Youtube API how to request all videos from a channel?

Actually how? I use Node.JS and the "request" library https://github.com/request/request
I read the documentation, the maximum I can request is only 50 videos from the channel, in the response from the api server, there is a nextPageToken key, but I don’t understand how to do it a chain of requests for this very nextPageToken in order to go further along the entire chain.

`https://www.googleapis.com/youtube/v3/search?key=${key}&channelId=${channelId}&part=snippet,id&order=date&maxResults=50`;

I drew this code
request({url:url}, function(err, response, body){
    let data = JSON.parse(body);
    for(let i = 0; i < Math.fround(data.pageInfo.totalResults / maxResults); i++){
        let newUrl = url + '&pageToken=' + data.nextPageToken;
        request({url: newUrl}, function(err, response, body){
            newUrl = url + '&pageToken=' + JSON.parse(body).nextPageToken;
            console.log(JSON.parse(body).nextPageToken);
        })
    }
})

The channel has +450 videos, I didn’t think of a better solution and I take the result of the first request, and divide by the maximum number of requested videos, I get 9-10, then it turns out 10 passes per cycle, and in theory each request should update the newUrl variable and after that, again contact the api server for new data, and a new nextPageToken.
How to be?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question