diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 097aede8..400034ce 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -44,6 +44,7 @@ import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.filebot.Resource; import net.filebot.WebServices; import net.filebot.archive.Archive; import net.filebot.format.MediaBindingBean; @@ -458,23 +459,8 @@ public class MediaDetection { } } - // DEBUG debug.finest(format("Match Series Name => %s %s", names, matches)); - - try { - Collection priorityMatchSet = new LinkedHashSet(); - 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 querySet = getUniqueQuerySet(unids, names); - - // DEBUG debug.finest(format("Query Series => %s", querySet)); return querySet; } @@ -940,13 +926,8 @@ public class MediaDetection { } private static List queryMovieByFileName(Collection files, MovieIdentificationService queryLookupService, Locale locale) throws Exception { - // remove blacklisted terms - List querySet = new ArrayList(); - querySet.addAll(stripReleaseInfo(files, true)); - querySet.addAll(stripReleaseInfo(files, false)); - - // remove duplicates - querySet = getUniqueQuerySet(emptySet(), stripBlacklistedTerms(querySet)); + // remove blacklisted terms and remove duplicates + List querySet = getUniqueQuerySet(emptySet(), files); // DEBUG debug.finest(format("Query Movie => %s", querySet)); @@ -969,14 +950,19 @@ public class MediaDetection { } private static List getUniqueQuerySet(Collection exactMatches, Collection guessMatches) { - Map uniqueMap = new LinkedHashMap(); + Map unique = new LinkedHashMap(); // unique key function (case-insensitive ignore-punctuation) Function normalize = (s) -> normalizePunctuation(s).toLowerCase(); - addUniqueQuerySet(exactMatches, normalize, Function.identity(), uniqueMap); - addUniqueQuerySet(guessMatches, normalize, normalize, uniqueMap); + addUniqueQuerySet(exactMatches, normalize, Function.identity(), unique); - return new ArrayList(uniqueMap.values()); + // remove blacklisted terms and remove duplicates + Set terms = new LinkedHashSet(); + terms.addAll(stripReleaseInfo(guessMatches, true)); + terms.addAll(stripReleaseInfo(guessMatches, false)); + addUniqueQuerySet(stripBlacklistedTerms(terms), normalize, normalize, unique); + + return new ArrayList(unique.values()); } private static void addUniqueQuerySet(Collection terms, Function keyFunction, Function valueFunction, Map uniqueMap) { @@ -988,10 +974,6 @@ public class MediaDetection { } } - public static String stripReleaseInfo(String name) { - return stripReleaseInfo(name, true); - } - public static List matchMovieByWordSequence(String name, Collection options, int maxStartIndex) { List movies = new ArrayList(); @@ -1019,18 +1001,6 @@ public class MediaDetection { return formatInfoPattern.matcher(name).replaceAll(""); } - public static String stripReleaseInfo(String name, boolean strict) { - try { - Iterator 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 { if (folder == null || folder.getName() == null || folder.getName().isEmpty() || releaseInfo.getVolumeRoots().contains(folder)) { return true; @@ -1154,24 +1124,37 @@ public class MediaDetection { return file.getParentFile(); } - public static List stripReleaseInfo(Collection names, boolean strict) throws Exception { - return releaseInfo.cleanRelease(names, strict); + public static List stripReleaseInfo(Collection names, boolean 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(names); } - private static Pattern blacklistPattern; - - public static List stripBlacklistedTerms(Collection names) throws Exception { - if (blacklistPattern == null) { - blacklistPattern = releaseInfo.getBlacklistPattern(); + public static String stripReleaseInfo(String name, boolean strict) { + Iterator value = stripReleaseInfo(singleton(name), strict).iterator(); + if (value.hasNext()) { + return value.next(); } + return ""; // default value in case all tokens are stripped away + } - List acceptables = new ArrayList(names.size()); - for (String it : names) { - if (blacklistPattern.matcher(it).replaceAll("").trim().length() > 0) { - acceptables.add(it); - } + public static String stripReleaseInfo(String name) { + return stripReleaseInfo(name, true); + } + + private static final Resource blacklistPattern = Resource.lazy(releaseInfo::getBlacklistPattern); + + public static List stripBlacklistedTerms(Collection 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 grepImdbIdFor(File file) throws Exception {