IOS, GTLFramework - how to get all videos from a channel using pageTokens

Here's my code for getting a list of YouTube videos from a specific channel:

GTLServiceYouTube *service;
self.vidInfos = [[NSMutableArray alloc] init];
service = [[GTLServiceYouTube alloc] init];
service.APIKey = @"my-api-key";

GTLQueryYouTube *query1 = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
query1.playlistId = @"the-playlist-id-from-utube-channel";
query1.maxResults = 50;

[service executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (!error) {
        GTLYouTubePlaylistItemListResponse *playlistItems = object;
        for (GTLYouTubePlaylistItem *playlistItem in playlistItems) {
            [self.vidInfos addObject:playlistItem];
        }
        [self.tableView reloadData];
    }else {
        NSLog(@"%@", error);
    }
}];

      

And in the method cellForRowAtIndexPath

:

GTLYouTubePlaylistItem *itemToDisplay = [self.vidInfos objectAtIndex:indexPath.row];
cell.textLabel.text = itemToDisplay.snippet.title;

      

The request takes max 50 as the maximum result limit. But I need to display the whole list, which is about 250 videos.

How should I do it? I read about the usage pageTokens

, but I could not find any sample or code on how to use pageTokens

, where to get them, and where to pass them?

+3


source to share


1 answer


The A GTLYouTubeListResponse

that you get after doing the query has a property NSString

called nextPageToken

.
This property specifies the "address" on the next page if you have multiple "pages" of search results,
(Value, the number of search results is greater than the number you set in the property maxResults

, which you said has 50 result limits)

So, using your question as an example with only 250 results, you have 5 search results pages out of 50 search results on each page.

GTLYouTubeQuery

have a corresponding property pageToken

that "tells" the query what the "page" is of the results you want.

Perhaps this is another way to achieve this, but it was just on my head. and I think this is a pretty simple and straightforward way to achieve
this.Anyway, the example below uses your code to demonstrate how this property can be used.



IMPORTANT NOTE !!!
In the code below, I am "looping" through ALL of the search results,
This is just for illustrative purposes,
In your application, you probably also want to create your own custom limit,
So if a user searches for a general purpose keyword that has TONS of results, you will not try to extract them, which, among other disadvantages, will probably be more than it will ever read, make unnecessary use of network and memory, and will "depend" on your google developer points (or whatever it is called when it " stands for "to make Google API calls)

So if we use your original code

// First, make GTLServiceYouTube a property, so you could access it thru out your code  
@interface YourViewControllerName ()  
@property (nonatomic, strong) GTLServiceYouTube *service;  
@end  

// don't forget to still initialise it in your viewDidLoad
self.vidInfos = [[NSMutableArray alloc] init];
service = [[GTLServiceYouTube alloc] init];
service.APIKey = @"my-api-key";

GTLQueryYouTube *query1 = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
query1.playlistId = @"the-playlist-id-from-utube-channel";
query1.maxResults = 50;

// After you've created the query, we will pass it as a parameter to our method  
// that we will create below
[self makeYoutubeSearchWithQuery:query1];

// Here we create a new method that will actually make the query itself,  
// We do that, so it'll be simple to call a new search query, from within  
// our query completion block
-(void)makeYoutubeSearchWithQuery:(GTLQueryYouTube *)query {  
    [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
        if (!error) {
            GTLYouTubePlaylistItemListResponse *playlistItems = object;
            for (GTLYouTubePlaylistItem *playlistItem in playlistItems) {
                [self.vidInfos addObject:playlistItem];
            }
            [self.tableView reloadData];
        }else {
            NSLog(@"%@", error);
        }  

        // Here we will check if our response, has a value in the nextPageToken  
        // property, meaning there are more search results 'pages'.  
        // If it is not nil, we will just set our query pageToken property  
        // to be our response nextPageToken, and will call this method  
        // again, but this time pass the 'modified' query, so we'll make a new  
        // search, with the same parameters as before, but that will ask the 'next'  
        // page of results for our search  
        // It is advised to add some sort of your own limit to the below if statement,  
        // such as '&& [self.vidInfos count] < 250'
        if(playlistItems.nextPageToken) {  
            query.pageToken = playlistItems.nextPageToken;  
            [self makeYoutubeSearchWithQuery:query];
        } else {  
            NSLog(@"No more pages");
        }
    }];  
}  

      

Good luck.

0


source







All Articles