Sunday, July 20, 2014

Videos from youtube channel in iOS app (part 2)

We want to display list of channel's uploads. That list has an identifier, we need to find out, and it can be done with this request:
1. Channel details.

- (void) channelDetails {
   GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
   service.APIKey = API_KEY;
   GTLQueryYouTube *query = [GTLQueryYouTube queryForChannelsListWithPart:@"contentDetails"];
   query.forUsername = CHANNEL_NAME;
   [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
      if (!error) {
          GTLYouTubeChannelListResponse *items = object;
          for (GTLYouTubeChannelListResponse * item in items){
             NSLog(@"item: %@", item.JSON[@"contentDetails"]); 
          }
      } else{
           NSLog(@"%@", error);
      }
   }];
}


Response is in this format:

item: {
   googlePlusUserId = XXXXXXXXXX;
   relatedPlaylists = {
       uploads = "YYYYYYYYYYYYY";
   };
}

Store the value from "uploads" field to UPLOADS_LIST_ID.

2. List of uploaded videos. To receive list of uploaded videos we need to make a playlist items query with UPLOADS_LIST_ID as playlist id.


- (void) getUploads {
   GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];   service.APIKey = API_KEY;
   GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
   query.playlistId = UPLOADS_LIST_ID;
   query.maxResults = MAX_RESULTS; 
   [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
      if (!error) {
         GTLYouTubePlaylistItemListResponse *playlistItems = object;
         for (GTLYouTubePlaylistItem *playlistItem in playlistItems) {
            [self.identifiers addObject:playlistItem.snippet.resourceId.JSON[@"videoId"]];
         }
      [self getVideos];
     }else {
      NSLog(@"%@", error);
     }
   }];
}

3. List of uploaded videos with all information. Playlist queries, like one from above give us list of videos with details like: video id, title and thumbnail. To be able to read information like video duration and number of views, we need to make video list query. We can use list of video identifiers as filter for that query. For that reason we stored them in self.identifiers property of NSMutableDictionary type.

- (void) getVideos {
   GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
   service.APIKey = API_KEY;
   GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosListWithPart:@"id, snippet, contentDetails, statistics, player"];
   query.identifier = [[self.identifiers valueForKey:@"description"] componentsJoinedByString:@","];
   [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error){
      if (!error) {
         GTLYouTubeVideoListResponse *videolistItems = object;
         for (GTLYouTubeVideo *videoItem in videolistItems){
            [self.dataSource addObject:videoItem];
         }
         [self.tableView reloadData]; 
      } else {
      NSLog(@"%@", error);
      }
   }];
}

You may also need query for list of playlists for fetching all information from one channel. For this query we will need CHANNEL_ID, not name.
4. List of channel's playlists:

- (void) loadPlaylists {
   GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
   service.APIKey = API_KEY;
   GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistsListWithPart:@"id, snippet, contentDetails"];
   query.channelId = CHANNEL_ID;
   query.maxResults = MAX_RESULTS;
   [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
      if (!error) {
         GTLYouTubePlaylistListResponse *items = object;
         for (GTLYouTubePlaylist * item in items) {
            [self.dataSource addObject:item];
         }
         [self.tableView reloadData];
      } else {
         NSLog(@"%@", error);
      }
   }];
}

15 comments:

  1. this is a great tutorial.

    what is "self.identifiers" an NSArray
    what is "self.dataSource" also an NSArray

    please let me know thanks, that the only thing i don't understand here, but i believe that is because of my lack of knowledge not because the tutorial this is the only thing on the net about this that is clean and clear about youtube api v3 thanks again and please get back to me thank you

    ReplyDelete
    Replies
    1. Hi, thanks for compliments.

      _identifiers is an (mutable) array of NSString objects - youtube video identifiers, e.g.: @[4uY7yLEVtIU, eSiPMQxPPXw, AGJIjSqndj0]

      while _dataSource is an array of GTLYouTubeVideo objects.

      Both need to be initialized before using in functions, e.g. in viewDidLoad:

      self.identifiers = [[NSMutableArray alloc] init];
      self.dataSource = [[NSMutableArray alloc] init];

      Because GTLYouTubePlaylistItem doesn't have information about number of views and video duration I used query that takes list of identifiers and returns list of GTLYouTubeVideo objects, which have all those information.

      Delete
    2. Thanks so much for responding your tutorial is the only one that is in detail like this its the best one on the net i think thanks heaps

      Delete
  2. How would you do a simple search. Thank you

    ReplyDelete
  3. Search as in a keyword search. Sorry, I might not have been clear. Thank you

    ReplyDelete
    Replies
    1. Hi,

      Here is the query for searching videos with KEYWORD:

      GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
      service.APIKey = API_KEY;

      GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"];
      query.q = KEYWORD;
      query.maxResults = MAX_RESULTS;
      query.type = @"video";

      NSMutableArray *arrayOfIdentifiers = [[NSMutableArray alloc] init];

      [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error){
      if (!error) {
      GTLYouTubeSearchListResponse *videolistItems = object;

      for (GTLYouTubeSearchResult *videoItem in videolistItems) {
      [arrayOfIdentifiers addObject:videoItem.identifier.JSON[@"videoId"]];

      }
      self.identifiers = [NSArray arrayWithArray:arrayOfIdentifiers];
      [self getVideos];

      } else {
      NSLog(@"%@", error);
      }
      }];

      Delete
    2. Thank.
      Are you aware of this problem : https://code.google.com/p/gdata-issues/issues/detail?id=5770

      Apikey doesnt seem to work.

      Delete
    3. As I understand, that is the problem with Google API configuration, not with the code itself.

      Delete
  4. Awesome article.
    Any way to get videos in a channel using their tags?

    ie. get all containing "TAG"

    ReplyDelete
    Replies
    1. Sorry for late response. I'm not aware of any other solution than to add a tag as a q parameter (q: The q parameter specifies the query term to search for.). However, the response can have videos that doesn't contain specified term as a tag, but also as a part of a name.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. i get his in getVideos Error Domain=com.google.GTLJSONRPCErrorDomain Code=-32602 "(No filter selected.)" ??

    ReplyDelete
    Replies
    1. I suppose you didn't provide query.identifier (described in step 3). You can pass the array of video identifiers, if you want to get more details about the videos. However, if you need just a list of uploaded videos, consider using only the function described in step 2.

      Delete
  7. I want to use GTLServiceYouTube in swift. Can you please help me how we can use this?. Or any other library like GTLServiceYouTube

    ReplyDelete