You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
822 B

package strategy;
import model.Paper;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public abstract class AbstractCrawlerStrategy implements CrawlerStrategy {
protected Random random = new Random();
@Override
public List<Paper> crawl(String url, int count) throws Exception {
List<Paper> papers = new ArrayList<>();
papers.addAll(fetchPapers(url, count));
return papers;
}
protected abstract List<Paper> fetchPapers(String url, int count) throws Exception;
protected void addDelay(int minMs, int maxMs) {
try {
int delay = minMs + random.nextInt(maxMs - minMs + 1);
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}