Last active
December 27, 2015 17:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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