Sunday, 23 April 2017

Youtube Api, how to parse all vids with a nextPageToken?

I'm using Node.JS, and the "request" library https://github.com/request/request I read the documentation, as much as I can request only 50 videos from the channel, in response from the api server, there is a nextPageToken key, but I do not understand how to make the chain of requests for this nextPageToken to go through the whole chain.

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

I sketched out 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);
        })
    }
})

At channel +450 video, did not think of the best solution and take the result of the first request, and divide by the maximum number of requested video, I get 9-10, then it turns out 10 passes per cycle, and in theory each request should update the variable newUrl And after, again access to the server for new data, and a new nextPageToken.

How to be?



via CHBS

No comments:

Post a Comment