Jude Michael Murphy
Jude Michael Murphy

Reputation: 1198

Extract YouTube Video Link Using YouTube API

I have developed an app that loads a user's videos and when they click on the cell in the tableview it goes to another viewcontroller that holds a bunch of different information.

I'm trying to extract the actual YouTube link from a specific video. I print out the links I get and they come in this form.

https://www.youtube.com/v/Xf5pXlZJr1U?version=3&f=user_uploads&app=youtube_gdata

But I want it to be in this form.

http://www.youtube.com/watch?v=Xf5pXlZJr1U

Reason being that I want people to share the link via social media, but it comes out weird when it's in the first format.

Is there any way to easily convert the links?


SAMPLE CODE AND ANSWER EDIT

GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
NSString *tempURL = [flashContent URLString];

NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaThumbnails];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage *image = [UIImage imageWithData:data];

GDataYouTubeMediaGroup *mediaGroup = [(GDataEntryYouTubeVideo *)entry2 mediaGroup];
GDataMediaDescription *desc2 = [mediaGroup mediaDescription];
NSString *mystring = [desc2 stringValue];

NSArray *listItems = [tempURL componentsSeparatedByString:@"/"];
NSString *NewURL = @"";

NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:0]];
NewURL = [NewURL stringByAppendingString:@"//"];
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:2]];
NewURL = [NewURL stringByAppendingString:@"/"];
NewURL = [NewURL stringByAppendingString:@"watch"];
NewURL = [NewURL stringByAppendingString:@"?v="];

NSArray *listItems2 = [[listItems objectAtIndex:4] componentsSeparatedByString:@"?"];    
NewURL = [NewURL stringByAppendingString:[listItems2 objectAtIndex:0]];

Upvotes: 0

Views: 315

Answers (1)

nsgulliver
nsgulliver

Reputation: 12671

If you take a look at NSString docs then you will find number of useful methods for yourself to use, e.g componentsSeparatedByString splitting the string in two different chunks depending on some particular character & stringByAppendingString for appending string to an existing string. It is not difficult to use these methods, just use according to your needs.

Upvotes: 1

Related Questions