Showing posts with label videos. Show all posts
Showing posts with label videos. Show all posts

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

Monday, June 30, 2014

Videos from youtube channel in iOS app (part 1)


1. Download Google Objective C Client. For developing any iOS application, that use some of Google APIs, we need to have Google libraries downloaded. In terminal run this command:
svn checkout http://google-api-objectivec-client.googlecode.com/svn/trunk/ google-api-objectivec-client-read-only
2. Build library. From downloaded items in Source folder open GTL.xcodeproj and choose iOS simulator and GTLTouchStaticLib as target. Then build library again, this time for iOS device. From Organizer>Products go to derived data by clicking arrow:


From Build>Products>Debug-iphoneos copy to e.g Desktop libGTLTouchStaticLib.a and rename it to e.g. libGTLTouchStaticLib1.a. Do the same for the file from Build>Products>Debug-iphonesimulator and rename it to libGTLTouchStaticLib2. With this command in Terminal, build fat static library


lipo -create -output libGTLTouchStaticLib.a libGTLTouchStaticLib1.a libGTLTouchStaticLib2.a


3. Adding source files for the library. Find header files in Source>Services>Youtube>Generated and add them to project (select: copy items into destination group's folder and create groups for any added folders). However, you don't need GTLYouTube_Sources.m file, so you should deleted it. From Derived Data folder: Build>Products>Debug-iphoneos add Headers folder to the project.

4. Adding frameworks and libraries. Add Security.framework, SystemConfiguration.framework and libGTLTouchStaticLib.a to project.

Now you can run your application on both iOS device and simulator, and start using Google Youtube API for fetching videos.