* refactor

This commit is contained in:
Reinhard Pointner 2015-05-11 11:42:59 +00:00
parent c21e544e08
commit 97c9643871
7 changed files with 38 additions and 32 deletions

View File

@ -32,9 +32,9 @@ import net.filebot.web.MovieIdentificationService;
import net.filebot.web.MusicIdentificationService;
import net.filebot.web.OMDbClient;
import net.filebot.web.OpenSubtitlesClient;
import net.filebot.web.OpenSubtitlesSearchResult;
import net.filebot.web.SearchResult;
import net.filebot.web.SubtitleProvider;
import net.filebot.web.SubtitleSearchResult;
import net.filebot.web.TMDbClient;
import net.filebot.web.TVRageClient;
import net.filebot.web.TheTVDBClient;
@ -179,18 +179,18 @@ public final class WebServices {
}
// index of local OpenSubtitles data dump
private static LocalSearch<SearchResult> localIndex;
private static LocalSearch<SubtitleSearchResult> localIndex;
public synchronized LocalSearch<SearchResult> getLocalIndex() throws IOException {
public synchronized LocalSearch<SubtitleSearchResult> getLocalIndex() throws IOException {
if (localIndex == null) {
// fetch data dump
OpenSubtitlesSearchResult[] data = releaseInfo.getOpenSubtitlesIndex();
SubtitleSearchResult[] data = releaseInfo.getOpenSubtitlesIndex();
// index data dump
localIndex = new LocalSearch<SearchResult>(asList(data)) {
localIndex = new LocalSearch<SubtitleSearchResult>(asList(data)) {
@Override
protected Set<String> getFields(SearchResult object) {
protected Set<String> getFields(SubtitleSearchResult object) {
return set(object.getEffectiveNames());
}
};
@ -200,8 +200,8 @@ public final class WebServices {
}
@Override
public synchronized List<SearchResult> search(final String query, final boolean byMovie, final boolean bySeries) throws Exception {
List<SearchResult> results = getLocalIndex().search(query);
public synchronized List<SubtitleSearchResult> search(final String query, final boolean byMovie, final boolean bySeries) throws Exception {
List<SubtitleSearchResult> results = getLocalIndex().search(query);
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
}

View File

@ -36,7 +36,7 @@ import net.filebot.util.FileUtilities.RegexFileFilter;
import net.filebot.web.AnidbSearchResult;
import net.filebot.web.CachedResource;
import net.filebot.web.Movie;
import net.filebot.web.OpenSubtitlesSearchResult;
import net.filebot.web.SubtitleSearchResult;
import net.filebot.web.TheTVDBSearchResult;
import org.tukaani.xz.XZInputStream;
@ -306,7 +306,7 @@ public class ReleaseInfo {
return anidbIndexResource.get();
}
public OpenSubtitlesSearchResult[] getOpenSubtitlesIndex() throws IOException {
public SubtitleSearchResult[] getOpenSubtitlesIndex() throws IOException {
return osdbIndexResource.get();
}
@ -354,7 +354,7 @@ public class ReleaseInfo {
protected final CachedResource<String[]> seriesDirectMappingsResource = new PatternResource(getProperty("url.series-mappings"));
protected final CachedResource<TheTVDBSearchResult[]> tvdbIndexResource = new TheTVDBIndexResource(getProperty("url.thetvdb-index"));
protected final CachedResource<AnidbSearchResult[]> anidbIndexResource = new AnidbIndexResource(getProperty("url.anidb-index"));
protected final CachedResource<OpenSubtitlesSearchResult[]> osdbIndexResource = new OpenSubtitlesIndexResource(getProperty("url.osdb-index"));
protected final CachedResource<SubtitleSearchResult[]> osdbIndexResource = new OpenSubtitlesIndexResource(getProperty("url.osdb-index"));
protected String getProperty(String propertyName) {
// allow override via Java System properties
@ -441,16 +441,16 @@ public class ReleaseInfo {
}
}
protected static class OpenSubtitlesIndexResource extends CachedResource<OpenSubtitlesSearchResult[]> {
protected static class OpenSubtitlesIndexResource extends CachedResource<SubtitleSearchResult[]> {
public OpenSubtitlesIndexResource(String resource) {
super(resource, OpenSubtitlesSearchResult[].class, ONE_MONTH); // check for updates every month
super(resource, SubtitleSearchResult[].class, ONE_MONTH); // check for updates every month
}
@Override
public OpenSubtitlesSearchResult[] process(ByteBuffer data) throws IOException {
public SubtitleSearchResult[] process(ByteBuffer data) throws IOException {
List<String[]> rows = readCSV(new XZInputStream(new ByteBufferInputStream(data)), "UTF-8", "\t");
List<OpenSubtitlesSearchResult> result = new ArrayList<OpenSubtitlesSearchResult>(rows.size());
List<SubtitleSearchResult> result = new ArrayList<SubtitleSearchResult>(rows.size());
for (String[] row : rows) {
int imdbid = parseInt(row[0]);
@ -458,10 +458,10 @@ public class ReleaseInfo {
int year = parseInt(row[2]);
char kind = row[3].charAt(0);
int score = parseInt(row[4]);
result.add(new OpenSubtitlesSearchResult(imdbid, name, year, kind, score));
result.add(new SubtitleSearchResult(imdbid, name, year, kind, score));
}
return result.toArray(new OpenSubtitlesSearchResult[0]);
return result.toArray(new SubtitleSearchResult[0]);
}
}

View File

@ -42,9 +42,9 @@ import net.filebot.similarity.SimilarityMetric;
import net.filebot.vfs.ArchiveType;
import net.filebot.vfs.MemoryFile;
import net.filebot.web.Movie;
import net.filebot.web.SearchResult;
import net.filebot.web.SubtitleDescriptor;
import net.filebot.web.SubtitleProvider;
import net.filebot.web.SubtitleSearchResult;
public final class SubtitleUtilities {
@ -194,28 +194,28 @@ public final class SubtitleUtilities {
Set<SubtitleDescriptor> subtitles = new LinkedHashSet<SubtitleDescriptor>();
// search for and automatically select movie / show entry
Set<SearchResult> resultSet = new HashSet<SearchResult>();
Set<SubtitleSearchResult> resultSet = new HashSet<SubtitleSearchResult>();
for (String query : querySet) {
resultSet.addAll(findProbableSearchResults(query, service.search(query, searchByMovie, searchBySeries), querySet.size() == 1 ? 4 : 2));
}
// fetch subtitles for all search results
for (SearchResult it : resultSet) {
for (SubtitleSearchResult it : resultSet) {
subtitles.addAll(service.getSubtitleList(it, languageName));
}
return subtitles;
}
protected static Collection<SearchResult> findProbableSearchResults(String query, Iterable<? extends SearchResult> searchResults, int limit) {
protected static Collection<SubtitleSearchResult> findProbableSearchResults(String query, Iterable<SubtitleSearchResult> searchResults, int limit) {
// auto-select most probable search result
Set<SearchResult> probableMatches = new LinkedHashSet<SearchResult>();
Set<SubtitleSearchResult> probableMatches = new LinkedHashSet<SubtitleSearchResult>();
// use name similarity metric
SimilarityMetric metric = new MetricAvg(new SequenceMatchSimilarity(), new NameSimilarityMetric());
// find probable matches using name similarity > threshold
for (SearchResult result : searchResults) {
for (SubtitleSearchResult result : searchResults) {
if (probableMatches.size() <= limit) {
if (metric.getSimilarity(query, removeTrailingBrackets(result.getName())) > 0.8f || result.getName().toLowerCase().startsWith(query.toLowerCase())) {
probableMatches.add(result);

View File

@ -50,6 +50,7 @@ import net.filebot.web.OpenSubtitlesClient;
import net.filebot.web.SearchResult;
import net.filebot.web.SubtitleDescriptor;
import net.filebot.web.SubtitleProvider;
import net.filebot.web.SubtitleSearchResult;
import net.filebot.web.VideoHashSubtitleService;
import net.miginfocom.swing.MigLayout;
@ -218,10 +219,15 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
}
@Override
public Collection<SearchResult> search() throws Exception {
public Collection<SubtitleSearchResult> search() throws Exception {
return request.getProvider().search(request.getSearchText(), true, true);
}
@Override
public SubtitleSearchResult getSearchResult() {
return (SubtitleSearchResult) super.getSearchResult();
}
@Override
public Collection<SubtitlePackage> fetch() throws Exception {
List<SubtitlePackage> packages = new ArrayList<SubtitlePackage>();

View File

@ -88,12 +88,12 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
}
@Override
public synchronized List<SearchResult> search(String query, boolean byMovie, boolean bySeries) throws Exception {
public synchronized List<SubtitleSearchResult> search(String query, boolean byMovie, boolean bySeries) throws Exception {
throw new UnsupportedOperationException(); // XMLRPC::SearchMoviesOnIMDB is not allowed due to abuse
}
@Override
public synchronized List<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, String languageName) throws Exception {
public synchronized List<SubtitleDescriptor> getSubtitleList(SubtitleSearchResult searchResult, String languageName) throws Exception {
List<SubtitleDescriptor> subtitles = getCache().getSubtitleDescriptorList(searchResult, languageName);
if (subtitles != null) {
return subtitles;
@ -452,7 +452,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
}
@Override
public URI getSubtitleListLink(SearchResult searchResult, String languageName) {
public URI getSubtitleListLink(SubtitleSearchResult searchResult, String languageName) {
Movie movie = (Movie) searchResult;
String sublanguageid = "all";

View File

@ -7,11 +7,11 @@ import javax.swing.Icon;
public interface SubtitleProvider {
public List<SearchResult> search(String query, boolean byMovie, boolean bySeries) throws Exception;
public List<SubtitleSearchResult> search(String query, boolean byMovie, boolean bySeries) throws Exception;
public List<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, String languageName) throws Exception;
public List<SubtitleDescriptor> getSubtitleList(SubtitleSearchResult searchResult, String languageName) throws Exception;
public URI getSubtitleListLink(SearchResult searchResult, String languageName);
public URI getSubtitleListLink(SubtitleSearchResult searchResult, String languageName);
public String getName();

View File

@ -2,7 +2,7 @@ package net.filebot.web;
import java.util.Locale;
public class OpenSubtitlesSearchResult extends Movie {
public class SubtitleSearchResult extends Movie {
public static final char KIND_MOVIE = 'm';
public static final char KIND_SERIES = 's';
@ -10,7 +10,7 @@ public class OpenSubtitlesSearchResult extends Movie {
private char kind;
private int score;
public OpenSubtitlesSearchResult(int imdbId, String name, int year, char kind, int score) {
public SubtitleSearchResult(int imdbId, String name, int year, char kind, int score) {
super(name, null, year, imdbId, -1, Locale.ENGLISH);
this.kind = kind;