Add handler for only new files parameter

This commit is contained in:
RatzzFatzz
2026-01-08 22:03:06 +01:00
parent 1165dd8380
commit a5aae0acf4
12 changed files with 178 additions and 29 deletions

View File

@@ -39,13 +39,14 @@ public class CommandRunner implements Runnable {
System.out.println("Safemode active. No files will be changed!");
}
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate());
LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler("./last-executions.yml") : null;
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate(), lastExecutionHandler);
FileProcessor fileProcessor = new CachedFileProcessor(new MkvFileProcessor(config.getMkvToolNix(), fileFilter));
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired());
AttributeUpdater kernel = config.getCoherent() != null
? new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor)
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor);
? new CoherentAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler)
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
kernel.execute();
}
}

View File

@@ -9,9 +9,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -21,6 +19,7 @@ public class FileFilter {
private final Set<String> excluded;
private final Pattern includePattern;
private final Date filterDate;
private final LastExecutionHandler lastExecutionHandler;
private final String EXTENSION_GROUP = "extension";
private final Pattern extensionPattern = Pattern.compile(String.format(".*(?<%s>\\..*)", EXTENSION_GROUP));
@@ -33,8 +32,9 @@ public class FileFilter {
}
if (!hasMatchingPattern(pathName)
|| !isNewer(pathName)
|| isExcluded(pathName, new HashSet<>(excluded))) {
|| isExcluded(pathName, new HashSet<>(excluded))
|| lastExecutionHandler != null && !isNewOrChanged(pathName)
|| !isNewer(pathName, filterDate)) {
log.debug("Excluded {}", pathName);
ResultStatistic.getInstance().excluded();
return false;
@@ -52,19 +52,20 @@ public class FileFilter {
return includePattern.matcher(pathName.getName()).matches();
}
private boolean isNewer(File pathName) {
if (filterDate == null) return true;
private boolean isNewer(File pathName, Date date) {
if (date == null) return true;
try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()));
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()), date)
|| isNewer(DateUtils.convert(attributes.lastModifiedTime().toMillis()), date);
} catch (IOException e) {
log.warn("File attributes could not be read", e);
}
return true;
}
private boolean isNewer(Date creationDate) {
return creationDate.toInstant().isAfter(filterDate.toInstant());
private boolean isNewer(Date creationDate, Date compareDate) {
return creationDate.toInstant().isAfter(compareDate.toInstant());
}
private boolean isExcluded(File pathName, Set<String> excludedDirs) {
@@ -86,4 +87,9 @@ public class FileFilter {
return false;
}
private boolean isNewOrChanged(File pathname) {
Date lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
return lastExecutionDate == null || isNewer(pathname, lastExecutionDate);
}
}

View File

@@ -0,0 +1,61 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
@Slf4j
public class LastExecutionHandler {
private final File file;
private YAML lastFileExecution;
public LastExecutionHandler(String path) {
file = new File(path);
try {
lastFileExecution = loadLastFileExecution(file);
} catch (YamlInvalidContentException | IOException e) {
log.warn("Couldn't find or read {}", path, e);
}
}
public YAML loadLastFileExecution(File file) throws YamlInvalidContentException, IOException {
if (file.exists() && file.isFile()) {
return new YAML(file);
}
return new YAML("");
}
public Date get(String path) {
if (!lastFileExecution.isSet(path)) return null;
try {
return Date.from(Instant.parse(lastFileExecution.getString(path)));
} catch (YamlKeyNotFoundException e) {
throw new RuntimeException(e);
}
}
public void update(String path) {
update(path, Date.from(Instant.now()));
}
public void update(String path, Date execution) {
if (lastFileExecution == null) return;
lastFileExecution.set(path, execution.toInstant());
}
public void persist() {
try {
lastFileExecution.save(file);
} catch (IOException e) {
log.warn("", e);
}
}
}

View File

@@ -1,6 +1,7 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
@@ -25,14 +26,16 @@ public abstract class AttributeUpdater {
protected final InputConfig config;
protected final FileProcessor fileProcessor;
protected final AttributeChangeProcessor attributeChangeProcessor;
protected final LastExecutionHandler lastExecutionHandler;
protected final ResultStatistic statistic = ResultStatistic.getInstance();
private final ExecutorService executor;
public AttributeUpdater(InputConfig config, FileProcessor fileProcessor, AttributeChangeProcessor attributeChangeProcessor) {
public AttributeUpdater(InputConfig config, FileProcessor fileProcessor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
this.config = config;
this.fileProcessor = fileProcessor;
this.attributeChangeProcessor = attributeChangeProcessor;
this.lastExecutionHandler = lastExecutionHandler;
this.executor = Executors.newFixedThreadPool(config.getThreads());
}
@@ -62,7 +65,7 @@ public abstract class AttributeUpdater {
executor.awaitTermination(1, TimeUnit.DAYS);
}
// writeLastExecutionDate();
lastExecutionHandler.persist();
statistic.stopTimer();
statistic.print();
@@ -84,6 +87,7 @@ public abstract class AttributeUpdater {
* @param fileInfo contains information about file and desired configuration.
*/
protected void checkStatusAndUpdate(FileInfo fileInfo) {
if (lastExecutionHandler != null) lastExecutionHandler.update(fileInfo.getFile().getAbsolutePath());
if (!fileInfo.getChanges().isEmpty()) {
statistic.changePlanned();

View File

@@ -1,5 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
@@ -15,8 +16,8 @@ import java.util.Set;
@Slf4j
public class CoherentAttributeUpdater extends SingleFileAttributeUpdater {
public CoherentAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor) {
super(config, processor, attributeChangeProcessor);
public CoherentAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
super(config, processor, attributeChangeProcessor, lastExecutionHandler);
}
@Override

View File

@@ -1,5 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.LastExecutionHandler;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
import lombok.extern.slf4j.Slf4j;
@@ -12,8 +13,8 @@ import java.util.List;
@Slf4j
public class SingleFileAttributeUpdater extends AttributeUpdater {
public SingleFileAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor) {
super(config, processor, attributeChangeProcessor);
public SingleFileAttributeUpdater(InputConfig config, FileProcessor processor, AttributeChangeProcessor attributeChangeProcessor, LastExecutionHandler lastExecutionHandler) {
super(config, processor, attributeChangeProcessor, lastExecutionHandler);
}
@Override

View File

@@ -53,8 +53,8 @@ public class InputConfig implements CommandLine.IVersionProvider {
private boolean forceCoherent;
// TODO: implement usage
// @Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)")
// private boolean onlyNewFiles;
@Option(names = {"-n", "--only-new-file"}, description = "ignores all files unchanged and previously processed")
private boolean onlyNewFiles;
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
private Date filterDate;
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern")