com.moviejukebox.plugin.poster.MovieCoversPosterPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yamj Show documentation
Show all versions of yamj Show documentation
Static analysis of MovieJukebox project
/*
* Copyright (c) 2004-2012 YAMJ Members
* http://code.google.com/p/moviejukebox/people/list
*
* Web: http://code.google.com/p/moviejukebox/
*
* This software is licensed under a Creative Commons License
* See this page: http://code.google.com/p/moviejukebox/wiki/License
*
* For any reuse or distribution, you must make clear to others the
* license terms of this work.
*/
package com.moviejukebox.plugin.poster;
import com.moviejukebox.model.IImage;
import com.moviejukebox.model.Image;
import com.moviejukebox.model.Movie;
import com.moviejukebox.tools.HTMLTools;
import com.moviejukebox.tools.StringTools;
import com.moviejukebox.tools.WebBrowser;
import java.net.URLEncoder;
import java.text.Normalizer;
import org.apache.log4j.Logger;
public class MovieCoversPosterPlugin extends AbstractMoviePosterPlugin {
private static final Logger logger = Logger.getLogger(MovieCoversPosterPlugin.class);
private WebBrowser webBrowser;
public MovieCoversPosterPlugin() {
super();
// Check to see if we are needed
if (!isNeeded()) {
return;
}
webBrowser = new WebBrowser();
}
@Override
public String getIdFromMovieInfo(String title, String year) {
String returnString = Movie.UNKNOWN;
try {
StringBuilder sb = new StringBuilder("http://www.moviecovers.com/multicrit.html?titre=");
sb.append(URLEncoder.encode(title.replace("\u0153", "oe"), "iso-8859-1"));
if (StringTools.isValidString(year)) {
sb.append("&anneemin=");
sb.append(URLEncoder.encode(Integer.toString(Integer.parseInt(year) - 1), "iso-8859-1"));
sb.append("&anneemax=");
sb.append(URLEncoder.encode(Integer.toString(Integer.parseInt(year) + 1), "iso-8859-1"));
}
sb.append("&slow=0&tri=Titre&listes=1");
logger.debug("MovieCoversPosterPlugin: Searching for: " + sb.toString());
String content = webBrowser.request(sb.toString());
if (content != null) {
String formattedTitle = Normalizer.normalize(title.replace("\u0153", "oe").toUpperCase(), Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
if (formattedTitle.endsWith(" (TV)")) {
formattedTitle = new String(formattedTitle.substring(0, formattedTitle.length() - 5));
}
String formattedTitleNormalized = formattedTitle;
for (String prefix : Movie.getSortIgnorePrefixes()) {
if (formattedTitle.startsWith(prefix.toUpperCase())) {
formattedTitleNormalized = new String(formattedTitle.substring(prefix.length())) + " (" + prefix.toUpperCase().replace(" ","") + ")";
break;
}
}
// logger.debug("MovieCoversPosterPlugin: Looking for a poster for: " + formattedTitleNormalized);
// Checking for "no result" message...
if (!content.contains("/forum/index.html?forum=MovieCovers&vue=demande")) {
// There is some results
for (String filmURL : HTMLTools.extractTags(content, "", false)) {
if ( (filmURL.endsWith(formattedTitleNormalized)) || (filmURL.endsWith(formattedTitle)) ) {
returnString = HTMLTools.extractTag(filmURL, "_", ".html\">");
// logger.debug("MovieCoversPosterPlugin: Seems to find something: " + returnString + " - " + filmURL);
break;
}
}
}
// Search the forum if no answer
if (returnString.equalsIgnoreCase(Movie.UNKNOWN)) {
sb = new StringBuilder("http://www.moviecovers.com/forum/search-mysql.html?forum=MovieCovers&query=");
sb.append(URLEncoder.encode(formattedTitle, "iso-8859-1"));
// logger.debug("MovieCoversPosterPlugin: We have to explore the forums: " + sb);
content = webBrowser.request(sb.toString());
if (content != null) {
// Loop through the search results
for (String filmURL : HTMLTools.extractTags(content, "", "