pt_cqw
Last Updated: February 25, 2016
·
2.887K
· webdevotion
Cf6fd030015a87035997fd19374e7746

Get Youtube's popular videos using the Google API

This is not the most beautiful code in the world, it's the result of just getting things up and running. Don't forget to create a project on the relevant google pages to get API access.
I also had to use the browser API key instead of the iOS API key.

- (GTLServiceYouTube *)youTubeService {
    static GTLServiceYouTube *service;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        service = [[GTLServiceYouTube alloc] init];

        // Have the service object set tickets to fetch consecutive pages
        // of the feed so we do not need to manually fetch them.
        service.shouldFetchNextPages = YES;

        // Have the service object set tickets to retry temporary error conditions
        // automatically.
        service.retryEnabled = YES;
    });
    return service;
}

- (id)init;
{
    self = [super init];
    if (self) {
        self.videos = [[NSMutableArray alloc] init];
        self.youTubeService.APIKey = @"your_api_key";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    GTLServiceYouTube *service = self.youTubeService;
    GTLQueryYouTube *query;

    query = [GTLQueryYouTube queryForGuideCategoriesListWithPart:@"snippet"];
    query.regionCode = @"US";
    query.hl = @"en-US";


    __block NSMutableArray *blockVideos = self.videos;

    // let's get the categories
    ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *blockTicket, GTLYouTubeGuideCategoryListResponse *list, NSError *error) {

        GTLYouTubeGuideCategory *cat = [list.items objectAtIndex:0];

        GTLQueryYouTube *channelsQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id,snippet"];
        channelsQuery.categoryId = cat.identifier;
        channelsQuery.maxResults = 10; // only need one, but maxresults = 1 is slower than 10


        // let's get the channels for the given category
        __unused GTLServiceTicket *channelsTicket = [service executeQuery:channelsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannelListResponse *channelList, NSError *videoError) {

            // we are only interested in one channel: the best of the best
            [channelList.items enumerateObjectsUsingBlock:^(GTLYouTubeChannel *channel, NSUInteger idx, BOOL *stop) {

                if( [channel.snippet.title isEqualToString:@"Popular on YouTube - Worldwide"] )
                {
                    // get related playlists for our channel

                    GTLQueryYouTube *playlistsQuery = [GTLQueryYouTube queryForPlaylistsListWithPart:@"id,snippet"];
                    playlistsQuery.channelId = channel.identifier;
                    playlistsQuery.maxResults = 20;

                    __unused GTLServiceTicket *playlistsTicket = [service executeQuery:playlistsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubePlaylistListResponse *playlistsResponse, NSError *error) {

                        // list of related playlists: only 1
                        [playlistsResponse.items enumerateObjectsUsingBlock:^(GTLYouTubePlaylistItem *playlist, NSUInteger idx, BOOL *stop) {
                            NSLog(@"obj: %@", playlist.identifier);

                            GTLQueryYouTube *videosQuery = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
                            videosQuery.playlistId = playlist.identifier;
                            videosQuery.maxResults = 20;

                            __unused GTLServiceTicket *videosTicket = [service executeQuery:videosQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubePlaylistItemListResponse *playlistItemsResponse, NSError *error) {


                                [playlistItemsResponse.items enumerateObjectsUsingBlock:^(GTLYouTubePlaylistItem *playlistItem, NSUInteger idx, BOOL *stop) {
                                    GTLYouTubeVideo *video = (GTLYouTubeVideo *)playlistItem.snippet.resourceId;
                                    NSString *videoTitle = playlistItem.snippet.title;
                                    NSString *videoId = [video.JSON valueForKey:@"videoId"];

                                    [blockVideos addObject:@{@"title":videoTitle,@"identifier":videoId}];
                                }];

                                [self.tableView reloadData];

                            }];
                        }];
                    }];

                    return;
                }

            }];
        }];
    }];


}
Say Thanks
Respond