1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-04 16:35:08 -05:00

* added preferred language support for movie mode (via TheMovieDB data source only)

This commit is contained in:
Reinhard Pointner 2011-08-10 05:05:11 +00:00
parent e90f970b80
commit f85224a8ff
6 changed files with 35 additions and 42 deletions

View File

@ -48,7 +48,7 @@ class MovieHashMatcher implements AutoCompleteMatcher {
public List<Match<File, ?>> match(final List<File> files, Locale locale) throws Exception {
// handle movie files
File[] movieFiles = filter(files, VIDEO_FILES).toArray(new File[0]);
MovieDescriptor[] movieDescriptors = service.getMovieDescriptors(movieFiles);
MovieDescriptor[] movieDescriptors = service.getMovieDescriptors(movieFiles, locale);
// map movies to (possibly multiple) files (in natural order)
Map<MovieDescriptor, SortedSet<File>> filesByMovie = new HashMap<MovieDescriptor, SortedSet<File>>();
@ -59,7 +59,7 @@ class MovieHashMatcher implements AutoCompleteMatcher {
// unknown hash, try via imdb id from nfo file
if (movie == null) {
movie = determineMovie(movieFiles[i]);
movie = determineMovie(movieFiles[i], locale);
}
// check if we managed to lookup the movie descriptor
@ -141,12 +141,12 @@ class MovieHashMatcher implements AutoCompleteMatcher {
}
protected MovieDescriptor determineMovie(File movieFile) throws Exception {
protected MovieDescriptor determineMovie(File movieFile, Locale locale) throws Exception {
List<MovieDescriptor> options = new ArrayList<MovieDescriptor>();
// try to grep imdb id from nfo files
for (int imdbid : grepImdbId(movieFile.getParentFile().listFiles(getDefaultFilter("application/nfo")))) {
MovieDescriptor movie = service.getMovieDescriptor(imdbid);
MovieDescriptor movie = service.getMovieDescriptor(imdbid, locale);
if (movie != null) {
options.add(movie);
@ -156,12 +156,12 @@ class MovieHashMatcher implements AutoCompleteMatcher {
// search by file name
if (options.isEmpty()) {
String query = getName(movieFile).replaceAll("\\p{Punct}+", " ").trim();
options = service.searchMovie(query);
options = service.searchMovie(query, locale);
// search by folder name
if (options.isEmpty()) {
query = getName(movieFile.getParentFile()).replaceAll("\\p{Punct}+", " ").trim();
options = service.searchMovie(query);
options = service.searchMovie(query, locale);
}
}

View File

@ -4,16 +4,17 @@ package net.sourceforge.filebot.web;
import java.io.File;
import java.util.List;
import java.util.Locale;
public interface MovieIdentificationService {
public List<MovieDescriptor> searchMovie(String query) throws Exception;
public List<MovieDescriptor> searchMovie(String query, Locale locale) throws Exception;
public MovieDescriptor getMovieDescriptor(int imdbid) throws Exception;
public MovieDescriptor getMovieDescriptor(int imdbid, Locale locale) throws Exception;
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles) throws Exception;
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception;
}

View File

@ -12,6 +12,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
@ -147,7 +148,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
@Override
public List<MovieDescriptor> searchMovie(String query) throws Exception {
public List<MovieDescriptor> searchMovie(String query, Locale locale) throws Exception {
// require login
login();
@ -156,7 +157,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
@Override
public MovieDescriptor getMovieDescriptor(int imdbid) throws Exception {
public MovieDescriptor getMovieDescriptor(int imdbid, Locale locale) throws Exception {
// require login
login();
@ -165,7 +166,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
@Override
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles) throws Exception {
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception {
// create result array
MovieDescriptor[] result = new MovieDescriptor[movieFiles.length];

View File

@ -45,18 +45,7 @@ public class OpenSubtitlesXmlRpc {
* Login as anonymous user
*/
public void loginAnonymous() throws XmlRpcFault {
login("", "");
}
/**
* Login as user and use English as language
*
* @param username username (blank for anonymous user)
* @param password password (blank for anonymous user)
*/
public void login(String username, String password) throws XmlRpcFault {
login(username, password, "en");
login("", "", "en");
}

View File

@ -12,6 +12,7 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -48,9 +49,9 @@ public class TMDbClient implements MovieIdentificationService {
@Override
public List<MovieDescriptor> searchMovie(String query) throws IOException {
public List<MovieDescriptor> searchMovie(String query, Locale locale) throws IOException {
try {
return getMovies("Movie.search", query);
return getMovies("Movie.search", query, locale);
} catch (SAXException e) {
// TMDb output is sometimes malformed xml
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage());
@ -59,19 +60,19 @@ public class TMDbClient implements MovieIdentificationService {
}
public List<MovieDescriptor> searchMovie(File file) throws IOException, SAXException {
return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length());
public List<MovieDescriptor> searchMovie(File file, Locale locale) throws IOException, SAXException {
return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length(), locale);
}
public List<MovieDescriptor> searchMovie(String hash, long bytesize) throws IOException, SAXException {
return getMovies("Media.getInfo", hash + "/" + bytesize);
public List<MovieDescriptor> searchMovie(String hash, long bytesize, Locale locale) throws IOException, SAXException {
return getMovies("Media.getInfo", hash + "/" + bytesize, locale);
}
@Override
public MovieDescriptor getMovieDescriptor(int imdbid) throws Exception {
URL resource = getResource("Movie.imdbLookup", String.format("tt%07d", imdbid));
public MovieDescriptor getMovieDescriptor(int imdbid, Locale locale) throws Exception {
URL resource = getResource("Movie.imdbLookup", String.format("tt%07d", imdbid), locale);
Node movie = selectNode("//movie", getDocument(resource));
if (movie == null)
@ -85,11 +86,11 @@ public class TMDbClient implements MovieIdentificationService {
@Override
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles) throws Exception {
public MovieDescriptor[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception {
MovieDescriptor[] movies = new MovieDescriptor[movieFiles.length];
for (int i = 0; i < movies.length; i++) {
List<MovieDescriptor> options = searchMovie(movieFiles[i]);
List<MovieDescriptor> options = searchMovie(movieFiles[i], locale);
// just use first result, if possible
movies[i] = options.isEmpty() ? null : options.get(0);
@ -99,10 +100,10 @@ public class TMDbClient implements MovieIdentificationService {
}
protected List<MovieDescriptor> getMovies(String method, String parameter) throws IOException, SAXException {
protected List<MovieDescriptor> getMovies(String method, String parameter, Locale locale) throws IOException, SAXException {
List<MovieDescriptor> result = new ArrayList<MovieDescriptor>();
for (Node node : selectNodes("//movie", getDocument(getResource(method, parameter)))) {
for (Node node : selectNodes("//movie", getDocument(getResource(method, parameter, locale)))) {
try {
String name = getTextContent("name", node);
@ -122,9 +123,9 @@ public class TMDbClient implements MovieIdentificationService {
}
protected URL getResource(String method, String parameter) throws MalformedURLException {
protected URL getResource(String method, String parameter, Locale locale) throws MalformedURLException {
// e.g. http://api.themoviedb.org/2.1/Movie.search/en/xml/{apikey}/serenity
return new URL("http", host, "/" + version + "/" + method + "/en/xml/" + apikey + "/" + parameter);
return new URL("http", host, "/" + version + "/" + method + "/" + locale.getLanguage() + "/xml/" + apikey + "/" + parameter);
}
}

View File

@ -6,6 +6,7 @@ import static net.sourceforge.filebot.Settings.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Locale;
import org.junit.Test;
@ -17,10 +18,10 @@ public class TMDbClientTest {
@Test
public void searchByName() throws Exception {
List<MovieDescriptor> result = tmdb.searchMovie("Serenity");
List<MovieDescriptor> result = tmdb.searchMovie("Serenity", Locale.CHINESE);
MovieDescriptor movie = result.get(0);
assertEquals("Serenity", movie.getName());
assertEquals("冲出宁静号", movie.getName());
assertEquals(2005, movie.getYear());
assertEquals(379786, movie.getImdbId());
}
@ -28,7 +29,7 @@ public class TMDbClientTest {
@Test
public void searchByHash() throws Exception {
List<MovieDescriptor> results = tmdb.searchMovie("907172e7fe51ba57", 742086656);
List<MovieDescriptor> results = tmdb.searchMovie("907172e7fe51ba57", 742086656, Locale.ENGLISH);
MovieDescriptor movie = results.get(0);
assertEquals("Sin City", movie.getName());
@ -39,7 +40,7 @@ public class TMDbClientTest {
@Test
public void searchByIMDB() throws Exception {
MovieDescriptor movie = tmdb.getMovieDescriptor(418279);
MovieDescriptor movie = tmdb.getMovieDescriptor(418279, Locale.ENGLISH);
assertEquals("Transformers", movie.getName());
assertEquals(2007, movie.getYear());