// can't use parallel stream because default fork/join pool doesn't play well with the security manager

This commit is contained in:
Reinhard Pointner 2016-04-05 22:21:16 +00:00
parent 419b71b08e
commit aa06ecbabd
1 changed files with 54 additions and 50 deletions

View File

@ -6,6 +6,7 @@ import static java.util.regex.Pattern.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.Settings.*;
import static net.filebot.media.MediaDetection.*;
import static net.filebot.similarity.Normalization.*;
import static net.filebot.util.FileUtilities.*;
@ -24,6 +25,8 @@ import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.regex.Pattern;
@ -78,9 +81,24 @@ public class AutoDetection {
// sort keys and values
Map<Group, Set<File>> groups = new TreeMap<Group, Set<File>>();
stream(files).parallel().collect(groupingByConcurrent(f -> {
Group group = new Group();
// can't use parallel stream because default fork/join pool doesn't play well with the security manager
ExecutorService executor = Executors.newFixedThreadPool(getPreferredThreadPoolSize());
stream(files).collect(toMap(f -> f, f -> executor.submit(() -> detectGroup(f)))).forEach((file, group) -> {
try {
groups.computeIfAbsent(group.get(), k -> new TreeSet<File>()).add(file);
} catch (Exception e) {
debug.log(Level.SEVERE, e.getMessage(), e);
}
});
executor.shutdown();
return groups;
}
private Group detectGroup(File f) throws Exception {
Group group = new Group();
if (forceIgnore.test(f)) {
return group;
}
@ -96,38 +114,6 @@ public class AutoDetection {
if (forceAnime.test(f)) {
return group.anime(getSeriesMatches(f, true));
}
return detectGroup(f);
} catch (Exception e) {
debug.log(Level.SEVERE, e.getMessage(), e);
}
return group;
})).forEach((k, v) -> groups.put(k, new TreeSet<File>(v)));
return groups;
}
private List<String> getSeriesMatches(File f, boolean anime) throws Exception {
List<String> names = detectSeriesNames(singleton(f), anime, locale);
if (names.size() > 0) {
return names;
}
List<File> episodes = getVideoFiles(f.getParentFile());
if (episodes.size() >= 5) {
return detectSeriesNames(episodes, anime, locale);
}
return emptyList();
}
private List<Movie> getMovieMatches(File file, boolean strict) throws Exception {
return MediaDetection.detectMovie(file, WebServices.TheMovieDB, locale, strict);
}
private List<File> getVideoFiles(File parent) {
return stream(files).filter(it -> parent.equals(it.getParentFile())).filter(VIDEO_FILES::accept).collect(toList());
}
private Group detectGroup(File f) throws Exception {
Group group = new Group();
List<String> s = getSeriesMatches(f, false);
List<Movie> m = getMovieMatches(f, false);
@ -140,10 +126,28 @@ public class AutoDetection {
return group.movie(m);
log.fine(format("%s [series: %s, movie: %s]", f.getName(), s.get(0), m.get(0)));
return new Rules(f, s, m).apply();
}
private List<String> getSeriesMatches(File f, boolean anime) throws Exception {
List<String> names = detectSeriesNames(singleton(f), anime, locale);
if (names.isEmpty()) {
List<File> episodes = getVideoFiles(f.getParentFile());
if (episodes.size() >= 5) {
names = detectSeriesNames(episodes, anime, locale);
}
}
return names;
}
private List<Movie> getMovieMatches(File file, boolean strict) throws Exception {
return MediaDetection.detectMovie(file, WebServices.TheMovieDB, locale, strict);
}
private List<File> getVideoFiles(File parent) {
return stream(files).filter(it -> parent.equals(it.getParentFile())).filter(VIDEO_FILES::accept).collect(toList());
}
private static final Pattern YEAR = Pattern.compile("\\D(?:19|20)\\d{2}\\D");
private static final Pattern EPISODE_NUMBERS = Pattern.compile("\\b\\d{1,3}\\b");
private static final Pattern DASH = Pattern.compile("^.{0,3}\\s[-]\\s.+$", UNICODE_CHARACTER_CLASS);
@ -319,7 +323,7 @@ public class AutoDetection {
}
public enum Type {
Movie, Series, Anime, Music;
MOVIE, SERIES, ANIME, MUSIC;
}
public static class Group extends EnumMap<Type, Object> implements Comparable<Group> {
@ -329,40 +333,40 @@ public class AutoDetection {
}
public Object getMovie() {
return get(Type.Movie);
return get(Type.MOVIE);
}
public Object getSeries() {
return get(Type.Series);
return get(Type.SERIES);
}
public Object getAnime() {
return get(Type.Anime);
return get(Type.ANIME);
}
public Object getMusic() {
return get(Type.Music);
return get(Type.MUSIC);
}
public Group movie(List<Movie> movies) {
put(Type.Movie, movies == null || movies.isEmpty() ? null : movies.get(0));
put(Type.MOVIE, movies == null || movies.isEmpty() ? null : movies.get(0));
return this;
}
public Group series(List<String> names) {
put(Type.Series, names == null || names.isEmpty() ? null : names.get(0));
put(Type.SERIES, names == null || names.isEmpty() ? null : names.get(0));
return this;
}
public Group anime(List<String> names) {
put(Type.Anime, names == null || names.isEmpty() ? null : names.get(0));
put(Type.ANIME, names == null || names.isEmpty() ? null : names.get(0));
return this;
}
public Group music(File f) {
put(Type.Music, f == null ? null : f.getParent());
put(Type.MUSIC, f == null ? null : f.getParent());
return this;
}