Skip to content

Instantly share code, notes, and snippets.

@dbgrandi
Last active December 27, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbgrandi/7360214 to your computer and use it in GitHub Desktop.
Save dbgrandi/7360214 to your computer and use it in GitHub Desktop.
Here's a version that parses out the files and directories using Hpple.
#import "AFNetworking.h"
#import "TFHpple.h"
NSString *baseURLString = @"http://gd2.mlb.com/components/game/mlb/year_2013/month_06/day_01/gid_2013_06_01_wasmlb_atlmlb_1/";
NSMutableArray *files = [@[] mutableCopy];
NSMutableArray *dirs = [@[] mutableCopy];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:baseURLString]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
TFHpple *html = [TFHpple hppleWithHTMLData:responseObject];
NSArray *entities = [html searchWithXPathQuery:@"//a"];
//skip the first a tag (it's the link to parent dir)
for (int i=1; i<[entities count]; i++) {
TFHppleElement *element = entities[i];
NSString *href = [element objectForKey:@"href"];
NSString *fullUrl = [NSString stringWithFormat:@"%@%@", baseURLString, href];
if ([fullUrl hasSuffix:@"/"]) {
[dirs addObject:fullUrl];
NSLog(@"directory = %@", fullUrl);
} else {
[files addObject:fullUrl];
NSLog(@"file = %@", fullUrl);
}
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"failure");
NSLog(@"%@", error.localizedDescription);
}];
[operation start];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment