Search code examples
objective-cios5youtube-apigdata-api

Youtube GData Videos in a Playlist


Good Day, I'm new to this Youtube GData api and i'm having trouble with accessing the list of videos of a particular playlist.

What I did first was get the list of playlist of a particular user. It returned something like this GDataEntryYouTubePlaylistLink 0x888ea20: {v:2.1 title:Playlist Title summary: contentSrc:https://gdata.youtube.com/feeds/api/playlists/PL5......92D127 etag:W/"D04N.....WhJVEU8." authors:1 categories:1 links:related,alternate,self id:tag:youtube.com,2008:user:MyYoutubeUser:playlist:PL5......92D127 countHint:7 unparsed:<media:group>}

I've learned that countHint is the number of videos on that playlist. Now, I want to get those videos.

I tried creating a function that would do so, but I don't know what FeedID I'll use:

    GDataServiceGoogleYouTube *service = [self youTubeService];

NSString *feedID = kGDataYouTubeUserFeedIDPlaylists; //this was the feedID i used to get the list of playlist of a user
NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"MyYoutubeUser"
                                                        userFeedID:feedID];

[service fetchFeedWithURL:feedURL
                 delegate:self
        didFinishSelector:@selector(request:finishedLoading:error:)];

Solution

  • I was able to find solution to my question. And I hope it can also help you.

    In my viewDidLoad, i called loadYoutube and loadYoutubePlaylist functions.

    • loadYoutube -> gives us the URL given the user and what feedID. In my case, feedID is kGDataYouTubeUserFeedIDUploads because I only want to get the playlists and the videos uploaded by the user.
    • loadYoutubePlaylist -> gives us the URL of the playlists of the user. feedID is kGDataYouTubeUserFeedIDPlaylists
    • loadYoutubeVideosPerPlaylist -> gives us the URL of the videos per playlist.

    This is how the codes look like:

    - (void)loadYoutube
    {
        GDataServiceGoogleYouTube *service = [self youTubeService];
    
        NSString *uploadsID = kGDataYouTubeUserFeedIDUploads;
        NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"USERNAME"
                                                             userFeedID:uploadsID];
        NSLog(@"loadYoutube URL: %@", feedURL);
        [service fetchFeedWithURL:feedURL
                         delegate:self
                didFinishSelector:@selector(request:finishedLoadingYoutubeWithFeed:error:)];
    }
    
    - (void)loadYoutubePlaylist
    {
        GDataServiceGoogleYouTube *service = [self youTubeService];
    
        NSString *feedID = kGDataYouTubeUserFeedIDPlaylists;
        NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"USERNAME"
                                                                userFeedID:feedID];
        NSLog(@"loadYoutubePlaylist URL: %@", feedURL);
        [service fetchFeedWithURL:feedURL
                         delegate:self
                didFinishSelector:@selector(request:finishedLoadingPlaylistWithFeed:error:)];
    }
    
    - (void)loadYoutubeVideosPerPlaylist:(NSString *)urlString
    {
        GDataServiceGoogleYouTube *service = [self youTubeService];
        NSURL *feedURL = [NSURL URLWithString:urlString];
    
        NSLog(@"loadYoutubeVideosPerPlaylist URL: %@", feedURL);
    
        [service fetchFeedWithURL:feedURL
                         delegate:self
                didFinishSelector:@selector(request:finishedLoadingYoutubeVideosPerPlaylistWithFeed:error:)];
    }
    

    And here are the Youtube requests..

    #pragma mark - Youtube Requests
    
    - (void)request:(GDataServiceTicket *)ticket finishedLoadingYoutubeWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
    {
    
    }
    
    - (void)request:(GDataServiceTicket *)ticket finishedLoadingPlaylistWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
    {
        if (kGDataYouTubeUserFeedIDPlaylists) {
            self.playlistFeed = (GDataFeedYouTubeVideo *)aFeed;
    
            NSArray *feedArray = [self.playlistFeed entries];
            for (int i = 0; i < [feedArray count]; i++) {
                GDataEntryBase *entry = [[self.playlistFeed entries] objectAtIndex:i];
                NSString *title = [[entry title] stringValue];
                [playlist addObject:NSLocalizedString(title, @"playlistTitle")];
                NSLog(@"%@", [[entry content] sourceURI]);
                [videoPlaylistURLArray addObject:[[entry content] sourceURI]];
            }
        }
        if (!error) {
            [self addMenuButtons];
            [self setPlaylistIndex:0];
            [SVProgressHUD showSuccessWithStatus:@"Success!"];
        }else{
            [SVProgressHUD showErrorWithStatus:@"Failed Fetching Playlist."];
        }
    }
    
    - (void)request:(GDataServiceTicket *)ticket finishedLoadingYoutubeVideosPerPlaylistWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
    {
        self.feed = (GDataFeedYouTubeVideo *)aFeed;
        [self.youtubeTable reloadData];
    }