Consider media encoding date when comparing TimeStamp episode similarity

This commit is contained in:
Reinhard Pointner 2016-08-10 05:50:54 +08:00
parent c6514b817a
commit 9b640acd53
3 changed files with 58 additions and 40 deletions

View File

@ -13,6 +13,7 @@ import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
import java.io.File; import java.io.File;
import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -520,20 +521,20 @@ public enum EpisodeMetrics implements SimilarityMetric {
return f >= 0.9 ? 1 : f >= 0 ? 0 : -1; return f >= 0.9 ? 1 : f >= 0 ? 0 : -1;
} }
@Override private long getTimeStamp(SimpleDate date) {
public long getTimeStamp(Object object) {
if (object instanceof Episode) {
SimpleDate date = ((Episode) object).getAirdate();
// some episodes may not have a defined airdate // some episodes may not have a defined airdate
if (date != null) { if (date != null) {
long ts = date.getTimeStamp(); Instant t = date.toInstant();
if (t.isBefore(Instant.now())) {
return t.toEpochMilli();
}
}
// big penalty for episodes not yet aired // big penalty for episodes not yet aired
return ts > System.currentTimeMillis() ? -1 : ts; return -1;
} }
} else if (object instanceof File) {
File file = (File) object; private long getTimeStamp(File file) {
if (VIDEO_FILES.accept(file) && file.length() > ONE_MEGABYTE) { if (VIDEO_FILES.accept(file) && file.length() > ONE_MEGABYTE) {
try (MediaInfo mi = new MediaInfo().open(file)) { 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 String date = mi.get(StreamKind.General, 0, "Encoded_Date"); // e.g. UTC 2008-01-08 19:54:39
@ -545,9 +546,24 @@ public enum EpisodeMetrics implements SimilarityMetric {
debug.warning(format("Failed to read media encoding date: %s", e.getMessage())); debug.warning(format("Failed to read media encoding date: %s", e.getMessage()));
} }
} }
return super.getTimeStamp(file); // default to file creation date
} }
return super.getTimeStamp(object); @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;
} }
}), }),

View File

@ -21,22 +21,20 @@ public class TimeStampMetric implements SimilarityMetric {
return min / max; return min / max;
} }
public long getTimeStamp(Object obj) { public long getTimeStamp(Object object) {
if (obj instanceof File) { if (object instanceof File) {
File f = (File) object;
try { try {
BasicFileAttributes attr = Files.readAttributes(((File) obj).toPath(), BasicFileAttributes.class); BasicFileAttributes attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
long creationTime = attr.creationTime().toMillis(); long creationTime = attr.creationTime().toMillis();
if (creationTime > 0) { if (creationTime > 0) {
return creationTime; return creationTime;
} else { } else {
return attr.lastModifiedTime().toMillis(); return attr.lastModifiedTime().toMillis();
} }
} catch (Throwable e) { } catch (Exception e) {
// ignore Java 6 issues // ignore, default to -1
return ((File) obj).lastModified();
} }
} else if (obj instanceof Number) {
return ((Number) obj).longValue();
} }
return -1; return -1;

View File

@ -48,17 +48,13 @@ public class SimpleDate implements Serializable, Comparable<Object> {
return day; return day;
} }
public long getTimeStamp() {
return this.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof SimpleDate) { if (obj instanceof SimpleDate) {
SimpleDate other = (SimpleDate) obj; SimpleDate other = (SimpleDate) obj;
return year == other.year && month == other.month && day == other.day; return year == other.year && month == other.month && day == other.day;
} else if (obj instanceof CharSequence) { } else if (obj instanceof CharSequence) {
return this.toString().equals(obj.toString()); return toString().equals(obj.toString());
} }
return super.equals(obj); return super.equals(obj);
@ -79,7 +75,7 @@ public class SimpleDate implements Serializable, Comparable<Object> {
} }
public int compareTo(SimpleDate other) { public int compareTo(SimpleDate other) {
return Long.compare(this.getTimeStamp(), other.getTimeStamp()); return Long.compare(getTimeStamp(), other.getTimeStamp());
} }
@Override @Override
@ -93,13 +89,21 @@ public class SimpleDate implements Serializable, Comparable<Object> {
} }
public String format(String pattern) { 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() { public LocalDate toLocalDate() {
return LocalDate.of(year, month, day); return LocalDate.of(year, month, day);
} }
public Instant toInstant() {
return toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant();
}
public long getTimeStamp() {
return toInstant().toEpochMilli();
}
@Override @Override
public String toString() { public String toString() {
return String.format("%04d-%02d-%02d", year, month, day); return String.format("%04d-%02d-%02d", year, month, day);