From 07b675eb937c58c96d8f68bee26c75a1cdaca3a1 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 24 Oct 2010 16:33:38 +0000 Subject: [PATCH] * support for matching date based tv shows (that don't have SxE) --- .../filebot/similarity/DateMetric.java | 89 +++++++++++++++++++ ...hEqualsMetric.java => FileSizeMetric.java} | 2 +- .../similarity/SeasonEpisodeMatcher.java | 6 +- ...tyMetric.java => SeasonEpisodeMetric.java} | 2 +- .../panel/rename/MatchSimilarityMetric.java | 44 +++++++-- source/net/sourceforge/filebot/web/Date.java | 22 ++++- .../filebot/similarity/DateMetricTest.java | 23 +++++ ...Test.java => SeasonEpisodeMetricTest.java} | 6 +- .../similarity/SimilarityTestSuite.java | 2 +- 9 files changed, 177 insertions(+), 19 deletions(-) create mode 100644 source/net/sourceforge/filebot/similarity/DateMetric.java rename source/net/sourceforge/filebot/similarity/{LengthEqualsMetric.java => FileSizeMetric.java} (86%) rename source/net/sourceforge/filebot/similarity/{SeasonEpisodeSimilarityMetric.java => SeasonEpisodeMetric.java} (94%) create mode 100644 test/net/sourceforge/filebot/similarity/DateMetricTest.java rename test/net/sourceforge/filebot/similarity/{SeasonEpisodeSimilarityMetricTest.java => SeasonEpisodeMetricTest.java} (81%) diff --git a/source/net/sourceforge/filebot/similarity/DateMetric.java b/source/net/sourceforge/filebot/similarity/DateMetric.java new file mode 100644 index 00000000..4b399183 --- /dev/null +++ b/source/net/sourceforge/filebot/similarity/DateMetric.java @@ -0,0 +1,89 @@ + +package net.sourceforge.filebot.similarity; + + +import java.io.File; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.sourceforge.filebot.web.Date; + + +public class DateMetric implements SimilarityMetric { + + private final DatePattern[] patterns; + + + public DateMetric() { + patterns = new DatePattern[2]; + + // match yyyy-mm-dd patterns like 2010-10-24, 2009/6/1, etc. + patterns[0] = new DatePattern("(? parse(Object object) { @@ -49,7 +61,23 @@ enum MatchSimilarityMetric implements SimilarityMetric { Episode episode = (Episode) object; // create SxE from episode - return Collections.singleton(new SxE(episode.getSeason(), episode.getEpisode())); + return singleton(new SxE(episode.getSeason(), episode.getEpisode())); + } + + return super.parse(object); + } + }), + + // Match episode airdate + AirDate(new DateMetric() { + + @Override + protected Date parse(Object object) { + if (object instanceof Episode) { + Episode episode = (Episode) object; + + // create SxE from episode + return episode.airdate(); } return super.parse(object); @@ -119,7 +147,7 @@ enum MatchSimilarityMetric implements SimilarityMetric { // 2. pass: match by season / episode numbers // 3. pass: match by generic name similarity (slow, but most matches will have been determined in second pass) // 4. pass: match by generic numeric similarity - return new SimilarityMetric[] { Length, SeasonEpisode, Name, Numeric }; + return new SimilarityMetric[] { FileSize, EpisodeIdentifier, Name, Numeric }; } } diff --git a/source/net/sourceforge/filebot/web/Date.java b/source/net/sourceforge/filebot/web/Date.java index fd29a4cc..276ca417 100644 --- a/source/net/sourceforge/filebot/web/Date.java +++ b/source/net/sourceforge/filebot/web/Date.java @@ -7,6 +7,7 @@ import static java.util.Calendar.*; import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; @@ -48,6 +49,23 @@ public class Date implements Serializable { } + @Override + public boolean equals(Object obj) { + if (obj instanceof Date) { + Date other = (Date) obj; + return year == other.year && month == other.month && day == other.day; + } + + return false; + } + + + @Override + public int hashCode() { + return Arrays.hashCode(new Object[] { year, month, day }); + } + + @Override public String toString() { return String.format("%04d-%02d-%02d", year, month, day); @@ -55,7 +73,7 @@ public class Date implements Serializable { public String format(String pattern) { - return new SimpleDateFormat(pattern).format(new GregorianCalendar(year, month, day).getTime()); + return new SimpleDateFormat(pattern).format(new GregorianCalendar(year, month - 1, day).getTime()); // Calendar months start at 0 } @@ -66,7 +84,7 @@ public class Date implements Serializable { try { Calendar date = new GregorianCalendar(Locale.ROOT); date.setTime(formatter.parse(string)); - return new Date(date.get(YEAR), date.get(MONTH) + 1, date.get(DAY_OF_MONTH)); + return new Date(date.get(YEAR), date.get(MONTH) + 1, date.get(DAY_OF_MONTH)); // Calendar months start at 0 } catch (ParseException e) { // no result if date is invalid Logger.getLogger(Date.class.getName()).log(Level.WARNING, e.getMessage()); diff --git a/test/net/sourceforge/filebot/similarity/DateMetricTest.java b/test/net/sourceforge/filebot/similarity/DateMetricTest.java new file mode 100644 index 00000000..86a13f6b --- /dev/null +++ b/test/net/sourceforge/filebot/similarity/DateMetricTest.java @@ -0,0 +1,23 @@ + +package net.sourceforge.filebot.similarity; + + +import static org.junit.Assert.*; + +import org.junit.Test; + + +public class DateMetricTest { + + private static DateMetric metric = new DateMetric(); + + + @Test + public void getSimilarity() { + assertEquals(1, metric.getSimilarity("2008-02-10", "The Daily Show [10.2.2008] Lou Dobbs"), 0); + assertEquals(0, metric.getSimilarity("2008-01-01", "The Daily Show [10.2.2008] Lou Dobbs"), 0); + assertEquals(1, metric.getSimilarity("2008-04-03", "The Daily Show - 2008.04.03 - George Clooney"), 0); + assertEquals(0, metric.getSimilarity("2008-01-01", "The Daily Show - 2008.04.03 - George Clooney"), 0); + } + +} diff --git a/test/net/sourceforge/filebot/similarity/SeasonEpisodeSimilarityMetricTest.java b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMetricTest.java similarity index 81% rename from test/net/sourceforge/filebot/similarity/SeasonEpisodeSimilarityMetricTest.java rename to test/net/sourceforge/filebot/similarity/SeasonEpisodeMetricTest.java index 348ac681..1bccfe05 100644 --- a/test/net/sourceforge/filebot/similarity/SeasonEpisodeSimilarityMetricTest.java +++ b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMetricTest.java @@ -7,11 +7,11 @@ import static org.junit.Assert.*; import org.junit.Test; -public class SeasonEpisodeSimilarityMetricTest { - - private static SeasonEpisodeSimilarityMetric metric = new SeasonEpisodeSimilarityMetric(); +public class SeasonEpisodeMetricTest { + private static SeasonEpisodeMetric metric = new SeasonEpisodeMetric(); + @Test public void getSimilarity() { // single pattern match, single episode match diff --git a/test/net/sourceforge/filebot/similarity/SimilarityTestSuite.java b/test/net/sourceforge/filebot/similarity/SimilarityTestSuite.java index e81df87d..5649412e 100644 --- a/test/net/sourceforge/filebot/similarity/SimilarityTestSuite.java +++ b/test/net/sourceforge/filebot/similarity/SimilarityTestSuite.java @@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses( { SeriesNameMatcherTest.class, SeasonEpisodeMatcherTest.class, NameSimilarityMetricTest.class, NumericSimilarityMetricTest.class, SeasonEpisodeSimilarityMetricTest.class }) +@SuiteClasses( { SeriesNameMatcherTest.class, SeasonEpisodeMatcherTest.class, NameSimilarityMetricTest.class, NumericSimilarityMetricTest.class, SeasonEpisodeMetricTest.class }) public class SimilarityTestSuite { }