Refactor filename bindings

This commit is contained in:
Reinhard Pointner 2016-05-29 23:24:54 +08:00
parent e541bf1154
commit 2d31f81367
1 changed files with 38 additions and 46 deletions

View File

@ -30,7 +30,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Optional;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -51,6 +51,7 @@ import net.filebot.media.NamingStandard;
import net.filebot.mediainfo.MediaInfo; import net.filebot.mediainfo.MediaInfo;
import net.filebot.mediainfo.MediaInfo.StreamKind; import net.filebot.mediainfo.MediaInfo.StreamKind;
import net.filebot.mediainfo.MediaInfoException; import net.filebot.mediainfo.MediaInfoException;
import net.filebot.similarity.Normalization;
import net.filebot.similarity.SimilarityComparator; import net.filebot.similarity.SimilarityComparator;
import net.filebot.util.FileUtilities; import net.filebot.util.FileUtilities;
import net.filebot.util.WeakValueHashMap; import net.filebot.util.WeakValueHashMap;
@ -251,7 +252,7 @@ public class MediaBindingBean {
return WebServices.TheTVDB.getSeriesInfo(getSeriesInfo().getId(), Locale.ENGLISH).getName(); return WebServices.TheTVDB.getSeriesInfo(getSeriesInfo().getId(), Locale.ENGLISH).getName();
} }
// default to series info name (for anime this would be the primary title) // default to series info name (for Anime this would be the primary title)
return getSeriesInfo().getName(); return getSeriesInfo().getName();
} }
@ -419,7 +420,8 @@ public class MediaBindingBean {
@Define("original") @Define("original")
public String getOriginalFileName() { public String getOriginalFileName() {
return getOriginalFileName(getMediaFile()); String name = xattr.getOriginalName(getMediaFile());
return name == null ? null : getNameWithoutExtension(name);
} }
@Define("xattr") @Define("xattr")
@ -433,13 +435,9 @@ public class MediaBindingBean {
File inferredMediaFile = getInferredMediaFile(); File inferredMediaFile = getInferredMediaFile();
// try to get checksum from file name // try to get checksum from file name
for (String filename : new String[] { getOriginalFileName(inferredMediaFile), inferredMediaFile.getName() }) { Optional<String> embeddedChecksum = stream(getFileNames(inferredMediaFile)).map(Normalization::getEmbeddedChecksum).filter(Objects::nonNull).findFirst();
if (filename != null) { if (embeddedChecksum.isPresent()) {
String checksum = getEmbeddedChecksum(filename); return embeddedChecksum.get();
if (checksum != null) {
return checksum;
}
}
} }
// try to get checksum from sfv file // try to get checksum from sfv file
@ -478,54 +476,36 @@ public class MediaBindingBean {
@Define("source") @Define("source")
public String getVideoSource() { public String getVideoSource() {
// use inferred media file // look for video source patterns in media file and it's parent folder (use inferred media file)
File inferredMediaFile = getInferredMediaFile(); return releaseInfo.getVideoSource(getFileNames(getInferredMediaFile()));
// look for video source patterns in media file and it's parent folder
return releaseInfo.getVideoSource(inferredMediaFile.getParent(), inferredMediaFile.getName(), getOriginalFileName(inferredMediaFile));
} }
@Define("tags") @Define("tags")
public List<String> getVideoTags() { public List<String> getVideoTags() {
// use inferred media file // look for video source patterns in media file and it's parent folder (use inferred media file)
File inferredMediaFile = getInferredMediaFile(); List<String> matches = releaseInfo.getVideoTags(getFileNames(getInferredMediaFile()));
// look for video source patterns in media file and it's parent folder
List<String> matches = releaseInfo.getVideoTags(inferredMediaFile.getParent(), inferredMediaFile.getName(), getOriginalFileName(inferredMediaFile));
if (matches.isEmpty()) { if (matches.isEmpty()) {
return null; return null;
} }
Set<String> tags = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); // heavy normalization for whatever text was captured with the tags pattern
for (String m : matches) { return matches.stream().map(s -> {
// heavy normalization of whatever pattern was matched with the regex pattern return lowerTrail(upperInitial(normalizePunctuation(normalizeSpace(s, " "))));
tags.add(lowerTrail(upperInitial(normalizePunctuation(normalizeSpace(m, " "))))); }).sorted().distinct().collect(toList());
}
return new ArrayList<String>(tags);
} }
@Define("s3d") @Define("s3d")
public String getStereoscopic3D() { public String getStereoscopic3D() {
return releaseInfo.getStereoscopic3D(getFileName()); return releaseInfo.getStereoscopic3D(getFileNames(getInferredMediaFile()));
} }
@Define("group") @Define("group")
public String getReleaseGroup() throws Exception { public String getReleaseGroup() throws Exception {
// use inferred media file
File inferredMediaFile = getInferredMediaFile();
// consider foldername, filename and original filename
String[] filenames = new String[] { inferredMediaFile.getParentFile().getName(), getNameWithoutExtension(inferredMediaFile.getName()), getOriginalFileName(inferredMediaFile) };
// reduce false positives by removing the know titles from the name // reduce false positives by removing the know titles from the name
Pattern nonGroupPattern = releaseInfo.getCustomRemovePattern(getKeywords()); Pattern[] nonGroupPattern = { releaseInfo.getCustomRemovePattern(getKeywords()), releaseInfo.getVideoSourcePattern(), releaseInfo.getVideoFormatPattern(true), releaseInfo.getResolutionPattern(), releaseInfo.getStructureRootPattern() };
for (int i = 0; i < filenames.length; i++) {
if (filenames[i] == null)
continue;
filenames[i] = releaseInfo.clean(filenames[i], nonGroupPattern, releaseInfo.getVideoSourcePattern(), releaseInfo.getVideoFormatPattern(true), releaseInfo.getResolutionPattern(), releaseInfo.getStructureRootPattern()); // consider foldername, filename and original filename of inferred media file
} String[] filenames = stream(getFileNames(getInferredMediaFile())).map(s -> releaseInfo.clean(s, nonGroupPattern)).toArray(String[]::new);
// look for release group names in media file and it's parent folder // look for release group names in media file and it's parent folder
return releaseInfo.getReleaseGroup(filenames); return releaseInfo.getReleaseGroup(filenames);
@ -539,8 +519,8 @@ public class MediaBindingBean {
Language language = getLanguageTag(); Language language = getLanguageTag();
if (language != null) { if (language != null) {
String tag = '.' + language.getISO3B(); String tag = '.' + language.getISO3B(); // Plex only supports ISO 639-2/B language codes
String category = releaseInfo.getSubtitleCategoryTag(FileUtilities.getName(getMediaFile()), getOriginalFileName(getMediaFile())); String category = releaseInfo.getSubtitleCategoryTag(getFileNames(getMediaFile()));
if (category != null) { if (category != null) {
return tag + '.' + category; return tag + '.' + category;
} }
@ -552,7 +532,7 @@ public class MediaBindingBean {
@Define("lang") @Define("lang")
public Language getLanguageTag() throws Exception { public Language getLanguageTag() throws Exception {
Locale languageSuffix = releaseInfo.getSubtitleLanguageTag(FileUtilities.getName(getMediaFile()), getOriginalFileName(getMediaFile())); Locale languageSuffix = releaseInfo.getSubtitleLanguageTag(getFileNames(getMediaFile()));
if (languageSuffix != null) { if (languageSuffix != null) {
return Language.getLanguage(languageSuffix); return Language.getLanguage(languageSuffix);
} }
@ -1069,9 +1049,21 @@ public class MediaBindingBean {
return getMediaInfo().snapshot().get(kind).stream().map(AssociativeScriptObject::new).collect(toList()); return getMediaInfo().snapshot().get(kind).stream().map(AssociativeScriptObject::new).collect(toList());
} }
private String getOriginalFileName(File file) { private String[] getFileNames(File file) {
String name = xattr.getOriginalName(file); List<String> names = new ArrayList<String>(3);
return name == null ? null : getNameWithoutExtension(name); names.add(getNameWithoutExtension(file.getName())); // current file name
String original = xattr.getOriginalName(file);
if (original != null) {
names.add(getNameWithoutExtension(original)); // original file name
}
File parent = file.getParentFile();
if (parent != null && parent.getParent() != null) {
names.add(parent.getName()); // current folder name
}
return names.toArray(new String[0]);
} }
private List<String> getKeywords() { private List<String> getKeywords() {