From 6a43ab6e3fd9465212c0b55a20780f1473dc4888 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 22 Nov 2009 12:51:23 +0000 Subject: [PATCH] * added special handling for subtitles in movie matcher --- .../ui/panel/rename/MovieFormatter.java | 4 +- .../ui/panel/rename/MovieHashMatcher.java | 47 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/source/net/sourceforge/filebot/ui/panel/rename/MovieFormatter.java b/source/net/sourceforge/filebot/ui/panel/rename/MovieFormatter.java index b8e52bf4..e9f718df 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/MovieFormatter.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/MovieFormatter.java @@ -22,8 +22,8 @@ class MovieFormatter implements MatchFormatter { @Override public String format(Match match) { - MovieDescriptor movie = (MovieDescriptor) match.getValue(); - return movie.getName(); + // use default format for the time being + return match.getValue().toString(); } } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/MovieHashMatcher.java b/source/net/sourceforge/filebot/ui/panel/rename/MovieHashMatcher.java index b6777912..c1ede12a 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/MovieHashMatcher.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/MovieHashMatcher.java @@ -3,11 +3,14 @@ package net.sourceforge.filebot.ui.panel.rename; import static net.sourceforge.filebot.MediaTypes.*; +import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.ui.TunedUtilities.*; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Scanner; @@ -23,7 +26,6 @@ import net.sourceforge.filebot.similarity.Match; import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.web.MovieDescriptor; import net.sourceforge.filebot.web.MovieIdentificationService; -import net.sourceforge.tuned.FileUtilities; class MovieHashMatcher implements AutoCompleteMatcher { @@ -37,9 +39,9 @@ class MovieHashMatcher implements AutoCompleteMatcher { @Override - public List> match(List files) throws Exception { - // focus on movie and subtitle files - File[] movieFiles = FileUtilities.filter(files, VIDEO_FILES).toArray(new File[0]); + public List> match(final List files) throws Exception { + // handle movie files + File[] movieFiles = filter(files, VIDEO_FILES).toArray(new File[0]); MovieDescriptor[] movieDescriptors = service.getMovieDescriptors(movieFiles); List> matches = new ArrayList>(); @@ -57,6 +59,30 @@ class MovieHashMatcher implements AutoCompleteMatcher { } } + // handle subtitle files + for (File subtitle : filter(files, SUBTITLE_FILES)) { + // check if subtitle corresponds to a movie file (same name, different extension) + for (Match movieMatch : matches) { + String subtitleName = getName(subtitle); + String movieName = getName(movieMatch.getValue()); + + if (subtitleName.equalsIgnoreCase(movieName)) { + matches.add(new Match(subtitle, movieMatch.getCandidate())); + // movie match found, we're done + break; + } + } + } + + // restore original order + Collections.sort(matches, new Comparator>() { + + @Override + public int compare(Match o1, Match o2) { + return files.indexOf(o1.getValue()) - files.indexOf(o2.getValue()); + } + }); + return matches; } @@ -66,11 +92,16 @@ class MovieHashMatcher implements AutoCompleteMatcher { for (File file : files) { Scanner scanner = new Scanner(file); - String imdb = null; - // scan for imdb id patterns like tt1234567 - while ((imdb = scanner.findWithinHorizon("(?<=tt)\\d{7}", 32 * 1024)) != null) { - collection.add(Integer.parseInt(imdb)); + try { + // scan for imdb id patterns like tt1234567 + String imdb = null; + + while ((imdb = scanner.findWithinHorizon("(?<=tt)\\d{7}", 32 * 1024)) != null) { + collection.add(Integer.parseInt(imdb)); + } + } finally { + scanner.close(); } }