Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting youtube video links programmatically in Objective-C

I need to download and save videos from video sites like youtube. I want to save them to the video library of the iPhone. How can I detect the videos and save them to library? I already checked out some source codes available. This is what i have done in the download action, but is not working.

- (IBAction)download {
    [downloadButton setEnabled:NO];

    [webView setUserInteractionEnabled:NO];

    UIUserInterfaceIdiom userInterfaceIdiom = [UIDevice currentDevice].userInterfaceIdiom;

    NSString *getURL = @"";

    if (userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        getURL = [webView stringByEvaluatingJavaScriptFromString:@"function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();"];
    } else {
        getURL = [webView stringByEvaluatingJavaScriptFromString:@"function getURL() {var bh = document.getElementsByClassName('bh'); if (bh.length) {return bh[0].getAttribute('src');} else {var zq = document.getElementsByClassName('zq')[0]; return zq.getAttribute('src');}} getURL();"];
    }

    NSString *getTitle = [webView stringByEvaluatingJavaScriptFromString:@"function getTitle() {var jm = document.getElementsByClassName('jm'); if (jm.length) {return jm[0].innerHTML;} else {var lp = document.getElementsByClassName('lp')[0]; return lp.childNodes[0].innerHTML;}} getTitle();"];

    NSString *getTitleFromChannel = [webView stringByEvaluatingJavaScriptFromString:@"function getTitleFromChannel() {var video_title = document.getElementById('video_title'); return video_title.childNodes[0].innerHTML;} getTitleFromChannel();"];

    NSLog(@"%@, %@, %@", getURL, getTitle, getTitleFromChannel);

    [webView setUserInteractionEnabled:YES];

    NSArray *components = [getTitle componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];
    getTitle = [components componentsJoinedByString:@" "];

    if ([getURL length] > 0) {
        if ([getTitle length] > 0) {
            videoTitle = [getTitle retain];

            bar = [[UIDownloadBar alloc] initWithURL:[NSURL URLWithString:getURL]
                                    progressBarFrame:CGRectMake(85.0, 17.0, 150.0, 11.0)
                                             timeout:15
                                            delegate:self];

            [bar setProgressViewStyle:UIProgressViewStyleBar];

            [toolbar addSubview:bar];
        } else {
            NSArray *components = [getTitleFromChannel componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];
            getTitleFromChannel = [components componentsJoinedByString:@" "];

            if ([getTitleFromChannel length] > 0) {
                videoTitle = [getTitleFromChannel retain];

                bar = [[UIDownloadBar alloc] initWithURL:[NSURL URLWithString:getURL]
                                        progressBarFrame:CGRectMake(85.0, 17.0, 150.0, 11.0)
                                                 timeout:15
                                                delegate:self];

                [bar setProgressViewStyle:UIProgressViewStyleBar];

                [toolbar addSubview:bar];
            } else {
                //NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyTube" message:@"Couldn't get video title." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

                [alertView show];
                [alertView release];

                [downloadButton setEnabled:YES];
            }
        }
    } else {
        //NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyTube" message:@"Couldn't get MP4 URL." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];
        [alertView release];

        [downloadButton setEnabled:YES];
    }
}
like image 960
iOS Developer Avatar asked Jul 18 '12 09:07

iOS Developer


2 Answers

This will grab the video ids if you are consistently expecting they to be in the < object > format. Regex will obviously need to be changed if it's in an iframe.

NSError *error = nil;
NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:@"<object .*name=\"movie\" value=\"[a-zA-z:\\/]*.com\\/\\w*\\/([A-Za-z0-9\\_-]*).*<\\/object>"
                                          options:NSRegularExpressionCaseInsensitive
                                            error:&error];

[regex enumerateMatchesInString:pageSource options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    NSString *videoID = [pageSource substringWithRange:[match rangeAtIndex:1]];        
}];
like image 192
endy Avatar answered Sep 24 '22 00:09

endy


You can save downloaded videos either to photos album or to the app directory itself.For downloading videos u have to generate a downloadble link of the corresponding video.In the case of downloading videos from the site youtube,refer this source code https://github.com/comonitos/youtube_video. Some sites are download restricted ones that may not be possible.In the case of all other sites by generating the downloadable url,you can make it work

like image 25
iOS Developer Avatar answered Sep 23 '22 00:09

iOS Developer