forked from hac/googlebookdownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleBooksAPI.m
49 lines (38 loc) · 1.71 KB
/
GoogleBooksAPI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#import "GoogleBooksAPI.h"
#import <RegexKit/RegexKit.h>
@implementation GoogleBooksAPI
#pragma mark -
#pragma mark HTML Scraping
+ (BOOL)overviewPageExistsForBookWithId:(NSString *)bookId
// Check whether the book exists.
{
NSString *overviewPath = [NSString stringWithFormat:@"http://books.google.com/books?id=%@", bookId];
NSString *overviewHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:overviewPath]];
return ([overviewHTML length] > 0);
}
+ (NSString *)titleForBookWithId:(NSString *)bookId
// Scrape a book's preview page for its title.
{
NSString *bookInfoPath = [NSString stringWithFormat:@"http://books.google.com/books?id=%@&printsec=frontcover", bookId];
NSString *bookInfoHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:bookInfoPath]];
// Scrape the page for the book title.
NSString *openHeadingTag = @"<meta name=\"title\" content=\"";
NSString *closeHeadingTag = @"\"/>";
NSRange headingRange = [bookInfoHTML rangeOfString:openHeadingTag];
if (headingRange.location != NSNotFound)
{
bookInfoHTML = [bookInfoHTML substringWithRange:NSMakeRange(headingRange.location+[openHeadingTag length], 1000)];
headingRange = [bookInfoHTML rangeOfString:closeHeadingTag];
if (headingRange.location != NSNotFound)
{
bookInfoHTML = [bookInfoHTML substringToIndex:headingRange.location];
// Strip any HTML tags that sneaked into the heading:
RKRegex *htmlTagPattern = [RKRegex regexWithRegexString:@"<(.|\n)*?>" options:RKCompileCaseless];
bookInfoHTML = [bookInfoHTML stringByMatching:htmlTagPattern replace:RKReplaceAll withReferenceString:@""];
return bookInfoHTML;
}
}
// This method will fail in the future when/if the HTML of Google Books changes.
return nil;
}
@end