-
Notifications
You must be signed in to change notification settings - Fork 1
/
EYWebScraper.m
executable file
·138 lines (99 loc) · 3.53 KB
/
EYWebScraper.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// erkanyildiz
// 20180304-1945+0900
//
// EYWebScraper.m
#import "EYWebScraper.h"
NSErrorDomain const EYWebScraperErrorDomain = @"EYWebScraperErrorDomain";
@interface EYWebScraper ()
@property (nonatomic, copy) NSString* js;
@property (nonatomic, copy) UIWebView* web;
@property (nonatomic, copy) void (^completion)(NSString* result, NSError* error);
@property (nonatomic, strong) EYWebScraper* keeper;
@end
@implementation EYWebScraper
+ (void)scrape:(NSString *)URL usingGist:(NSString *)gist completion:(void (^)(NSString* result, NSError* error))completion
{
if (!URL.length || !gist.length || !completion)
return;
NSString* gistURL = [NSString stringWithFormat:@"https://gist.githubusercontent.com/%@/raw", gist];
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:gistURL] completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSString* js = [NSString.alloc initWithData:data encoding:NSUTF8StringEncoding];
onMainThread(^
{
if (error)
completion(nil, error);
else if (!js)
completion(nil, [EYWebScraper error:EYWebScraperErrorInvalidGistContent]);
else
[EYWebScraper scrape:URL usingJS:js completion:completion];
});
}] resume];
}
+ (void)scrape:(NSString *)URL usingJS:(NSString *)js completion:(void (^)(NSString* result, NSError* error))completion
{
if (!URL.length || !js.length || !completion)
return;
EYWebScraper* ws = EYWebScraper.new;
NSString* wrapped = [NSString stringWithFormat:@"function EYWebScraperWrapperFunction(){%@} EYWebScraperWrapperFunction();", js];
ws.js = wrapped;
ws.completion = completion;
ws.web = [UIWebView.alloc initWithFrame:CGRectZero];
ws.web.hidden = YES;
ws.web.delegate = ws;
[ws.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL]]];
ws.keeper = ws;
}
#pragma mark -
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (webView.isLoading)
return;
NSString* result = [webView stringByEvaluatingJavaScriptFromString:self.js];
onMainThread(^
{
if (!result.length)
self.completion(nil, [EYWebScraper error:EYWebScraperErrorJavaScriptEvaluationEmptyString]);
else
self.completion(result, nil);
});
self.keeper = nil;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
onMainThread(^
{
self.completion(nil, error);
});
self.keeper = nil;
}
#pragma mark -
+ (NSError *)error:(NSInteger)errorCode
{
NSMutableDictionary* userInfo = NSMutableDictionary.new;
NSString* const kEYWebScraperErrorDescriptionKey = @"description";
switch (errorCode)
{
case EYWebScraperErrorInvalidGistContent:
{
userInfo[kEYWebScraperErrorDescriptionKey] = @"Gist content is invalid.";
}break;
case EYWebScraperErrorJavaScriptEvaluationEmptyString:
{
userInfo[kEYWebScraperErrorDescriptionKey] = @"JavaScript evaluation result is empty string.";
}break;
default:
userInfo = nil;
break;
}
return [NSError errorWithDomain:EYWebScraperErrorDomain code:errorCode userInfo:userInfo.copy];
}
void onMainThread(void (^block)(void))
{
dispatch_async(dispatch_get_main_queue(), block);
}
@end