【问题标题】:iOS, GTLFramework - how to fetch all videos from a channel using pageTokensiOS,GTLFramework - 如何使用 pageTokens 从频道中获取所有视频
【发布时间】:2015-02-28 00:02:57
【问题描述】:

这是我从特定频道检索 YouTube 视频列表的代码:

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);
    }
}];

并且,在cellForRowAtIndexPath 方法中:

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

查询接受 max 50 作为最大结果限制。但我需要显示整个列表,大约 250 个视频。

我该怎么做?我阅读了有关使用pageTokens 的信息,但我找不到任何关于如何使用pageTokens、在哪里获取它们以及在哪里传递它们的示例或代码?

【问题讨论】:

  • 我认为,您需要获取页面令牌并再次将其传递给查询以获取下一个 50 的列表
  • 我如何获得 pagetoken?

标签: ios objective-c youtube-api


【解决方案1】:

您在查询后收到的GTLYouTubeListResponse 有一个名为nextPageTokenNSString 属性。
此属性表示“下一页”的“地址”,以防您有多个“页面”的搜索结果,
(意思是,搜索结果的数量高于您在 maxResults 属性中设置的数量,正如您所说,它有 50 个结果的限制)

因此,以您的问题为例,总共有 250 个结果,您有 5 个搜索结果“页面”,每个“页面”上有 50 个搜索结果。

GTLYouTubeQuery 有一个对应的pageToken 属性,它“告诉”查询您想要接收的结果的“页面”。

可能是实现这一目标的另一种方法,但这只是我的想法
我认为这是实现这一目标的非常简单直接的方法,
无论如何,下面的示例使用您的代码来演示如何使用此属性。

重要提示!!!
在下面的代码中,我在 ALL 的搜索结果中“循环”,
这只是为了说明目的,
在您的应用中,您可能还想创建自己的自定义限制,
因此,如果用户搜索具有大量结果的通用关键字,您将不会尝试获取所有结果,除其他缺点外,这可能会比他读到的更多,造成不必要的网络和内存使用,以及将“占用”你的谷歌开发者积分(或任何它被称为的东西,当它“花费”你进行谷歌 API 调用时)

所以,如果我们使用您的原始代码-

// 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's 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's pageToken property  
        // to be our response's 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");
        }
    }];  
}  

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2021-03-14
    • 2014-10-29
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多