2023-03-16 20:35:54 +00:00
|
|
|
package plan;
|
2022-05-19 15:45:26 +00:00
|
|
|
|
2022-08-08 13:18:04 +00:00
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import lombok.ToString;
|
2023-03-04 12:19:01 +00:00
|
|
|
import nu.marginalia.crawling.io.CrawledDomainReader;
|
|
|
|
import nu.marginalia.crawling.model.CrawledDomain;
|
2023-07-24 13:25:09 +00:00
|
|
|
import nu.marginalia.crawling.model.SerializableCrawlData;
|
2023-03-16 20:35:54 +00:00
|
|
|
import nu.marginalia.crawling.model.spec.CrawlerSpecificationLoader;
|
|
|
|
import nu.marginalia.crawling.model.spec.CrawlingSpecification;
|
|
|
|
import nu.marginalia.process.log.WorkLog;
|
2023-03-04 12:19:01 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2022-08-08 13:18:04 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2023-06-27 08:56:21 +00:00
|
|
|
import java.nio.file.Files;
|
2022-05-19 15:45:26 +00:00
|
|
|
import java.nio.file.Path;
|
2022-08-10 13:04:25 +00:00
|
|
|
import java.util.Iterator;
|
2022-09-02 07:34:20 +00:00
|
|
|
import java.util.function.Predicate;
|
2023-07-06 18:18:09 +00:00
|
|
|
import java.util.Optional;
|
2022-05-19 15:45:26 +00:00
|
|
|
|
|
|
|
@AllArgsConstructor @NoArgsConstructor @ToString
|
2023-03-12 10:42:07 +00:00
|
|
|
public class CrawlPlan {
|
2023-03-04 12:19:01 +00:00
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
2022-05-19 15:45:26 +00:00
|
|
|
public String jobSpec;
|
|
|
|
public WorkDir crawl;
|
|
|
|
public WorkDir process;
|
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
private static String rootDirRewrite = System.getProperty("crawl.rootDirRewrite");
|
|
|
|
|
2022-05-19 15:45:26 +00:00
|
|
|
public Path getJobSpec() {
|
2023-03-30 13:41:07 +00:00
|
|
|
return Path.of(rewrite(jobSpec));
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor @NoArgsConstructor @ToString
|
|
|
|
public static class WorkDir {
|
|
|
|
public String dir;
|
|
|
|
public String logName;
|
|
|
|
|
|
|
|
public Path getDir() {
|
2023-03-04 12:19:01 +00:00
|
|
|
return Path.of(rewrite(dir));
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
public Path getLogFile() {
|
2023-03-04 12:19:01 +00:00
|
|
|
return Path.of(rewrite(dir)).resolve(logName);
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
private static String rewrite(String dir) {
|
|
|
|
if (rootDirRewrite == null) {
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
String[] parts = rootDirRewrite.split(":");
|
|
|
|
|
2023-03-30 13:41:07 +00:00
|
|
|
return dir.replaceFirst(parts[0], parts[1]);
|
2023-03-04 12:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:57:58 +00:00
|
|
|
public Path getCrawledFilePath(String fileName) {
|
|
|
|
String sp1 = fileName.substring(0, 2);
|
|
|
|
String sp2 = fileName.substring(2, 4);
|
|
|
|
return crawl.getDir().resolve(sp1).resolve(sp2).resolve(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Path getProcessedFilePath(String fileName) {
|
|
|
|
String sp1 = fileName.substring(0, 2);
|
|
|
|
String sp2 = fileName.substring(2, 4);
|
|
|
|
return process.getDir().resolve(sp1).resolve(sp2).resolve(fileName);
|
|
|
|
}
|
2022-08-08 13:18:04 +00:00
|
|
|
|
2022-08-10 15:03:58 +00:00
|
|
|
public WorkLog createCrawlWorkLog() throws IOException {
|
|
|
|
return new WorkLog(crawl.getLogFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
public WorkLog createProcessWorkLog() throws IOException {
|
|
|
|
return new WorkLog(process.getLogFile());
|
|
|
|
}
|
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
public Iterable<CrawlingSpecification> crawlingSpecificationIterable() {
|
|
|
|
return CrawlerSpecificationLoader.asIterable(getJobSpec());
|
2022-08-10 13:04:25 +00:00
|
|
|
}
|
2023-07-11 12:46:21 +00:00
|
|
|
|
|
|
|
public int countCrawledDomains() {
|
2023-07-12 15:47:36 +00:00
|
|
|
int count = 0;
|
|
|
|
for (var ignored : WorkLog.iterable(crawl.getLogFile())) {
|
|
|
|
count++;
|
2023-07-11 12:46:21 +00:00
|
|
|
}
|
2023-07-12 15:47:36 +00:00
|
|
|
return count;
|
2023-07-11 12:46:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
public Iterable<CrawledDomain> domainsIterable() {
|
2022-09-02 07:34:20 +00:00
|
|
|
final CrawledDomainReader reader = new CrawledDomainReader();
|
2022-08-10 13:04:25 +00:00
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
return WorkLog.iterableMap(crawl.getLogFile(),
|
|
|
|
entry -> {
|
|
|
|
var path = getCrawledFilePath(entry.path());
|
|
|
|
if (!Files.exists(path)) {
|
|
|
|
logger.warn("File not found: {}", path);
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
return reader.readOptionally(path);
|
|
|
|
});
|
2022-08-10 13:04:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
public Iterable<CrawledDomain> domainsIterable(Predicate<String> idPredicate) {
|
|
|
|
final CrawledDomainReader reader = new CrawledDomainReader();
|
2022-08-10 13:04:25 +00:00
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
return WorkLog.iterableMap(crawl.getLogFile(),
|
|
|
|
entry -> {
|
2023-07-17 19:20:31 +00:00
|
|
|
if (!idPredicate.test(entry.id())) {
|
2023-07-12 15:47:36 +00:00
|
|
|
return Optional.empty();
|
|
|
|
}
|
2022-08-10 13:04:25 +00:00
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
var path = getCrawledFilePath(entry.path());
|
2022-08-10 13:04:25 +00:00
|
|
|
|
2023-07-12 15:47:36 +00:00
|
|
|
if (!Files.exists(path)) {
|
|
|
|
logger.warn("File not found: {}", path);
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
return reader.readOptionally(path);
|
|
|
|
});
|
2022-08-08 13:18:04 +00:00
|
|
|
}
|
2023-07-24 13:25:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
public Iterable<Iterator<SerializableCrawlData>> crawlDataIterable(Predicate<String> idPredicate) {
|
|
|
|
final CrawledDomainReader reader = new CrawledDomainReader();
|
|
|
|
|
|
|
|
return WorkLog.iterableMap(crawl.getLogFile(),
|
|
|
|
entry -> {
|
|
|
|
if (!idPredicate.test(entry.id())) {
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
var path = getCrawledFilePath(entry.path());
|
|
|
|
|
|
|
|
if (!Files.exists(path)) {
|
|
|
|
logger.warn("File not found: {}", path);
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Optional.of(reader.createIterator(path));
|
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|