-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTAArticleContentFetch.m
More file actions
241 lines (209 loc) · 7.84 KB
/
UTAArticleContentFetch.m
File metadata and controls
241 lines (209 loc) · 7.84 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// UTAArticleContentFetch.m
// UTANews
//
// Created by David on 16/8/19.
// Copyright © 2016年 UTA. All rights reserved.
//
#import "UTAArticleContentFetch.h"
#import "API+Doome.h"
#import "API+Sundaily.h"
#import "API+Yesdaily.h"
#import "API+Movie.h"
#import "YYCache.h"
#import "YYDiskCache.h"
@import UIKit;
@interface UTAArticleContentFetch () <UIWebViewDelegate>
@property (nonatomic, copy) CompletionBlock completionBlock;
@property (nonatomic, copy) NSString *link;
@property (nonatomic, assign) UTAHost host;
@end
@implementation UTAArticleContentFetch {
UIWebView *_webView;
YYCache *_cacheDoome;
YYCache *_cacheSundaily;
YYCache *_cacheYesdaily;
// 下载码的缓存
YYCache *_cacheDownloadCode;
}
- (instancetype)init {
self = [super init];
if (self) {
_cacheDoome = [YYCache cacheWithName:@"com.doome.article"];
_cacheDoome.diskCache.ageLimit = 7*24*3600;
_cacheSundaily = [YYCache cacheWithName:@"com.sundaily.article"];
_cacheSundaily.diskCache.ageLimit = 7*24*3600;
_cacheYesdaily = [YYCache cacheWithName:@"com.yesdaily.article"];
_cacheYesdaily.diskCache.ageLimit = 7*24*3600;
_cacheDownloadCode = [YYCache cacheWithName:@"com.1115hd.downloadcode"];
_cacheDownloadCode.diskCache.ageLimit = 3600;
}
return self;
}
+ (instancetype)shareInstance {
static UTAArticleContentFetch *fetch;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
fetch = [[UTAArticleContentFetch alloc] init];
});
return fetch;
}
/**
获取文章内容,默认请求超时时间为30s
@param artId artId description
@param host host description
@param completionBlock completionBlock description
*/
- (void)getContentWithArtId:(NSInteger)artId host:(UTAHost)host completionBlock:(CompletionBlock)completionBlock {
[self getContentWithArtId:artId host:host completionBlock:completionBlock timeout:30];
}
/**
获取文章内容,默认请求超时时间为30s
@param artId artId description
@param host host description
@param completionBlock completionBlock description
@param timeout timeout description
*/
- (void)getContentWithArtId:(NSInteger)artId host:(UTAHost)host completionBlock:(CompletionBlock)completionBlock timeout:(NSTimeInterval)timeout {
if (_webView) {
_webView.delegate = nil;
[_webView loadHTMLString:@"" baseURL:nil];
_webView = nil;
}
if (_completionBlock) {
_completionBlock(@"", nil);
_completionBlock = nil;
}
_host = host;
YYCache *cache = nil;
NSString *api = @"";
switch (_host) {
case UTAHostDoome:
api = API_DMArticleDetail;
cache = _cacheDoome;
break;
case UTAHostSundaily:
api = API_SDArticleDetail;
cache = _cacheSundaily;
break;
case UTAHostYesdaily:
api = API_YDArticleDetail;
cache = _cacheYesdaily;
break;
default:
break;
}
NSString *link = [api stringByAppendingString:[@(artId) stringValue]];
self.completionBlock = completionBlock;
self.link = link;
NSString *content = (id)[cache objectForKey:link];
if (content) {
_completionBlock(content, [NSURL URLWithString:link]);
_completionBlock = nil;
}
else {
_webView = [UIWebView new];
_webView.delegate = self;
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:link]];
req.timeoutInterval = timeout;
[_webView loadRequest:req];
_webView.delegate = self;
}
}
/**
通过下载码获取下载信息
@param link 下载码
@param completionBlock completionBlock description
@param timeout timeout description
*/
- (void)getConentWithDownloadLink:(NSString *)link completionBlock:(CompletionBlock)completionBlock timeout:(NSTimeInterval)timeout {
if (_webView) {
_webView.delegate = nil;
[_webView loadHTMLString:@"" baseURL:nil];
_webView = nil;
}
_host = UTAHostMovie;
self.completionBlock = completionBlock;
self.link = link;
NSString *content = (id)[_cacheDownloadCode objectForKey:link];
if (content) {
_completionBlock(content, [NSURL URLWithString:link]);
}
else {
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:link]];
req.timeoutInterval = timeout;
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRange begin = [string rangeOfString:@"<br />"];
NSRange end = [string rangeOfString:@"</div>"];
string = [string substringWithRange:NSMakeRange(begin.location+begin.length, end.location-(begin.location+begin.length))];
[_cacheDownloadCode setObject:string forKey:self.link];
dispatch_async(dispatch_get_main_queue(), ^{
if (_completionBlock) {
_completionBlock(string, [NSURL URLWithString:self.link]);
_completionBlock = nil;
}
});
}];
[task resume];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *content;
if (_host==UTAHostMovie) {
content = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('div')[0].innerHTML"];
NSRange begin = [content rangeOfString:@"{"];
content = [content substringFromIndex:begin.location];
}
else {
NSString *js = @"function rmAllStyle(){removeChildrenAttrs(document.body);} function removeChildrenAttrs(nd) {nd.removeAttribute('style');nd.removeAttribute('class');for (var i=0; i<nd.children.length;i++) {removeChildrenAttrs(nd.children[i]);}} rmAllStyle();";
[_webView stringByEvaluatingJavaScriptFromString:js];
content = [_webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
if (content.length) {
YYCache *cache = nil;
switch (_host) {
case UTAHostDoome:
cache = _cacheDoome;
break;
case UTAHostSundaily:
cache = _cacheSundaily;
break;
case UTAHostYesdaily:
cache = _cacheYesdaily;
break;
default:
break;
}
[cache setObject:content forKey:self.link];
}
}
if (self.completionBlock) {
self.completionBlock(content?:@"", _webView.request.URL);
_completionBlock = nil;
}
_webView.delegate = nil;
[_webView loadHTMLString:@"" baseURL:nil];
_webView = nil;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
switch (error.code) {
case NSURLErrorTimedOut: {
/*
如果网页获取不到主机地址,说明从来没有加载成功过,可以认为是当前网页超时
如果超时的主机地址和当前网页的主机地址相同,则认为是网页超时
*/
NSURL *urlError = error.userInfo[NSURLErrorFailingURLErrorKey];
if (webView.request.URL.host && ![urlError.host isEqualToString:webView.request.URL.host]) break;
}
case NSURLErrorNotConnectedToInternet: {
if (self.completionBlock) {
self.completionBlock(error.localizedFailureReason?:@"", _webView.request.URL);
_completionBlock = nil;
}
} break;
default:
break;
}
}
@end