All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moviejukebox.plugin.FilmaffinityPlugin Maven / Gradle / Ivy

There is a newer version: 2.9
Show newest version
/*
 /*
 *      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;

import com.moviejukebox.model.Library;
import com.moviejukebox.model.Movie;
import com.moviejukebox.tools.HTMLTools;
import com.moviejukebox.tools.StringTools;
import com.moviejukebox.tools.SystemTools;
import com.moviejukebox.tools.WebBrowser;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class FilmaffinityPlugin extends ImdbPlugin {

    private static final Logger logger = Logger.getLogger(FilmaffinityPlugin.class);
    /*
     * Literals of web of each movie info
     */
    private static final String FA_ORIGINAL_TITLE = "TÍTULO ORIGINAL";
    private static final String FA_YEAR = "AÑO";
    private static final String FA_RUNTIME = "DURACIÓN";
    private static final String FA_DIRECTOR = "DIRECTOR";
    private static final String FA_WRITER = "GUIÓN";
    private static final String FA_CAST = "REPARTO";
    private static final String FA_GENRE = "GÉNERO";
    private static final String FA_COMPANY = "PRODUCTORA";
    private static final String FA_PLOT = "SINOPSIS";
    private FilmAffinityInfo filmAffinityInfo;

    public FilmaffinityPlugin() {
        super();  // use IMDB if FilmAffinity doesn't know movie
        filmAffinityInfo = new FilmAffinityInfo();
        webBrowser = new WebBrowser();
    }

    @Override
    public String getPluginID() {
        return FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID;
    }

    @Override
    public boolean scan(Movie movie) {
        String filmAffinityId = filmAffinityInfo.arrangeId(movie.getId(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID));

        if (StringTools.isNotValidString(filmAffinityId)) {
            filmAffinityId = filmAffinityInfo.getIdFromMovieInfo(movie.getTitle(), movie.getYear(), movie.getSeason());
        }

        if (StringTools.isValidString(filmAffinityId)) {
            movie.setId(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID, filmAffinityId);
        }

        return updateFilmAffinityMediaInfo(movie);
    }

    /**
     * Scan FilmAffinity html page for the specified movie
     */
    private boolean updateFilmAffinityMediaInfo(Movie movie) {
        Boolean returnStatus = true;
        String xml;
        String spanishTitle;
        Pattern countryPattern = Pattern.compile("").replaceAll("\\s{2,}", " ");
            if (!movie.isOverrideTitle()) {
                movie.setTitle(spanishTitle);
                movie.setOriginalTitle(HTMLTools.getTextAfterElem(xml, FA_ORIGINAL_TITLE));
            }

            movie.setYear(HTMLTools.getTextAfterElem(xml, FA_YEAR));
            // check to see if the year is numeric, if not, try a different approach
            if (!StringUtils.isNumeric(movie.getYear())) {
                movie.setYear(HTMLTools.getTextAfterElem(xml, FA_YEAR, 1));
            }

            String runTime = HTMLTools.getTextAfterElem(xml, FA_RUNTIME, 1).replace(" min.", "m");
            if (!runTime.equals("min.")) {
                movie.setRuntime(runTime);
            }

            countryMatcher = countryPattern.matcher(xml);
            if (countryMatcher.find()) {
                movie.setCountry(countryMatcher.group(1));
            }

            for (String director : HTMLTools.removeHtmlTags(HTMLTools.extractTag(xml, FA_DIRECTOR, "")).split(",")) {
                movie.addDirector(director.trim());
            }

            /*
             * Sometimes FilmAffinity includes the writer of novel in the form:
             * screenwriter (Novela: writer) OR screenwriter1, screenwriter2,
             * ... (Novela: writer) OR even!! screenwriter1, screenwriter2, ...
             * (Story: writer1, writer2)
             *
             * The info between parenthesis doesn't add.
             */
            for (String writer : HTMLTools.getTextAfterElem(xml, FA_WRITER).split("\\(")[0].split(",")) {
                movie.addWriter(writer.trim());
            }

            for (String actor : HTMLTools.removeHtmlTags(HTMLTools.extractTag(xml, FA_CAST, "")).split(",")) {
                movie.addActor(actor.trim());
            }

            // TODO: Save more than one company.
            movie.setCompany(HTMLTools.getTextAfterElem(xml, FA_COMPANY).split("/")[0].trim());

            for (String genre : HTMLTools.removeHtmlTags(HTMLTools.extractTag(xml, FA_GENRE, "")).split("\\.|\\|")) {
                movie.addGenre(Library.getIndexingGenre(cleanStringEnding(genre.trim())));
            }

            try {
                movie.addRating(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID, (int) (Float.parseFloat(HTMLTools.extractTag(xml, "", "").replace(",", ".")) * 10));
            } catch (Exception e) {
                // Don't set a rating
            }

            String plot = HTMLTools.getTextAfterElem(xml, FA_PLOT);
            if (plot.endsWith("(FILMAFFINITY)")) {
                plot = new String(plot.substring(0, plot.length() - 14));
            }
            movie.setPlot(plot.trim());

            /*
             * Fill the rest of the fields from IMDB, taking care not to allow
             * the title to get overwritten.
             *
             * I change temporally: title = Original title to improve the chance
             * to find the right movie in IMDb.
             */
            boolean overrideTitle = movie.isOverrideTitle();
            String title = movie.getTitle();
            String originalTitle = movie.getOriginalTitle();
            movie.setOverrideTitle(true);
            movie.setTitle(movie.getOriginalTitle());
            super.scan(movie);
            // Change the title back to the way it was
            movie.setTitle(title);
            movie.setOriginalTitle(originalTitle);
            movie.setOverrideTitle(overrideTitle);

        } catch (Exception error) {
            logger.error("FilmAffinity: Failed retreiving movie info: " + filmAffinityId);
            logger.error(SystemTools.getStackTrace(error));
            returnStatus = false;
        }
        return returnStatus;
    }

    @Override
    public boolean scanNFO(String nfo, Movie movie) {
        Pattern filtroFAiD = Pattern.compile("http://www.filmaffinity.com/es/film([0-9]{6})\\.html|filmaffinity=((?:film)?[0-9]{6}(?:\\.html)?)|((?:film)?[0-9]{6}(?:\\.html)?)", Pattern.CASE_INSENSITIVE);
        Matcher nfoMatcher = filtroFAiD.matcher(nfo);

        boolean result = false;
        if (nfoMatcher.find()) {
            if (nfoMatcher.group(1) != null) {
                movie.setId(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID, filmAffinityInfo.arrangeId(nfoMatcher.group(1)));
            } else if (nfoMatcher.group(2) != null) {
                movie.setId(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID, filmAffinityInfo.arrangeId(nfoMatcher.group(2)));
            } else {
                movie.setId(FilmAffinityInfo.FILMAFFINITY_PLUGIN_ID, filmAffinityInfo.arrangeId(nfoMatcher.group(3)));
            }
            result = true;
        }

        // Look for IMDb id
        super.scanNFO(nfo, movie);
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy