Refactor
This commit is contained in:
parent
c552bdb8ae
commit
a73952ea81
|
@ -44,6 +44,7 @@ import java.util.logging.Level;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import net.filebot.Resource;
|
||||||
import net.filebot.WebServices;
|
import net.filebot.WebServices;
|
||||||
import net.filebot.archive.Archive;
|
import net.filebot.archive.Archive;
|
||||||
import net.filebot.format.MediaBindingBean;
|
import net.filebot.format.MediaBindingBean;
|
||||||
|
@ -458,23 +459,8 @@ public class MediaDetection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
debug.finest(format("Match Series Name => %s %s", names, matches));
|
debug.finest(format("Match Series Name => %s %s", names, matches));
|
||||||
|
|
||||||
try {
|
|
||||||
Collection<String> priorityMatchSet = new LinkedHashSet<String>();
|
|
||||||
priorityMatchSet.addAll(stripReleaseInfo(matches, true));
|
|
||||||
priorityMatchSet.addAll(stripReleaseInfo(matches, false));
|
|
||||||
matches = stripBlacklistedTerms(priorityMatchSet);
|
|
||||||
} catch (Exception e) {
|
|
||||||
debug.warning("Failed to clean matches: " + e);
|
|
||||||
}
|
|
||||||
names.addAll(matches);
|
|
||||||
|
|
||||||
// filter out duplicates
|
|
||||||
List<String> querySet = getUniqueQuerySet(unids, names);
|
List<String> querySet = getUniqueQuerySet(unids, names);
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
debug.finest(format("Query Series => %s", querySet));
|
debug.finest(format("Query Series => %s", querySet));
|
||||||
return querySet;
|
return querySet;
|
||||||
}
|
}
|
||||||
|
@ -940,13 +926,8 @@ public class MediaDetection {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Movie> queryMovieByFileName(Collection<String> files, MovieIdentificationService queryLookupService, Locale locale) throws Exception {
|
private static List<Movie> queryMovieByFileName(Collection<String> files, MovieIdentificationService queryLookupService, Locale locale) throws Exception {
|
||||||
// remove blacklisted terms
|
// remove blacklisted terms and remove duplicates
|
||||||
List<String> querySet = new ArrayList<String>();
|
List<String> querySet = getUniqueQuerySet(emptySet(), files);
|
||||||
querySet.addAll(stripReleaseInfo(files, true));
|
|
||||||
querySet.addAll(stripReleaseInfo(files, false));
|
|
||||||
|
|
||||||
// remove duplicates
|
|
||||||
querySet = getUniqueQuerySet(emptySet(), stripBlacklistedTerms(querySet));
|
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
debug.finest(format("Query Movie => %s", querySet));
|
debug.finest(format("Query Movie => %s", querySet));
|
||||||
|
@ -969,14 +950,19 @@ public class MediaDetection {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> getUniqueQuerySet(Collection<String> exactMatches, Collection<String> guessMatches) {
|
private static List<String> getUniqueQuerySet(Collection<String> exactMatches, Collection<String> guessMatches) {
|
||||||
Map<String, String> uniqueMap = new LinkedHashMap<String, String>();
|
Map<String, String> unique = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
// unique key function (case-insensitive ignore-punctuation)
|
// unique key function (case-insensitive ignore-punctuation)
|
||||||
Function<String, String> normalize = (s) -> normalizePunctuation(s).toLowerCase();
|
Function<String, String> normalize = (s) -> normalizePunctuation(s).toLowerCase();
|
||||||
addUniqueQuerySet(exactMatches, normalize, Function.identity(), uniqueMap);
|
addUniqueQuerySet(exactMatches, normalize, Function.identity(), unique);
|
||||||
addUniqueQuerySet(guessMatches, normalize, normalize, uniqueMap);
|
|
||||||
|
|
||||||
return new ArrayList<String>(uniqueMap.values());
|
// remove blacklisted terms and remove duplicates
|
||||||
|
Set<String> terms = new LinkedHashSet<String>();
|
||||||
|
terms.addAll(stripReleaseInfo(guessMatches, true));
|
||||||
|
terms.addAll(stripReleaseInfo(guessMatches, false));
|
||||||
|
addUniqueQuerySet(stripBlacklistedTerms(terms), normalize, normalize, unique);
|
||||||
|
|
||||||
|
return new ArrayList<String>(unique.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addUniqueQuerySet(Collection<String> terms, Function<String, String> keyFunction, Function<String, String> valueFunction, Map<String, String> uniqueMap) {
|
private static void addUniqueQuerySet(Collection<String> terms, Function<String, String> keyFunction, Function<String, String> valueFunction, Map<String, String> uniqueMap) {
|
||||||
|
@ -988,10 +974,6 @@ public class MediaDetection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stripReleaseInfo(String name) {
|
|
||||||
return stripReleaseInfo(name, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Movie> matchMovieByWordSequence(String name, Collection<Movie> options, int maxStartIndex) {
|
public static List<Movie> matchMovieByWordSequence(String name, Collection<Movie> options, int maxStartIndex) {
|
||||||
List<Movie> movies = new ArrayList<Movie>();
|
List<Movie> movies = new ArrayList<Movie>();
|
||||||
|
|
||||||
|
@ -1019,18 +1001,6 @@ public class MediaDetection {
|
||||||
return formatInfoPattern.matcher(name).replaceAll("");
|
return formatInfoPattern.matcher(name).replaceAll("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stripReleaseInfo(String name, boolean strict) {
|
|
||||||
try {
|
|
||||||
Iterator<String> value = releaseInfo.cleanRelease(singleton(name), strict).iterator();
|
|
||||||
if (value.hasNext()) {
|
|
||||||
return value.next();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
debug.log(Level.SEVERE, "Failed to strip release info: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return ""; // default value in case all tokens are stripped away
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isStructureRoot(File folder) throws Exception {
|
public static boolean isStructureRoot(File folder) throws Exception {
|
||||||
if (folder == null || folder.getName() == null || folder.getName().isEmpty() || releaseInfo.getVolumeRoots().contains(folder)) {
|
if (folder == null || folder.getName() == null || folder.getName().isEmpty() || releaseInfo.getVolumeRoots().contains(folder)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1154,24 +1124,37 @@ public class MediaDetection {
|
||||||
return file.getParentFile();
|
return file.getParentFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> stripReleaseInfo(Collection<String> names, boolean strict) throws Exception {
|
public static List<String> stripReleaseInfo(Collection<String> names, boolean strict) {
|
||||||
return releaseInfo.cleanRelease(names, strict);
|
try {
|
||||||
|
return releaseInfo.cleanRelease(names, strict);
|
||||||
|
} catch (Exception e) {
|
||||||
|
debug.log(Level.SEVERE, "Failed to strip release info: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return new ArrayList<String>(names);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pattern blacklistPattern;
|
public static String stripReleaseInfo(String name, boolean strict) {
|
||||||
|
Iterator<String> value = stripReleaseInfo(singleton(name), strict).iterator();
|
||||||
public static List<String> stripBlacklistedTerms(Collection<String> names) throws Exception {
|
if (value.hasNext()) {
|
||||||
if (blacklistPattern == null) {
|
return value.next();
|
||||||
blacklistPattern = releaseInfo.getBlacklistPattern();
|
|
||||||
}
|
}
|
||||||
|
return ""; // default value in case all tokens are stripped away
|
||||||
|
}
|
||||||
|
|
||||||
List<String> acceptables = new ArrayList<String>(names.size());
|
public static String stripReleaseInfo(String name) {
|
||||||
for (String it : names) {
|
return stripReleaseInfo(name, true);
|
||||||
if (blacklistPattern.matcher(it).replaceAll("").trim().length() > 0) {
|
}
|
||||||
acceptables.add(it);
|
|
||||||
}
|
private static final Resource<Pattern> blacklistPattern = Resource.lazy(releaseInfo::getBlacklistPattern);
|
||||||
|
|
||||||
|
public static List<String> stripBlacklistedTerms(Collection<String> names) {
|
||||||
|
try {
|
||||||
|
Pattern pattern = blacklistPattern.get();
|
||||||
|
return names.stream().filter(s -> pattern.matcher(s).replaceAll("").trim().length() > 0).collect(toList());
|
||||||
|
} catch (Exception e) {
|
||||||
|
debug.log(Level.SEVERE, "Failed to strip release info: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return acceptables;
|
return emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<Integer> grepImdbIdFor(File file) throws Exception {
|
public static Set<Integer> grepImdbIdFor(File file) throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue