Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable inmediate redirection #433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,20 +448,17 @@ private void processPage(WebURL curURL) throws IOException, InterruptedException
onRedirectedStatusCode(page);

if (myController.getConfig().isFollowRedirects()) {
if (curURL.isFollowRedirectsInmediatly() && curURL.getMaxInmediateRedirects() > 0) {
followRedirectInmediatly(curURL, movedToUrl);
return;
}
int newDocId = docIdServer.getDocId(movedToUrl);
if (newDocId > 0) {
logger.debug("Redirect page: {} is already seen", curURL);
return;
}

WebURL webURL = new WebURL();
webURL.setTldList(myController.getTldList());
webURL.setURL(movedToUrl);
webURL.setParentDocid(curURL.getParentDocid());
webURL.setParentUrl(curURL.getParentUrl());
webURL.setDepth(curURL.getDepth());
webURL.setDocid(-1);
webURL.setAnchor(curURL.getAnchor());
WebURL webURL = createRedirectedWebURL(curURL, movedToUrl);
if (shouldVisit(page, webURL)) {
if (!shouldFollowLinksIn(webURL) || robotstxtServer.allows(webURL)) {
webURL.setDocid(docIdServer.getNewDocID(movedToUrl));
Expand Down Expand Up @@ -584,6 +581,50 @@ private void processPage(WebURL curURL) throws IOException, InterruptedException
}
}

/**
* Creates a new WebURL based on provided WebURL data.
*
* Subclases may use aditional parameters or use subclasses of WebURL.
*
* @param curURL
* @param movedToUrl
* @return
*/
protected WebURL createRedirectedWebURL(WebURL curURL, String movedToUrl) {
WebURL webURL = new WebURL();
webURL.setTldList(myController.getTldList());
webURL.setURL(movedToUrl);
webURL.setParentDocid(curURL.getParentDocid());
webURL.setParentUrl(curURL.getParentUrl());
webURL.setDepth(curURL.getDepth());
webURL.setAnchor(curURL.getAnchor());
webURL.setDocid(-1);
return webURL;
}

/**
* Processes the redirected page without scheduling it, even if it was already seen.
*
* @param curURL
* @param movedToUrl
* @throws IOException
* @throws InterruptedException
* @throws ParseException
*/
protected void followRedirectInmediatly(WebURL curURL, String movedToUrl)
throws IOException, InterruptedException, ParseException {
WebURL webURL = createRedirectedWebURL(curURL, movedToUrl);
webURL.setFollowRedirectsInmediatly(true);
int newDocId = docIdServer.getDocId(movedToUrl);
if (newDocId < 0) {
// Repeated visits are accepted, however, no new docIds will be generated.
newDocId = docIdServer.getNewDocID(movedToUrl);
}
webURL.setDocid(newDocId);
webURL.setMaxInmediateRedirects((short)(curURL.getMaxInmediateRedirects() - 1));
this.processPage(webURL);
}

public Thread getThread() {
return myThread;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public WebURL entryToObject(TupleInput input) {
webURL.setDepth(input.readShort());
webURL.setPriority(input.readByte());
webURL.setAnchor(input.readString());
webURL.setFollowRedirectsInmediatly(input.readBoolean());
webURL.setMaxInmediateRedirects(input.readShort());
return webURL;
}

Expand All @@ -50,5 +52,7 @@ public void objectToEntry(WebURL url, TupleOutput output) {
output.writeShort(url.getDepth());
output.writeByte(url.getPriority());
output.writeString(url.getAnchor());
output.writeBoolean(url.isFollowRedirectsInmediatly());
output.writeShort(url.getMaxInmediateRedirects());
}
}
19 changes: 19 additions & 0 deletions crawler4j/src/main/java/edu/uci/ics/crawler4j/url/WebURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class WebURL implements Serializable {
private String tag;
private Map<String, String> attributes;
private TLDList tldList;
private boolean followRedirectsInmediatly = false;
private short maxInmediateRedirects = 10;

/**
* Set the TLDList if you want {@linkplain #getDomain()} and
Expand Down Expand Up @@ -249,6 +251,22 @@ public String getAttribute(String name) {
return attributes.getOrDefault(name, "");
}

public boolean isFollowRedirectsInmediatly() {
return followRedirectsInmediatly;
}

public void setFollowRedirectsInmediatly(boolean followRedirectsInmediatly) {
this.followRedirectsInmediatly = followRedirectsInmediatly;
}

public short getMaxInmediateRedirects() {
return maxInmediateRedirects;
}

public void setMaxInmediateRedirects(short maxInmediateRedirects) {
this.maxInmediateRedirects = maxInmediateRedirects;
}

@Override
public int hashCode() {
return url.hashCode();
Expand All @@ -272,4 +290,5 @@ public boolean equals(Object o) {
public String toString() {
return url;
}

}