* parse imdb movie index from osdb

This commit is contained in:
Reinhard Pointner 2011-12-25 09:07:55 +00:00
parent 187e330131
commit 11facfa8ab
2 changed files with 72 additions and 19 deletions

View File

@ -3,24 +3,30 @@ package net.sourceforge.filebot.web;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*;
import static net.sourceforge.filebot.web.WebRequest.*;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
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.Map.Entry;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Icon;
@ -73,7 +79,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
try {
// search for movies / series
List<Movie> resultSet = xmlrpc.searchMoviesOnIMDB(query);
return Arrays.asList(resultSet.toArray(new SearchResult[0]));
return asList(resultSet.toArray(new SearchResult[0]));
} catch (ClassCastException e) {
// unexpected xmlrpc responses (e.g. error messages instead of results) will trigger this
throw new XmlRpcException("Illegal XMLRPC response on searchMoviesOnIMDB");
@ -93,7 +99,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
// get subtitle list
SubtitleDescriptor[] subtitles = xmlrpc.searchSubtitles(imdbid, languageFilter).toArray(new SubtitleDescriptor[0]);
return Arrays.asList(subtitles);
return asList(subtitles);
}
@ -333,4 +339,40 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
return null;
}
public List<Movie> exportMovie() throws IOException {
Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource");
String cacheKey = getClass().getName() + ".exportMovie";
Element element = cache.get(cacheKey);
if (element != null) {
return asList((Movie[]) element.getValue());
}
URL url = new URL("http://www.opensubtitles.org/addons/export_movie.php");
Scanner in = new Scanner(getReader(url.openConnection()));
try {
// e.g. IDMovie IDMovieImdb MovieName MovieYear
Pattern linePattern = Pattern.compile("(\\d+)\\t(\\d+)\\t([^\\t]+)\\t(\\d{4})");
List<Movie> result = new ArrayList<Movie>();
while (in.hasNextLine()) {
String line = in.nextLine().trim();
Matcher matcher = linePattern.matcher(line);
if (matcher.matches()) {
int idMovieImdb = Integer.parseInt(matcher.group(2));
String movieName = matcher.group(3);
int movieYear = Integer.parseInt(matcher.group(4));
result.add(new Movie(movieName, movieYear, idMovieImdb));
}
}
// cache data
cache.put(new Element(cacheKey, result.toArray(new Movie[0])));
return result;
} finally {
in.close();
}
}
}

View File

@ -201,4 +201,15 @@ public class OpenSubtitlesXmlRpcTest {
xmlrpc.logout();
}
@Test
public void exportMovie() throws Exception {
List<Movie> list = new OpenSubtitlesClient(null).exportMovie();
Movie sample = (Movie) list.get(17);
// check sample entry
assertEquals("Back to the Future", sample.getName());
assertEquals(1985, sample.getYear());
assertEquals(88763, sample.getImdbId());
}
}