Pass the task to onSuccess & onError.

pull/1095/head
Sutra Zhou 2 years ago
parent 5f80e02abd
commit d2b2eed9df

@ -3,6 +3,7 @@ package us.codecraft.webmagic.downloader;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.selector.Html;
/**
@ -35,15 +36,26 @@ public abstract class AbstractDownloader implements Downloader {
return (Html) page.getHtml();
}
@Deprecated
protected void onSuccess(Request request) {
}
/**
* @since 0.7.6
*/
protected void onSuccess(Request request, Task task) {
this.onSuccess(request);
}
@Deprecated
protected void onError(Request request) {
this.onError(request, null);
}
protected void onError(Request request, Throwable e) {
/**
* @since 0.7.6
*/
protected void onError(Request request, Task task, Throwable e) {
this.onError(request);
}
}

@ -82,12 +82,12 @@ public class HttpClientDownloader extends AbstractDownloader {
try {
httpResponse = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext());
page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), httpResponse, task);
onSuccess(request);
onSuccess(request, task);
logger.info("downloading page success {}", request.getUrl());
return page;
} catch (IOException e) {
logger.warn("download page {} error", request.getUrl(), e);
onError(request, e);
onError(request, task, e);
return page;
} finally {
if (httpResponse != null) {

@ -98,9 +98,9 @@ public class PhantomJSDownloader extends AbstractDownloader {
page.setRequest(request);
page.setStatusCode(200);
}
onSuccess(request);
onSuccess(request, task);
} catch (Exception e) {
onError(request, e);
onError(request, task, e);
logger.warn("download page {} error", request.getUrl(), e);
}
return page;

@ -111,10 +111,10 @@ public class SeleniumDownloader extends AbstractDownloader implements Closeable
page.setHtml(new Html(content, request.getUrl()));
page.setUrl(new PlainText(request.getUrl()));
page.setRequest(request);
onSuccess(request);
onSuccess(request, task);
} catch (Exception e) {
logger.warn("download page {} error", request.getUrl(), e);
onError(request, e);
onError(request, task, e);
} finally {
if (webDriver != null) {
webDriverPool.returnToPool(webDriver);

Loading…
Cancel
Save