1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-02 08:25:02 -04:00

+ ExpressionFormat: support for "inferred" files (e.g. if we are formatting a subtitle, use media info of the actual movie file if possible)

+ ExpressionFormat: bindings are now case insensitive
This commit is contained in:
Reinhard Pointner 2009-05-03 16:28:39 +00:00
parent 1ed4d2b495
commit f4fa49e49b
2 changed files with 37 additions and 13 deletions

View File

@ -2,7 +2,9 @@
package net.sourceforge.filebot.format; package net.sourceforge.filebot.format;
import static net.sourceforge.filebot.FileBotUtilities.MOVIE_FILES;
import static net.sourceforge.filebot.FileBotUtilities.SFV_FILES; import static net.sourceforge.filebot.FileBotUtilities.SFV_FILES;
import static net.sourceforge.filebot.FileBotUtilities.SUBTITLE_FILES;
import static net.sourceforge.filebot.format.Define.undefined; import static net.sourceforge.filebot.format.Define.undefined;
import java.io.File; import java.io.File;
@ -126,25 +128,27 @@ public class EpisodeFormatBindingBean {
// make sure media file is defined // make sure media file is defined
checkMediaFile(); checkMediaFile();
File inferredMediaFile = getInferredMediaFile();
// try to get checksum from file name // try to get checksum from file name
String checksum = FileBotUtilities.getEmbeddedChecksum(mediaFile.getName()); String checksum = FileBotUtilities.getEmbeddedChecksum(inferredMediaFile.getName());
if (checksum != null) if (checksum != null)
return checksum; return checksum;
// try to get checksum from sfv file // try to get checksum from sfv file
checksum = getChecksumFromSfvFile(mediaFile); checksum = getChecksumFromSfvFile(inferredMediaFile);
if (checksum != null) if (checksum != null)
return checksum; return checksum;
// calculate checksum from file // calculate checksum from file
return crc32(mediaFile); return crc32(inferredMediaFile);
} }
@Define("ext") @Define("ext")
public String getContainerExtension() { public String getExtension() {
// make sure media file is defined // make sure media file is defined
checkMediaFile(); checkMediaFile();
@ -153,7 +157,7 @@ public class EpisodeFormatBindingBean {
} }
@Define("general") @Define("media")
public Object getGeneralMediaInfo() { public Object getGeneralMediaInfo() {
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.General, 0)); return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.General, 0));
} }
@ -195,6 +199,24 @@ public class EpisodeFormatBindingBean {
} }
@Define("inferredFile")
public File getInferredMediaFile() {
if (SUBTITLE_FILES.accept(mediaFile)) {
// file is a subtitle
String name = FileUtilities.getName(mediaFile);
// find corresponding movie file
for (File movie : mediaFile.getParentFile().listFiles(MOVIE_FILES)) {
if (name.startsWith(FileUtilities.getName(movie))) {
return movie;
}
}
}
return mediaFile;
}
private void checkMediaFile() { private void checkMediaFile() {
// make sure file is not null // make sure file is not null
if (mediaFile == null) if (mediaFile == null)
@ -210,11 +232,13 @@ public class EpisodeFormatBindingBean {
// make sure media file is defined // make sure media file is defined
checkMediaFile(); checkMediaFile();
mediaInfo = new MediaInfo(); MediaInfo newMediaInfo = new MediaInfo();
if (!mediaInfo.open(mediaFile)) { if (!newMediaInfo.open(getInferredMediaFile())) {
throw new RuntimeException(String.format("Cannot open file: %s", mediaFile)); throw new RuntimeException(String.format("Cannot open media file: %s", mediaFile));
} }
mediaInfo = newMediaInfo;
} }
return mediaInfo; return mediaInfo;
@ -234,8 +258,8 @@ public class EpisodeFormatBindingBean {
} }
private String getChecksumFromSfvFile(File mediaFile) throws IOException { private String getChecksumFromSfvFile(File file) throws IOException {
File folder = mediaFile.getParentFile(); File folder = file.getParentFile();
for (File sfvFile : folder.listFiles(SFV_FILES)) { for (File sfvFile : folder.listFiles(SFV_FILES)) {
SfvFileScanner scanner = new SfvFileScanner(sfvFile); SfvFileScanner scanner = new SfvFileScanner(sfvFile);
@ -245,7 +269,7 @@ public class EpisodeFormatBindingBean {
try { try {
Entry<File, String> entry = scanner.next(); Entry<File, String> entry = scanner.next();
if (mediaFile.getName().equals(entry.getKey().getPath())) { if (file.getName().equals(entry.getKey().getPath())) {
return entry.getValue(); return entry.getValue();
} }
} catch (IllegalSyntaxException e) { } catch (IllegalSyntaxException e) {

View File

@ -4,10 +4,10 @@ package net.sourceforge.filebot.format;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap;
import javax.script.Bindings; import javax.script.Bindings;
@ -18,7 +18,7 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
protected final Object bindingBean; protected final Object bindingBean;
protected final Map<String, Method> bindings = new HashMap<String, Method>(); protected final Map<String, Method> bindings = new TreeMap<String, Method>(String.CASE_INSENSITIVE_ORDER);
public ExpressionBindings(Object bindingBean) { public ExpressionBindings(Object bindingBean) {