Add basic date filter

This commit is contained in:
2023-04-02 20:11:42 +02:00
parent 943308dd59
commit 7ee51421e0
6 changed files with 93 additions and 2 deletions

View File

@@ -38,6 +38,8 @@ public class Config {
private Integer coherent;
private boolean forceCoherent;
private boolean onlyNewFiles;
private Date filterDate;
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs", "songs"));
private Set<String> commentaryKeywords = new HashSet<>(Arrays.asList("commentary", "director"));

View File

@@ -24,6 +24,8 @@ public class ConfigLoader {
new ThreadValidator(THREADS, false, 2),
new MkvToolNixPathValidator(MKV_TOOL_NIX, true, Path.of("C:\\Program Files\\MKVToolNix").toFile()),
new BooleanValidator(SAFE_MODE, false),
new BooleanValidator(ONLY_NEW_FILES, false),
new DateValidator(FILTER_DATE, false),
new PatternValidator(INCLUDE_PATTERN, false, Pattern.compile(".*")),
new SetValidator(FORCED_KEYWORDS, false, true),
new SetValidator(COMMENTARY_KEYWORDS, false, true),

View File

@@ -0,0 +1,22 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import java.util.Date;
public class DateValidator extends ConfigValidator<Date> {
public DateValidator(ConfigProperty property, boolean required) {
super(property, required, null);
}
@Override
Date parse(String value) {
return DateUtils.convert(value); // TODO fix null return value
}
@Override
boolean isValid(Date result) {
return result != null;
}
}

View File

@@ -2,18 +2,56 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
@Slf4j
public class FileFilter {
static boolean accept(File pathName, String[] fileExtensions) {
if (StringUtils.endsWithAny(pathName.getAbsolutePath().toLowerCase(), fileExtensions)
&& Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches()) {
if (hasProperFileExtension(pathName, fileExtensions) && isIncluded(pathName) && isNewer(pathName)) {
return true;
}
ResultStatistic.getInstance().excluded();
return false;
}
private static boolean hasProperFileExtension(File pathName, String[] fileExtensions) {
return StringUtils.endsWithAny(pathName.getAbsolutePath().toLowerCase(), fileExtensions);
}
private static boolean isIncluded(File pathName) {
return Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
}
private static boolean isNewer(File pathName) {
Config config = Config.getInstance();
if (config.getFilterDate() == null) return true;
try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()));
} catch (IOException e) {
log.warn("File attributes could not be read. This could have XX reason"); // TODO
}
return true;
}
private static boolean isNewer(Date creationDate) {
return creationDate.toInstant().isAfter(Config.getInstance().getFilterDate().toInstant());
}
private static boolean isNewerThanLastExecution(File pathName) {
if (Config.getInstance().isOnlyNewFiles()) {
return isNewer(pathName);
}
return true;
// return Config.getInstance().isOnlyNewFiles() && isNewer(pathName);
}
}

View File

@@ -18,6 +18,8 @@ public enum ConfigProperty {
COHERENT("coherent", "Try to match all files in dir of depth with the same config", "c", 1),
FORCE_COHERENT("force-coherent", "Force coherent and don't update anything if config fits not whole config (default: false)", "cf", 0),
WINDOWS("windows", "Is operating system windows", null, 0),
ONLY_NEW_FILES("only-new-files", "Sets filter-date to last successful execution (Overwrites input of filter-date)", "n", 0),
FILTER_DATE("filter-date", "Only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")", "d", 1),
THREADS("threads", "Thread count (default: 2)", "t", 1),
INCLUDE_PATTERN("include-pattern", "Include files matching pattern (default: \".*\")", "i", 1),
EXCLUDED_DIRECTORY("excluded-directories", "Directories to be excluded, combines with config file", "e", Option.UNLIMITED_VALUES),

View File

@@ -0,0 +1,25 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy-HH:mm:ss");
public static Date convert(long millis) {
return new Date(millis);
}
/**
* Convert String to date.
* @return parsed date, null if exception occurs
*/
public static Date convert(String date) {
try {
return dateFormat.parse(date);
} catch (ParseException e) {
return null;
}
}
}