* auto-detect movie info via imdb id from nfo files

This commit is contained in:
Reinhard Pointner 2011-12-05 15:38:41 +00:00
parent 9cb97bf93a
commit c2f7def29b
3 changed files with 64 additions and 35 deletions

View File

@ -259,6 +259,21 @@ public class CmdlineOperations implements CmdlineInterface {
movieFiles = subtitleFiles; movieFiles = subtitleFiles;
} }
// fill in movie information from nfo file imdb when necessary
for (int i = 0; i < movieFiles.length; i++) {
if (movieDescriptors[i] == null) {
Set<Integer> imdbid = grepImdbIdFor(movieFiles[i]);
if (imdbid.size() > 1) {
CLILogger.warning(String.format("Multiple imdb ids found for %s: %s", movieFiles[i].getName(), imdbid));
}
if (imdbid.size() == 1 || (imdbid.size() > 1 && !strict)) {
movieDescriptors[i] = db.getMovieDescriptor(imdbid.iterator().next(), locale);
CLILogger.fine(String.format("Identified %s as %s via imdb id", movieFiles[i].getName(), movieDescriptors[i]));
}
}
}
// use user query if search by hash did not return any results, only one query for one movie though // use user query if search by hash did not return any results, only one query for one movie though
if (query != null && movieDescriptors.length == 1 && movieDescriptors[0] == null) { if (query != null && movieDescriptors.length == 1 && movieDescriptors[0] == null) {
CLILogger.fine(format("Looking up movie by query [%s]", query)); CLILogger.fine(format("Looking up movie by query [%s]", query));
@ -544,7 +559,7 @@ public class CmdlineOperations implements CmdlineInterface {
SimilarityMetric sanity = EpisodeMetrics.verificationMetric(); SimilarityMetric sanity = EpisodeMetrics.verificationMetric();
for (Match<File, SubtitleDescriptor> it : matcher.match()) { for (Match<File, SubtitleDescriptor> it : matcher.match()) {
if (sanity.getSimilarity(it.getValue(), it.getCandidate()) >= 1) { if (sanity.getSimilarity(it.getValue(), it.getCandidate()) >= 0.9) {
CLILogger.finest(format("Matched [%s] to [%s] via filename", it.getValue().getName(), it.getCandidate().getName())); CLILogger.finest(format("Matched [%s] to [%s] via filename", it.getValue().getName(), it.getCandidate().getName()));
subtitleByVideo.put(it.getValue(), it.getCandidate()); subtitleByVideo.put(it.getValue(), it.getCandidate());
} }
@ -556,7 +571,20 @@ public class CmdlineOperations implements CmdlineInterface {
private Collection<String> detectQuery(Collection<File> mediaFiles, boolean strict) throws Exception { private Collection<String> detectQuery(Collection<File> mediaFiles, boolean strict) throws Exception {
Collection<String> names = detectSeriesNames(mediaFiles); Collection<String> names = new LinkedHashSet<String>();
// detect by imdb id from nfo file in the same folder
for (List<File> file : mapByFolder(mediaFiles).values()) {
for (int imdbid : grepImdbIdFor(file.get(0))) {
Movie movie = WebServices.TMDb.getMovieDescriptor(imdbid, Locale.ENGLISH);
if (movie != null) {
names.add(movie.getName());
}
}
}
// detect series name by common word sequence
names.addAll(detectSeriesNames(mediaFiles));
if (names.isEmpty() || (strict && names.size() > 1)) { if (names.isEmpty() || (strict && names.size() > 1)) {
throw new Exception("Unable to auto-select query: " + names); throw new Exception("Unable to auto-select query: " + names);

View File

@ -8,6 +8,7 @@ import static java.util.regex.Pattern.*;
import static net.sourceforge.tuned.StringUtilities.*; import static net.sourceforge.tuned.StringUtilities.*;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -15,9 +16,12 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.sourceforge.filebot.MediaTypes;
import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.similarity.SeriesNameMatcher;
import net.sourceforge.filebot.web.CachedResource; import net.sourceforge.filebot.web.CachedResource;
@ -34,6 +38,29 @@ public class ReleaseInfo {
} }
public static Set<Integer> grepImdbIdFor(File movieFile) throws IOException {
Set<Integer> collection = new LinkedHashSet<Integer>();
File movieFolder = movieFile.getParentFile(); // lookup imdb id from nfo files in this folder
for (File file : movieFolder.listFiles(MediaTypes.getDefaultFilter("application/nfo"))) {
Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8");
try {
// scan for imdb id patterns like tt1234567
String imdb = null;
while ((imdb = scanner.findWithinHorizon("(?<=tt)\\d{7}", 64 * 1024)) != null) {
collection.add(Integer.parseInt(imdb));
}
} finally {
scanner.close();
}
}
return collection;
}
public String getVideoSource(File file) { public String getVideoSource(File file) {
// check parent and itself for group names // check parent and itself for group names
return matchLast(getVideoSourcePattern(), file.getParent(), file.getName()); return matchLast(getVideoSourcePattern(), file.getParent(), file.getName());

View File

@ -5,27 +5,23 @@ package net.sourceforge.filebot.ui.rename;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.sourceforge.filebot.MediaTypes.*; import static net.sourceforge.filebot.MediaTypes.*;
import static net.sourceforge.filebot.mediainfo.ReleaseInfo.*;
import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.FileUtilities.*;
import static net.sourceforge.tuned.ui.TunedUtilities.*; import static net.sourceforge.tuned.ui.TunedUtilities.*;
import java.awt.Component; import java.awt.Component;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture; import java.util.concurrent.RunnableFuture;
@ -139,28 +135,6 @@ class MovieHashMatcher implements AutoCompleteMatcher {
} }
private Set<Integer> grepImdbId(File... files) throws IOException {
Set<Integer> collection = new HashSet<Integer>();
for (File file : files) {
Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8");
try {
// scan for imdb id patterns like tt1234567
String imdb = null;
while ((imdb = scanner.findWithinHorizon("(?<=tt)\\d{7}", 64 * 1024)) != null) {
collection.add(Integer.parseInt(imdb));
}
} finally {
scanner.close();
}
}
return collection;
}
protected Movie grabMovieName(File movieFile, Locale locale, boolean autodetect, Component parent, Movie... suggestions) throws Exception { protected Movie grabMovieName(File movieFile, Locale locale, boolean autodetect, Component parent, Movie... suggestions) throws Exception {
List<Movie> options = new ArrayList<Movie>(); List<Movie> options = new ArrayList<Movie>();
@ -172,7 +146,7 @@ class MovieHashMatcher implements AutoCompleteMatcher {
} }
// try to grep imdb id from nfo files // try to grep imdb id from nfo files
for (int imdbid : grepImdbId(movieFile.getParentFile().listFiles(getDefaultFilter("application/nfo")))) { for (int imdbid : grepImdbIdFor(movieFile)) {
Movie movie = service.getMovieDescriptor(imdbid, locale); Movie movie = service.getMovieDescriptor(imdbid, locale);
if (movie != null) { if (movie != null) {