public class MovieMeterPosterPlugin extends AbstractMoviePosterPlugin {

  private static final Logger LOG = LoggerFactory.getLogger(MovieMeterPosterPlugin.class);
  private static final String API_KEY = PropertiesUtil.getProperty("API_KEY_MovieMeter");
  private static MovieMeterApi api;

  public MovieMeterPosterPlugin() {
    super();

    // Check to see if we are needed
    if (!isNeeded()) {
      return;
    }

    try {
      api = new MovieMeterApi(API_KEY, WebBrowser.getHttpClient());
    } catch (MovieMeterException ex) {
      LOG.warn("Failed to initialise MovieMeter API: {}", ex.getMessage(), ex);
      return;
    }

    PropertiesUtil.warnDeprecatedProperty("moviemeter.id.search");
  }

  @Override
  public String getIdFromMovieInfo(String title, String year) {
    String id = UNKNOWN;

    LOG.debug("Looking for MovieMeter ID for {} ({})", title, year);
    List<SearchResult> results;
    try {
      results = api.search(title);
    } catch (MovieMeterException ex) {
      LOG.warn(
          "Failed to get Movie Meter search results for {} ({}): {}",
          title,
          year,
          ex.getMessage(),
          ex);
      return id;
    }

    if (results.isEmpty()) {
      return id;
    }

    int fYear = NumberUtils.toInt(year, 0);
    double maxMatch = 0.0;

    for (SearchResult sr : results) {
      // if we have a year, check that first
      if (fYear > 0 && sr.getYear() != fYear) {
        continue;
      }

      // Check for best text similarity
      double result = StringUtils.getJaroWinklerDistance(title, sr.getTitle());
      if (result > maxMatch) {
        LOG.trace(
            "Better match found for {} ({}) = {} ({}) [{}]",
            title,
            year,
            sr.getTitle(),
            sr.getYear(),
            maxMatch);
        maxMatch = result;
        // Update the best result
        id = Integer.toString(sr.getId());
      }
    }

    if (isValidString(id)) {
      LOG.debug(
          "MovieMeter ID '{}' found for {} ({}), Match confidence: {}", id, title, year, maxMatch);
    }

    return id;
  }

  @Override
  public IImage getPosterUrl(String id) {
    if (!StringUtils.isNumeric(id)) {
      return Image.UNKNOWN;
    }

    String posterURL = Movie.UNKNOWN;
    FilmInfo filmInfo;

    try {
      filmInfo = api.getFilm(NumberUtils.toInt(id));
      if (filmInfo.getPosters() == null || filmInfo.getPosters().getLarge() == null) {
        LOG.debug("No MovieMeter Poster URL for movie: {}", id);
      } else {
        posterURL = filmInfo.getPosters().getLarge();
      }
    } catch (MovieMeterException ex) {
      LOG.error("Failed retreiving MovieMeter Poster URL for movie: {}", id);
      LOG.error(SystemTools.getStackTrace(ex));
    }

    if (StringTools.isValidString(posterURL)) {
      return new Image(posterURL);
    }
    return Image.UNKNOWN;
  }

  @Override
  public IImage getPosterUrl(String title, String year) {
    return getPosterUrl(getIdFromMovieInfo(title, year));
  }

  @Override
  public String getName() {
    return "moviemeterposter";
  }
}
/**
 * Film Katalogus Plugin for Hungarian language
 *
 * <p>Contains code for an alternate plugin for fetching information on movies in Hungarian
 *
 * @author [email protected]
 */
public class FilmKatalogusPlugin extends ImdbPlugin {

  private static final Logger LOG = LoggerFactory.getLogger(FilmKatalogusPlugin.class);
  public static final String FILMKAT_PLUGIN_ID = "filmkatalogus";
  private final TheTvDBPlugin tvdb;
  private static final String mjbProxyHost = PropertiesUtil.getProperty("mjb.ProxyHost");
  private static final String mjbProxyPort = PropertiesUtil.getProperty("mjb.ProxyPort");

  public FilmKatalogusPlugin() {
    super(); // use IMDB as basis
    tvdb = new TheTvDBPlugin();
  }

  @Override
  public String getPluginID() {
    return FILMKAT_PLUGIN_ID;
  }

  @Override
  public boolean scan(Movie mediaFile) {
    boolean result;

    result = super.scan(mediaFile); // use IMDB as basis
    if (!result && mediaFile.isTVShow()) {
      result = tvdb.scan(mediaFile);
    }

    // check if title or plot should be retrieved
    if (OverrideTools.checkOneOverwrite(
        mediaFile, FILMKAT_PLUGIN_ID, OverrideFlag.TITLE, OverrideFlag.PLOT)) {
      LOG.info("Id found in nfo = {}", mediaFile.getId(FilmKatalogusPlugin.FILMKAT_PLUGIN_ID));
      getHunPlot(mediaFile);
    }

    return result;
  }

  private void getHunPlot(Movie movie) {
    String filmKatURL = "http://filmkatalogus.hu";

    try {

      if (StringTools.isNotValidString(movie.getId(FILMKAT_PLUGIN_ID))) {

        LOG.debug("Movie title for filmkatalogus search = {}", movie.getTitle());
        HttpClient httpClient = new DefaultHttpClient();
        // httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        httpClient
            .getParams()
            .setParameter(
                "http.useragent",
                "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)"); // User-Agent header should be
                                                                  // overwrittem with somehting
                                                                  // Apache is not accepted by
                                                                  // filmkatalogus.hu

        if (mjbProxyHost != null) {
          HttpHost proxy = new HttpHost(mjbProxyHost, Integer.parseInt(mjbProxyPort));
          httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }

        HttpPost httppost = new HttpPost("http://www.filmkatalogus.hu/kereses");

        List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("gyorskeres", "0"));
        nameValuePairs.add(new BasicNameValuePair("keres0", "1"));
        nameValuePairs.add(new BasicNameValuePair("szo0", movie.getTitle()));

        httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
        httppost.addHeader("Accept", "text/plain");

        try {
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "ISO-8859-2"));
        } catch (UnsupportedEncodingException ex) {
          // writing error to Log
          LOG.error(SystemTools.getStackTrace(ex));
          return;
        }

        try {
          HttpResponse response = httpClient.execute(httppost);

          switch (response.getStatusLine().getStatusCode()) {
            case 302:
              filmKatURL = filmKatURL.concat(response.getHeaders("location")[0].getValue());
              LOG.debug("FilmkatalogusURL = {}", filmKatURL);
              break;

            case 200:
              HttpEntity body = response.getEntity();
              if (body == null) {
                return;
              }

              String xml = EntityUtils.toString(body);

              int beginIndex = xml.indexOf("Találat(ok) filmek között");
              if (beginIndex != -1) { // more then one entry found use the first one
                beginIndex = xml.indexOf("HREF='/", beginIndex);
                int endIndex = xml.indexOf("TITLE", beginIndex);
                filmKatURL = "http://filmkatalogus.hu";
                filmKatURL = filmKatURL.concat(xml.substring((beginIndex + 6), endIndex - 2));
                LOG.debug("FilmkatalogusURL = {}", filmKatURL);
              } else {
                return;
              }

              break;

            default:
              return;
          }

        } catch (ClientProtocolException ex) {
          // writing exception to log
          LOG.error(SystemTools.getStackTrace(ex));
          return;
        } catch (IOException ex) {
          // writing exception to log
          LOG.error(SystemTools.getStackTrace(ex));
          return;
        }
      } else {
        filmKatURL = "http://filmkatalogus.hu/f";
        filmKatURL = filmKatURL.concat(movie.getId(FilmKatalogusPlugin.FILMKAT_PLUGIN_ID));
        LOG.debug("FilmkatalogusURL = {}", filmKatURL);
      }

      String xml = webBrowser.request(filmKatURL);

      // name
      int beginIndex = xml.indexOf("<H1>");
      if (beginIndex > 0) { // exact match is found
        if (OverrideTools.checkOverwriteTitle(movie, FILMKAT_PLUGIN_ID)) {
          int endIndex = xml.indexOf("</H1>", beginIndex);
          movie.setTitle(xml.substring((beginIndex + 4), endIndex), FILMKAT_PLUGIN_ID);
          LOG.trace("Title: {}", movie.getTitle());
        }

        // PLOT
        if (OverrideTools.checkOverwritePlot(movie, FILMKAT_PLUGIN_ID)) {
          beginIndex = xml.indexOf("<DIV ALIGN=JUSTIFY>", beginIndex);
          if (beginIndex > 0) {
            int endIndex = xml.indexOf("</DIV>", beginIndex);
            String plot = xml.substring((beginIndex + 19), endIndex);
            movie.setPlot(plot, FILMKAT_PLUGIN_ID);
            LOG.trace("Plot: {}", movie.getPlot());
          }
        }
      }
    } catch (NumberFormatException | ParseException | IOException error) {
      LOG.error("Failed retreiving information for {}", movie.getTitle());
      LOG.error(SystemTools.getStackTrace(error));
    }
  }

  @Override
  public boolean scanNFO(String nfo, Movie movie) {
    super.scanNFO(nfo, movie); // use IMDB as basis

    boolean result = false;
    int beginIndex = nfo.indexOf("filmkatalogus.hu/");
    if (beginIndex != -1) {
      beginIndex = nfo.indexOf("--f", beginIndex);
      if (beginIndex != -1) {
        StringTokenizer filmKatID =
            new StringTokenizer(nfo.substring(beginIndex + 3), "/ \n,:!&é\"'(--è_çà)=$<>");
        movie.setId(FilmKatalogusPlugin.FILMKAT_PLUGIN_ID, filmKatID.nextToken());
        LOG.debug("ID found in NFO = {}", movie.getId(FilmKatalogusPlugin.FILMKAT_PLUGIN_ID));
        result = true;
      }
    }
    return result;
  }
}