Skip to content

Instantly share code, notes, and snippets.

@waitonza
Last active December 23, 2015 16:18
Show Gist options
  • Save waitonza/6660806 to your computer and use it in GitHub Desktop.
Save waitonza/6660806 to your computer and use it in GitHub Desktop.
โค้ด Objective-C ดาวน์โหลด คลิปจาก Youtube ของ Session (Barcamp Bangkhen #4)
....
#import "HCYoutubeParser.h"
#import "SVProgressHUD.h"
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Youtube Browser";
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(homePressed:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Capture" style:UIBarButtonItemStyleBordered target:self action:@selector(capturePressed:)];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com"]]];
}
- (IBAction)homePressed:(id)sender {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com"]]];
}
- (IBAction)capturePressed:(id)sender {
// NSLog(@"%s", __PRETTY_FUNCTION__);
NSString *currentURL = [self.webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
NSLog(@"Current URL : %@", currentURL);
NSString *regEx = [NSString stringWithFormat:@"http://m.youtube.com/watch.?v"];
NSRange range = [currentURL rangeOfString:regEx options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
[SVProgressHUD show];
NSURL *youtubeURL = [NSURL URLWithString:currentURL];
[HCYoutubeParser h264videosWithYoutubeURL:youtubeURL completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
self.videos = videoDictionary;
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Download" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *res in [self.videos allKeys]) {
[self.actionSheet addButtonWithTitle:res];
}
[self.actionSheet addButtonWithTitle:@"Cancel"];
[self.actionSheet showFromTabBar:[[super tabBarController] tabBar]];
});
}];
[HCYoutubeParser detailsForYouTubeURL:youtubeURL completeBlock:^(NSDictionary *details, NSError *error) {
self.videoDetails = details;
//NSLog(@"Details : %@", details);
}];
[HCYoutubeParser thumbnailForYoutubeURL:youtubeURL thumbnailSize:YouTubeThumbnailDefaultHighQuality completeBlock:^(UIImage *image, NSError *error) {
self.thumbImage = image;
}];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!!"
message:@"It is not a youtube video page!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
}
...
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != self.videos.count) {
NSArray *allKeys = [self.videos allKeys];
NSString *downloadURL = [self.videos objectForKey:[allKeys objectAtIndex:buttonIndex]];
NSLog(@"Download %@", downloadURL);
UITabBarController *tabBar = [super tabBarController];
NSArray *allVC = [tabBar viewControllers];
UINavigationController *downloadNavVC = [allVC objectAtIndex:1];
DownloadViewController *downloadVC = [[downloadNavVC viewControllers] objectAtIndex:0];
[downloadVC startDownloadingVideo:downloadURL withThumbImage:self.thumbImage andFileName:self.videoDetails[@"entry"][@"title"][@"$t"]];
[tabBar setSelectedIndex:1];
}
}
...
#import "AFNetworking.h"
...
- (void)startingDownload {
NSURL *url = [NSURL URLWithString:self.videoObj.downloadLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *op;
op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *documentsSearchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentsSearchPaths count] == 0 ? nil : [documentsSearchPaths objectAtIndex:0];
NSString *targetFilename = [self.videoObj.fileName stringByAppendingString:@".mp4"];
NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:targetFilename];
NSLog(@"Save to path : %@", targetPath);
op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.videoObj.isDownloaded = YES;
self.videoObj.isDownloading = YES;
self.videoObj.filePath = targetPath;
self.videoObj.fileSize = self.fileSize;
self.progressView.hidden = YES;
self.statusLabel.text = [NSString stringWithFormat:@"File Size : %.2f MB", self.videoObj.fileSize];
[[VideoObjectManager sharedManager] updateDownloadContent];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
self.statusLabel.text = @"Download Failed";
}];
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
if (totalBytesRead > 0) {
self.progressView.progress = (float)totalBytesRead/ (float)totalBytesExpectedToRead;
float totalReadMB = (float)totalBytesRead/1048576;
float totalExpectedMB = (float)totalBytesExpectedToRead/1048576;
self.fileSize = (float)totalExpectedMB;
self.statusLabel.text = [NSString stringWithFormat:@"Downloading %.2f MB of %.2f MB", totalReadMB, totalExpectedMB];
}
}];
[op start];
}
platform :ios, '6.0'
pod 'AFNetworking', '1.3.2'
pod 'SVProgressHUD'
pod 'HCYoutubeParser'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment