forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhitelistScraper.m
48 lines (41 loc) · 1.47 KB
/
WhitelistScraper.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
//
// WhitelistScraper.m
// SelfControl
//
// Created by Charles Stigler on 10/7/14.
//
//
#import "WhitelistScraper.h"
@implementation WhitelistScraper
+ (NSSet*)relatedDomains:(NSString*)domain; {
NSURL* rootURL = [NSURL URLWithString: [NSString stringWithFormat: @"http://%@", domain]];
if (!rootURL) {
return nil;;
}
// stale data is OK
NSURLRequest* request = [NSURLRequest requestWithURL: rootURL cachePolicy: NSURLRequestReturnCacheDataElseLoad timeoutInterval: 5];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
if (!response) {
return nil;
}
NSString* html = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
if (!html) {
return nil;
}
NSDataDetector* dataDetector = [[NSDataDetector alloc] initWithTypes: NSTextCheckingTypeLink error: nil];
NSCountedSet* relatedDomains = [NSCountedSet set];
[dataDetector enumerateMatchesInString: html
options: kNilOptions
range: NSMakeRange(0, [html length])
usingBlock:^(NSTextCheckingResult* result, NSMatchingFlags flags, BOOL* stop) {
if (([result.URL.scheme isEqualToString: @"http"] || [result.URL.scheme isEqualToString: @"https"])
&& [result.URL.host length]
&& ![result.URL.host isEqualToString: rootURL.host]) {
[relatedDomains addObject: result.URL.host];
}
}];
return relatedDomains;
}
@end