Skip to content

Commit

Permalink
將網路錯誤或是解析錯誤兩種狀況分開來, thanks @Splendent, fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
DaidoujiChen committed Nov 18, 2014
1 parent d153cf9 commit 33ccc30
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 97 deletions.
24 changes: 16 additions & 8 deletions e-Hentai/HentaiDownloadCenter/HentaiDownloadBookOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,22 @@ - (void)checkEndOfFile {
@weakify(self);
[HentaiParser requestImagesAtURL:self.hentaiInfo[@"url"] atIndex:self.hentaiIndex completion: ^(HentaiParserStatus status, NSArray *images) {
@strongify(self);
if (status && ![self isCancelled] && [images count]) {
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
[self waitingOnDownloadFinish];
}
else {
[UIAlertView hentai_alertViewWithTitle:self.hentaiInfo[@"title"] message:@"被移除囉~所以無法下載~" cancelButtonTitle:@"好~ O3O"];
[self hentaiFinish];
if (![self isCancelled]) {
switch (status) {
case HentaiParserStatusSuccess:
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
[self waitingOnDownloadFinish];
break;
case HentaiParserStatusNetworkFail:
[UIAlertView hentai_alertViewWithTitle:self.hentaiInfo[@"title"] message:@"網路失敗~所以無法下載~" cancelButtonTitle:@"好~ O3O"];
[self hentaiFinish];
break;
case HentaiParserStatusParseFail:
[UIAlertView hentai_alertViewWithTitle:self.hentaiInfo[@"title"] message:@"被移除囉~所以無法下載~" cancelButtonTitle:@"好~ O3O"];
[self hentaiFinish];
break;
}
}
}];
}
Expand Down
3 changes: 2 additions & 1 deletion e-Hentai/HentaiParser/HentaiParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#import <Foundation/Foundation.h>

typedef enum {
HentaiParserStatusFail,
HentaiParserStatusNetworkFail = -1,
HentaiParserStatusParseFail,
HentaiParserStatusSuccess
} HentaiParserStatus;

Expand Down
150 changes: 87 additions & 63 deletions e-Hentai/HentaiParser/HentaiParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,56 @@ + (void)requestListAtFilterUrl:(NSString *)urlString completion:(void (^)(Hentai
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[self defaultOperationQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusFail, nil);
completion(HentaiParserStatusNetworkFail, nil);
});
}
else {
//這段是從 e hentai 的網頁 parse 列表
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *photoURL = [xpathParser searchWithXPathQuery:@"//div [@class='it5']//a"];

NSMutableArray *returnArray = [NSMutableArray array];
NSMutableArray *urlStringArray = [NSMutableArray array];

for (TFHppleElement * eachTitleWithURL in photoURL) {
[urlStringArray addObject:[eachTitleWithURL attributes][@"href"]];
[returnArray addObject:[NSMutableDictionary dictionaryWithDictionary:@{ @"url": [eachTitleWithURL attributes][@"href"] }]];
}

//這段是從 e hentai 的 api 抓資料
[self requestGDataAPIWithURLStrings:urlStringArray completion: ^(HentaiParserStatus status, NSArray *gMetaData) {
if (status) {
for (NSUInteger i = 0; i < [gMetaData count]; i++) {
NSMutableDictionary *eachDictionary = returnArray[i];
NSDictionary *metaData = gMetaData[i];
eachDictionary[@"thumb"] = metaData[@"thumb"];
eachDictionary[@"title"] = metaData[@"title"];
eachDictionary[@"title_jpn"] = metaData[@"title_jpn"];
eachDictionary[@"category"] = metaData[@"category"];
eachDictionary[@"uploader"] = metaData[@"uploader"];
eachDictionary[@"filecount"] = metaData[@"filecount"];
eachDictionary[@"filesize"] = [NSByteCountFormatter stringFromByteCount:[metaData[@"filesize"] floatValue] countStyle:NSByteCountFormatterCountStyleFile];
eachDictionary[@"rating"] = metaData[@"rating"];
eachDictionary[@"posted"] = [self dateStringFrom1970:[metaData[@"posted"] doubleValue]];
}
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusSuccess, returnArray);
});
}
else {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusFail, nil);
});
}
}];
//如果 parse 有結果, 才做 request api 的動作, 反之 callback HentaiParserStatusParseFail
if ([photoURL count]) {
NSMutableArray *returnArray = [NSMutableArray array];
NSMutableArray *urlStringArray = [NSMutableArray array];

for (TFHppleElement * eachTitleWithURL in photoURL) {
[urlStringArray addObject:[eachTitleWithURL attributes][@"href"]];
[returnArray addObject:[NSMutableDictionary dictionaryWithDictionary:@{ @"url": [eachTitleWithURL attributes][@"href"] }]];
}

//這段是從 e hentai 的 api 抓資料
[self requestGDataAPIWithURLStrings:urlStringArray completion: ^(HentaiParserStatus status, NSArray *gMetaData) {
if (status) {
for (NSUInteger i = 0; i < [gMetaData count]; i++) {
NSMutableDictionary *eachDictionary = returnArray[i];
NSDictionary *metaData = gMetaData[i];
eachDictionary[@"thumb"] = metaData[@"thumb"];
eachDictionary[@"title"] = metaData[@"title"];
eachDictionary[@"title_jpn"] = metaData[@"title_jpn"];
eachDictionary[@"category"] = metaData[@"category"];
eachDictionary[@"uploader"] = metaData[@"uploader"];
eachDictionary[@"filecount"] = metaData[@"filecount"];
eachDictionary[@"filesize"] = [NSByteCountFormatter stringFromByteCount:[metaData[@"filesize"] floatValue] countStyle:NSByteCountFormatterCountStyleFile];
eachDictionary[@"rating"] = metaData[@"rating"];
eachDictionary[@"posted"] = [self dateStringFrom1970:[metaData[@"posted"] doubleValue]];
}
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusSuccess, returnArray);
});
}
else {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusNetworkFail, nil);
});
}
}];
}
else {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusParseFail, nil);
});
}
}
}];
}
Expand All @@ -89,34 +97,43 @@ + (void)requestImagesAtURL:(NSString *)urlString atIndex:(NSUInteger)index compl
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:newURL] queue:[self defaultOperationQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusFail, nil);
completion(HentaiParserStatusNetworkFail, nil);
});
}
else {
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *pageURL = [xpathParser searchWithXPathQuery:@"//div [@class='gdtm']//a"];
NSMutableArray *returnArray = [NSMutableArray hentai_preAllocWithCapacity:[pageURL count]];

dispatch_queue_t hentaiQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_group_t hentaiGroup = dispatch_group_create();

for (NSUInteger i = 0; i < [pageURL count]; i++) {
TFHppleElement *e = pageURL[i];
dispatch_group_async(hentaiGroup, hentaiQueue, ^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self requestCurrentImage:[NSURL URLWithString:[e attributes][@"href"]] atIndex:i completion: ^(HentaiParserStatus status, NSString *imageString, NSUInteger index) {
if (status) {
returnArray[index] = imageString;
}
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
}
dispatch_group_wait(hentaiGroup, DISPATCH_TIME_FOREVER);
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusSuccess, returnArray);
});
//如果 parse 有結果, 才做 request api 的動作, 反之 callback HentaiParserStatusParseFail
if ([pageURL count]) {
NSMutableArray *returnArray = [NSMutableArray hentai_preAllocWithCapacity:[pageURL count]];

dispatch_queue_t hentaiQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_group_t hentaiGroup = dispatch_group_create();

for (NSUInteger i = 0; i < [pageURL count]; i++) {
TFHppleElement *e = pageURL[i];
dispatch_group_async(hentaiGroup, hentaiQueue, ^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self requestCurrentImage:[NSURL URLWithString:[e attributes][@"href"]] atIndex:i completion: ^(HentaiParserStatus status, NSString *imageString, NSUInteger index) {
if (status) {
returnArray[index] = imageString;
}
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
}
dispatch_group_wait(hentaiGroup, DISPATCH_TIME_FOREVER);
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusSuccess, returnArray);
});
}
else {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(HentaiParserStatusNetworkFail, nil);
});
}
}
}];
}
Expand Down Expand Up @@ -153,7 +170,7 @@ + (void)requestGDataAPIWithURLStrings:(NSArray *)urlStringArray completion:(void

[NSURLConnection sendAsynchronousRequest:request queue:[self defaultOperationQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
completion(HentaiParserStatusFail, nil);
completion(HentaiParserStatusNetworkFail, nil);
}
else {
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
Expand All @@ -178,15 +195,22 @@ + (NSMutableURLRequest *)makeJsonPostRequest:(NSDictionary *)jsonDictionary {
+ (void)requestCurrentImage:(NSURL *)url atIndex:(NSUInteger)index completion:(void (^)(HentaiParserStatus status, NSString *imageString, NSUInteger index))completion {
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[self hentaiOperationQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
completion(HentaiParserStatusFail, nil, -1);
completion(HentaiParserStatusNetworkFail, nil, -1);
}
else {
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *pageURL = [xpathParser searchWithXPathQuery:@"//div [@id='i3']//img"];
for (TFHppleElement * e in pageURL) {
completion(HentaiParserStatusSuccess, [e attributes][@"src"], index);
break;
}

//如果 parse 有結果, 才做 request api 的動作, 反之 callback HentaiParserStatusParseFail
if ([pageURL count]) {
for (TFHppleElement * e in pageURL) {
completion(HentaiParserStatusSuccess, [e attributes][@"src"], index);
break;
}
}
else {
completion(HentaiParserStatusParseFail, nil, -1);
}
}
}];
}
Expand Down
13 changes: 8 additions & 5 deletions e-Hentai/MainViewController/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,14 @@ - (void)reloadDatas {
//把 request 的判斷都放到這個 method 裡面來
- (void)loadList:(void (^)(BOOL successed, NSArray *listArray))completion {
[HentaiParser requestListAtFilterUrl:self.filterString completion: ^(HentaiParserStatus status, NSArray *listArray) {
if (status && [listArray count]) {
completion(YES, listArray);
}
else {
completion(NO, nil);
switch (status) {
case HentaiParserStatusSuccess:
completion(YES, listArray);
break;
case HentaiParserStatusParseFail:
case HentaiParserStatusNetworkFail:
completion(NO, nil);
break;
}
}];
}
Expand Down
56 changes: 36 additions & 20 deletions e-Hentai/PhotoViewController/PhotoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,18 @@ - (void)checkEndOfFile {
@weakify(self);
[HentaiParser requestImagesAtURL:self.hentaiURLString atIndex:self.hentaiIndex completion: ^(HentaiParserStatus status, NSArray *images) {
@strongify(self);
if (status && [images count]) {
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
[self waitingOnDownloadFinish];
}
else {
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:nil cancelButtonTitle:@"確定"];
switch (status) {
case HentaiParserStatusSuccess:
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
[self waitingOnDownloadFinish];
break;
case HentaiParserStatusNetworkFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"網路失敗" cancelButtonTitle:@"確定"];
break;
case HentaiParserStatusParseFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"解析失敗" cancelButtonTitle:@"確定"];
break;
}
}];
}
Expand Down Expand Up @@ -359,12 +364,17 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
@weakify(self);
[HentaiParser requestImagesAtURL:self.hentaiURLString atIndex:self.hentaiIndex completion: ^(HentaiParserStatus status, NSArray *images) {
@strongify(self);
if (status && [images count]) {
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
}
else {
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:nil cancelButtonTitle:@"確定"];
switch (status) {
case HentaiParserStatusSuccess:
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
break;
case HentaiParserStatusNetworkFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"網路失敗" cancelButtonTitle:@"確定"];
break;
case HentaiParserStatusParseFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"解析失敗" cancelButtonTitle:@"確定"];
break;
}
}];
}
Expand Down Expand Up @@ -439,13 +449,19 @@ - (void)viewDidLoad {
@weakify(self);
[HentaiParser requestImagesAtURL:self.hentaiURLString atIndex:self.hentaiIndex completion: ^(HentaiParserStatus status, NSArray *images) {
@strongify(self);
if (status && [images count]) {
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
}
else {
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:nil cancelButtonTitle:@"確定"];
[DaiInboxHUD hide];
switch (status) {
case HentaiParserStatusSuccess:
[self.hentaiImageURLs addObjectsFromArray:images];
[self preloadImages:images];
break;
case HentaiParserStatusNetworkFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"網路失敗" cancelButtonTitle:@"確定"];
[DaiInboxHUD hide];
break;
case HentaiParserStatusParseFail:
[UIAlertView hentai_alertViewWithTitle:@"讀取失敗囉" message:@"解析失敗" cancelButtonTitle:@"確定"];
[DaiInboxHUD hide];
break;
}
}];
}
Expand Down

0 comments on commit 33ccc30

Please sign in to comment.