diff --git a/source/net/filebot/similarity/EpisodeMetrics.java b/source/net/filebot/similarity/EpisodeMetrics.java index 11698cbb..e2da2b52 100644 --- a/source/net/filebot/similarity/EpisodeMetrics.java +++ b/source/net/filebot/similarity/EpisodeMetrics.java @@ -13,6 +13,7 @@ import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.io.File; +import java.time.Instant; import java.time.LocalDate; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -520,34 +521,49 @@ public enum EpisodeMetrics implements SimilarityMetric { return f >= 0.9 ? 1 : f >= 0 ? 0 : -1; } - @Override - public long getTimeStamp(Object object) { - if (object instanceof Episode) { - SimpleDate date = ((Episode) object).getAirdate(); - - // some episodes may not have a defined airdate - if (date != null) { - long ts = date.getTimeStamp(); - - // big penalty for episodes not yet aired - return ts > System.currentTimeMillis() ? -1 : ts; - } - } else if (object instanceof File) { - File file = (File) object; - if (VIDEO_FILES.accept(file) && file.length() > ONE_MEGABYTE) { - try (MediaInfo mi = new MediaInfo().open(file)) { - String date = mi.get(StreamKind.General, 0, "Encoded_Date"); // e.g. UTC 2008-01-08 19:54:39 - if (date.length() > 0) { - ZonedDateTime time = ZonedDateTime.parse(date, DateTimeFormatter.ofPattern("zzz uuuu-MM-dd HH:mm:ss")); - return time.toInstant().toEpochMilli(); - } - } catch (Exception e) { - debug.warning(format("Failed to read media encoding date: %s", e.getMessage())); - } + private long getTimeStamp(SimpleDate date) { + // some episodes may not have a defined airdate + if (date != null) { + Instant t = date.toInstant(); + if (t.isBefore(Instant.now())) { + return t.toEpochMilli(); } } - return super.getTimeStamp(object); + // big penalty for episodes not yet aired + return -1; + } + + private long getTimeStamp(File file) { + if (VIDEO_FILES.accept(file) && file.length() > ONE_MEGABYTE) { + try (MediaInfo mi = new MediaInfo().open(file)) { + String date = mi.get(StreamKind.General, 0, "Encoded_Date"); // e.g. UTC 2008-01-08 19:54:39 + if (date.length() > 0) { + ZonedDateTime time = ZonedDateTime.parse(date, DateTimeFormatter.ofPattern("zzz uuuu-MM-dd HH:mm:ss")); + return time.toInstant().toEpochMilli(); + } + } catch (Exception e) { + debug.warning(format("Failed to read media encoding date: %s", e.getMessage())); + } + } + + return super.getTimeStamp(file); // default to file creation date + } + + @Override + public long getTimeStamp(Object object) { + if (object instanceof Episode) { + Episode e = (Episode) object; + return getTimeStamp(e.getAirdate()); + } else if (object instanceof Movie) { + Movie m = (Movie) object; + return getTimeStamp(new SimpleDate(m.getYear(), 1, 1)); + } else if (object instanceof File) { + File file = (File) object; + return getTimeStamp(file); + } + + return -1; } }), diff --git a/source/net/filebot/similarity/TimeStampMetric.java b/source/net/filebot/similarity/TimeStampMetric.java index fac822e9..deac557f 100644 --- a/source/net/filebot/similarity/TimeStampMetric.java +++ b/source/net/filebot/similarity/TimeStampMetric.java @@ -21,22 +21,20 @@ public class TimeStampMetric implements SimilarityMetric { return min / max; } - public long getTimeStamp(Object obj) { - if (obj instanceof File) { + public long getTimeStamp(Object object) { + if (object instanceof File) { + File f = (File) object; try { - BasicFileAttributes attr = Files.readAttributes(((File) obj).toPath(), BasicFileAttributes.class); + BasicFileAttributes attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class); long creationTime = attr.creationTime().toMillis(); if (creationTime > 0) { return creationTime; } else { return attr.lastModifiedTime().toMillis(); } - } catch (Throwable e) { - // ignore Java 6 issues - return ((File) obj).lastModified(); + } catch (Exception e) { + // ignore, default to -1 } - } else if (obj instanceof Number) { - return ((Number) obj).longValue(); } return -1; diff --git a/source/net/filebot/web/SimpleDate.java b/source/net/filebot/web/SimpleDate.java index 1b37bd23..5db3add9 100644 --- a/source/net/filebot/web/SimpleDate.java +++ b/source/net/filebot/web/SimpleDate.java @@ -48,17 +48,13 @@ public class SimpleDate implements Serializable, Comparable { return day; } - public long getTimeStamp() { - return this.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli(); - } - @Override public boolean equals(Object obj) { if (obj instanceof SimpleDate) { SimpleDate other = (SimpleDate) obj; return year == other.year && month == other.month && day == other.day; } else if (obj instanceof CharSequence) { - return this.toString().equals(obj.toString()); + return toString().equals(obj.toString()); } return super.equals(obj); @@ -79,7 +75,7 @@ public class SimpleDate implements Serializable, Comparable { } public int compareTo(SimpleDate other) { - return Long.compare(this.getTimeStamp(), other.getTimeStamp()); + return Long.compare(getTimeStamp(), other.getTimeStamp()); } @Override @@ -93,13 +89,21 @@ public class SimpleDate implements Serializable, Comparable { } public String format(String pattern) { - return DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH).format(this.toLocalDate()); + return DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH).format(toLocalDate()); } public LocalDate toLocalDate() { return LocalDate.of(year, month, day); } + public Instant toInstant() { + return toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant(); + } + + public long getTimeStamp() { + return toInstant().toEpochMilli(); + } + @Override public String toString() { return String.format("%04d-%02d-%02d", year, month, day);