filebot/source/net/filebot/WebServices.java

271 lines
8.9 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2013-09-11 13:22:00 -04:00
import static java.util.Arrays.*;
import static java.util.Collections.*;
2014-04-19 02:30:29 -04:00
import static net.filebot.Settings.*;
import static net.filebot.media.MediaDetection.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.filebot.media.XattrMetaInfoProvider;
2014-10-29 08:55:27 -04:00
import net.filebot.similarity.MetricAvg;
2014-04-19 02:30:29 -04:00
import net.filebot.web.AcoustIDClient;
import net.filebot.web.AnidbClient;
import net.filebot.web.AnidbSearchResult;
import net.filebot.web.EpisodeListProvider;
import net.filebot.web.FanartTVClient;
import net.filebot.web.ID3Lookup;
import net.filebot.web.LocalSearch;
import net.filebot.web.MovieIdentificationService;
import net.filebot.web.MusicIdentificationService;
2014-08-05 04:23:39 -04:00
import net.filebot.web.OMDbClient;
2014-04-19 02:30:29 -04:00
import net.filebot.web.OpenSubtitlesClient;
import net.filebot.web.SearchResult;
import net.filebot.web.ShooterSubtitles;
2014-04-19 02:30:29 -04:00
import net.filebot.web.SubtitleProvider;
2015-05-11 07:42:59 -04:00
import net.filebot.web.SubtitleSearchResult;
2014-04-19 02:30:29 -04:00
import net.filebot.web.TMDbClient;
import net.filebot.web.TheTVDBClient;
import net.filebot.web.TheTVDBSearchResult;
import net.filebot.web.VideoHashSubtitleService;
/**
* Reuse the same web service client so login, cache, etc. can be shared.
*/
public final class WebServices {
// episode dbs
2014-08-08 12:45:23 -04:00
public static final AnidbClient AniDB = new AnidbClientWithLocalSearch(getApiKey("anidb"), 6);
// extended TheTVDB module with local search
2014-08-07 05:35:19 -04:00
public static final TheTVDBClientWithLocalSearch TheTVDB = new TheTVDBClientWithLocalSearch(getApiKey("thetvdb"));
2012-07-15 22:42:15 -04:00
// movie dbs
2014-08-05 04:23:39 -04:00
public static final OMDbClient OMDb = new OMDbClient();
2014-08-07 05:35:19 -04:00
public static final TMDbClient TheMovieDB = new TMDbClient(getApiKey("themoviedb"));
// subtitle dbs
public static final OpenSubtitlesClient OpenSubtitles = new OpenSubtitlesClientWithLocalSearch(getApiKey("opensubtitles"), getApplicationVersion());
public static final ShooterSubtitles Shooter = new ShooterSubtitles();
// misc
2014-08-07 05:35:19 -04:00
public static final FanartTVClient FanartTV = new FanartTVClient(Settings.getApiKey("fanart.tv"));
public static final AcoustIDClient AcoustID = new AcoustIDClient(Settings.getApiKey("acoustid"));
public static final XattrMetaInfoProvider XattrMetaData = new XattrMetaInfoProvider();
public static EpisodeListProvider[] getEpisodeListProviders() {
2015-09-24 02:45:10 -04:00
return new EpisodeListProvider[] { TheTVDB, AniDB };
}
public static MovieIdentificationService[] getMovieIdentificationServices() {
2014-08-05 04:23:39 -04:00
return new MovieIdentificationService[] { TheMovieDB, OMDb };
}
public static SubtitleProvider[] getSubtitleProviders() {
return new SubtitleProvider[] { OpenSubtitles };
}
public static VideoHashSubtitleService[] getVideoHashSubtitleServices(Locale locale) {
if (locale.equals(Locale.CHINESE))
return new VideoHashSubtitleService[] { OpenSubtitles, Shooter };
else
return new VideoHashSubtitleService[] { OpenSubtitles };
}
public static MusicIdentificationService[] getMusicIdentificationServices() {
return new MusicIdentificationService[] { AcoustID, new ID3Lookup() };
}
public static EpisodeListProvider getEpisodeListProvider(String name) {
for (EpisodeListProvider it : WebServices.getEpisodeListProviders()) {
if (it.getName().equalsIgnoreCase(name))
return it;
}
return null; // default
}
public static MovieIdentificationService getMovieIdentificationService(String name) {
for (MovieIdentificationService it : getMovieIdentificationServices()) {
if (it.getName().equalsIgnoreCase(name))
return it;
}
return null; // default
}
public static MusicIdentificationService getMusicIdentificationService(String name) {
for (MusicIdentificationService it : getMusicIdentificationServices()) {
if (it.getName().equalsIgnoreCase(name))
return it;
}
return null; // default
}
2014-11-06 08:52:16 -05:00
public static final ExecutorService requestThreadPool = Executors.newCachedThreadPool();
public static class TheTVDBClientWithLocalSearch extends TheTVDBClient {
public TheTVDBClientWithLocalSearch(String apikey) {
super(apikey);
}
// index of local thetvdb data dump
private static LocalSearch<SearchResult> localIndex;
public synchronized LocalSearch<SearchResult> getLocalIndex() throws IOException {
if (localIndex == null) {
// fetch data dump
TheTVDBSearchResult[] data = releaseInfo.getTheTVDBIndex();
// index data dump
localIndex = new LocalSearch<SearchResult>(asList(data)) {
@Override
protected Set<String> getFields(SearchResult object) {
return set(object.getEffectiveNames());
}
};
// make local search more restrictive
localIndex.setResultMinimumSimilarity(0.7f);
}
return localIndex;
}
@Override
public List<SearchResult> fetchSearchResult(final String query, final Locale locale) throws Exception {
2014-11-06 08:52:16 -05:00
Callable<List<SearchResult>> apiSearch = () -> TheTVDBClientWithLocalSearch.super.fetchSearchResult(query, locale);
Callable<List<SearchResult>> localSearch = () -> getLocalIndex().search(query);
Set<SearchResult> results = new LinkedHashSet<SearchResult>();
for (Future<List<SearchResult>> resultSet : requestThreadPool.invokeAll(asList(localSearch, apiSearch))) {
try {
results.addAll(resultSet.get());
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
}
}
}
2014-11-06 08:52:16 -05:00
return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false);
}
}
public static class AnidbClientWithLocalSearch extends AnidbClient {
public AnidbClientWithLocalSearch(String client, int clientver) {
super(client, clientver);
}
@Override
public List<AnidbSearchResult> getAnimeTitles() throws Exception {
return asList(releaseInfo.getAnidbIndex());
}
}
2014-10-29 08:55:27 -04:00
public static class OpenSubtitlesClientWithLocalSearch extends OpenSubtitlesClient {
public OpenSubtitlesClientWithLocalSearch(String name, String version) {
2014-10-29 08:55:27 -04:00
super(name, version);
}
// index of local OpenSubtitles data dump
2015-05-11 07:42:59 -04:00
private static LocalSearch<SubtitleSearchResult> localIndex;
2014-10-29 08:55:27 -04:00
2015-05-11 07:42:59 -04:00
public synchronized LocalSearch<SubtitleSearchResult> getLocalIndex() throws IOException {
if (localIndex == null) {
// fetch data dump
2015-05-11 07:42:59 -04:00
SubtitleSearchResult[] data = releaseInfo.getOpenSubtitlesIndex();
// index data dump
2015-05-11 07:42:59 -04:00
localIndex = new LocalSearch<SubtitleSearchResult>(asList(data)) {
@Override
2015-05-11 07:42:59 -04:00
protected Set<String> getFields(SubtitleSearchResult object) {
return set(object.getEffectiveNames());
2014-10-29 08:55:27 -04:00
}
};
2014-10-29 08:55:27 -04:00
}
return localIndex;
2014-10-29 08:55:27 -04:00
}
@Override
2015-05-11 07:58:31 -04:00
public synchronized List<SubtitleSearchResult> search(final String query) throws Exception {
2015-05-11 07:42:59 -04:00
List<SubtitleSearchResult> results = getLocalIndex().search(query);
2014-10-29 08:55:27 -04:00
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
2014-10-29 08:55:27 -04:00
}
2014-10-29 08:55:27 -04:00
}
/**
* Dummy constructor to prevent instantiation.
*/
private WebServices() {
throw new UnsupportedOperationException();
}
public static final String LOGIN_SEPARATOR = ":";
public static final String LOGIN_OPENSUBTITLES = "osdb.user";
/**
* Initialize client settings from system properties
*/
static {
String[] osdbLogin = getLogin(LOGIN_OPENSUBTITLES);
OpenSubtitles.setUser(osdbLogin[0], osdbLogin[1]);
}
public static String[] getLogin(String key) {
try {
String[] values = Settings.forPackage(WebServices.class).get(key, LOGIN_SEPARATOR).split(LOGIN_SEPARATOR, 2); // empty username/password by default
if (values != null && values.length == 2 && values[0] != null && values[1] != null) {
return values;
}
} catch (Exception e) {
Logger.getLogger(WebServices.class.getName()).log(Level.WARNING, e.getMessage(), e);
}
return new String[] { "", "" };
}
public static void setLogin(String id, String user, String password) {
2015-05-09 02:38:47 -04:00
// delete login
if ((user == null || user.isEmpty()) && (password == null || password.isEmpty())) {
if (LOGIN_OPENSUBTITLES.equals(id)) {
OpenSubtitles.setUser("", "");
Settings.forPackage(WebServices.class).remove(id);
} else {
throw new IllegalArgumentException();
}
} else {
2015-05-09 02:38:47 -04:00
// enter login
if (user == null || password == null || user.contains(LOGIN_SEPARATOR) || (user.isEmpty() && !password.isEmpty()) || (!user.isEmpty() && password.isEmpty())) {
throw new IllegalArgumentException(String.format("Illegal login: %s:%s", user, password));
}
if (LOGIN_OPENSUBTITLES.equals(id)) {
String password_md5 = md5(password);
OpenSubtitles.setUser(user, password_md5);
Settings.forPackage(WebServices.class).put(id, join(LOGIN_SEPARATOR, user, password_md5));
2015-05-09 02:38:47 -04:00
} else {
throw new IllegalArgumentException();
}
}
}
}