com.moviejukebox.plugin.KinopoiskPlugin 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.
*/
/*
Plugin to retrieve movie data from Russian movie database www.kinopoisk.ru
Written by Yury Sidorov.
First the movie data is searched in IMDB and TheTvDB.
After that the movie is searched in kinopoisk and movie data
is updated.
It is possible to specify URL of the movie page on kinopoisk in
the .nfo file. In this case movie data will be retrieved from this page only.
*/
package com.moviejukebox.plugin;
import com.moviejukebox.model.*;
import com.moviejukebox.tools.*;
import static com.moviejukebox.tools.PropertiesUtil.FALSE;
import static com.moviejukebox.tools.PropertiesUtil.TRUE;
import java.io.File;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class KinopoiskPlugin extends ImdbPlugin {
private static final Logger logger = Logger.getLogger(KinopoiskPlugin.class);
private static final String logMessage = "KinopoiskPlugin: ";
public static String KINOPOISK_PLUGIN_ID = "kinopoisk";
// Define plot length
private int preferredPlotLength = PropertiesUtil.getIntProperty("plugin.plot.maxlength", "500");
@Deprecated
private String preferredRating = PropertiesUtil.getProperty("kinopoisk.rating", "imdb");
private TheTvDBPlugin tvdb;
// Shows what name is on the first position with respect to divider
private String titleLeader = PropertiesUtil.getProperty("kinopoisk.title.leader", "english");
private String titleDivider = PropertiesUtil.getProperty("kinopoisk.title.divider", " - ");
private boolean joinTitles = PropertiesUtil.getBooleanProperty("kinopoisk.title.join", TRUE);
// Set NFO information priority
private boolean nfoPriority = PropertiesUtil.getBooleanProperty("kinopoisk.NFOpriority", FALSE);
private boolean nfoPlot = false;
private boolean nfoCast = false;
private boolean nfoGenres = false;
private boolean nfoDirectors = false;
private boolean nfoWriters = false;
private boolean nfoCertification = false;
private boolean nfoCountry = false;
private String nfoYear = "";
private boolean nfoTagline = false;
private boolean nfoRating = false;
private boolean nfoTop250 = false;
private boolean nfoCompany = false;
private boolean nfoRelease = false;
private boolean nfoFanart = false;
private boolean nfoPoster = false;
private boolean nfoAwards = false;
private String tmpAwards = PropertiesUtil.getProperty("mjb.scrapeAwards", FALSE);
private boolean scrapeWonAwards = tmpAwards.equalsIgnoreCase("won");
private boolean scrapeAwards = tmpAwards.equalsIgnoreCase(TRUE) || scrapeWonAwards;
private boolean scrapeBusiness = PropertiesUtil.getBooleanProperty("mjb.scrapeBusiness", FALSE);
private boolean scrapeTrivia = PropertiesUtil.getBooleanProperty("mjb.scrapeTrivia", FALSE);
// Set priority fanart & poster by kinopoisk.ru
private boolean fanArt = PropertiesUtil.getBooleanProperty("kinopoisk.fanart", FALSE);
private boolean poster = PropertiesUtil.getBooleanProperty("kinopoisk.poster", FALSE);
private boolean kadr = PropertiesUtil.getBooleanProperty("kinopoisk.kadr", FALSE);
private boolean companyAll = PropertiesUtil.getProperty("kinopoisk.company", "first").equalsIgnoreCase("all");
private boolean countryAll = PropertiesUtil.getProperty("kinopoisk.country", "first").equalsIgnoreCase("all");
private boolean clearAward = PropertiesUtil.getBooleanProperty("kinopoisk.clear.award", FALSE);
private boolean clearTrivia = PropertiesUtil.getBooleanProperty("kinopoisk.clear.trivia", FALSE);
private boolean translitCountry = PropertiesUtil.getBooleanProperty("kinopoisk.translit.country", FALSE);
private String etalonId = PropertiesUtil.getProperty("kinopoisk.etalon", "448");
// Personal information
private int peopleMax = PropertiesUtil.getIntProperty("plugin.people.maxCount", "15");
private int actorMax = PropertiesUtil.getIntProperty("plugin.people.maxCount.actor", "10");
private int directorMax = PropertiesUtil.getIntProperty("plugin.people.maxCount.director", "2");
private int writerMax = PropertiesUtil.getIntProperty("plugin.people.maxCount.writer", "3");
private int filmographyMax = PropertiesUtil.getIntProperty("plugin.filmography.max", "20");
private int biographyLength = PropertiesUtil.getIntProperty("plugin.biography.maxlength", "500");
private int filmography = PropertiesUtil.getIntProperty("plugin.filmography.max", "20");
private boolean skipVG = PropertiesUtil.getBooleanProperty("plugin.people.skip.VG", TRUE);
private boolean skipTV = PropertiesUtil.getBooleanProperty("plugin.people.skip.TV", FALSE);
private boolean skipV = PropertiesUtil.getBooleanProperty("plugin.people.skip.V", FALSE);
private List jobsInclude = Arrays.asList(PropertiesUtil.getProperty("plugin.filmography.jobsInclude", "Director,Writer,Actor,Actress").split(","));
private int triviaMax = PropertiesUtil.getIntProperty("plugin.trivia.maxCount", "15");
private String skinHome = PropertiesUtil.getProperty("mjb.skin.dir", "./skins/default");
private String jukeboxTempLocationDetails = FileTools.getCanonicalPath(PropertiesUtil.getProperty("mjb.jukeboxTempDir", "./temp") + File.separator + PropertiesUtil.getProperty("mjb.detailsDirName", "Jukebox"));
public KinopoiskPlugin() {
super();
preferredCountry = PropertiesUtil.getProperty("imdb.preferredCountry", "Russia");
tvdb = new TheTvDBPlugin();
}
@Override
public String getPluginID() {
return KINOPOISK_PLUGIN_ID;
}
@Override
public boolean scan(Movie mediaFile) {
boolean retval = false;
String kinopoiskId = mediaFile.getId(KINOPOISK_PLUGIN_ID);
if (nfoPriority) {
// checked NFO data
nfoPlot = StringTools.isValidString(mediaFile.getPlot());
nfoCast = mediaFile.getPerson("Actors").size() > 0;
nfoGenres = mediaFile.getGenres().size() > 0;
nfoDirectors = mediaFile.getPerson("Directing").size() > 0;
nfoWriters = mediaFile.getPerson("Writing").size() > 0;
nfoCertification = StringTools.isValidString(mediaFile.getCertification());
nfoCountry = StringTools.isValidString(mediaFile.getCountry());
nfoYear = StringTools.isValidString(mediaFile.getYear()) ? mediaFile.getYear() : "";
nfoTagline = StringTools.isValidString(mediaFile.getTagline());
nfoRating = mediaFile.getRating() > -1;
nfoTop250 = mediaFile.getTop250() > -1;
nfoCompany = StringTools.isValidString(mediaFile.getCompany());
nfoRelease = StringTools.isValidString(mediaFile.getCompany());
nfoFanart = StringTools.isValidString(mediaFile.getFanartURL());
nfoPoster = StringTools.isValidString(mediaFile.getPosterURL());
nfoAwards = mediaFile.getAwards().size() > 0;
}
if (StringTools.isNotValidString(kinopoiskId)) {
// store original russian title and year
String name = mediaFile.getOriginalTitle();
String year = mediaFile.getYear();
// It's better to remove everything after dash (-) before call of English plugins...
final String previousTitle = mediaFile.getTitle();
int dash = previousTitle.indexOf(titleDivider);
if (dash != -1) {
if (titleLeader.equals("english")) {
mediaFile.setTitle(previousTitle.substring(0, dash));
} else {
mediaFile.setTitle(previousTitle.substring(dash));
}
}
// Get base info from imdb or tvdb
if (!mediaFile.isTVShow()) {
super.scan(mediaFile);
} else {
tvdb.scan(mediaFile);
}
// Let's replace dash (-) by space ( ) in Title.
//name.replace(titleDivider, " ");
dash = name.indexOf(titleDivider);
if (dash != -1) {
if (titleLeader.equals("english")) {
name = name.substring(0, dash);
} else {
name = name.substring(dash);
}
}
kinopoiskId = getKinopoiskId(mediaFile, name, year, mediaFile.getSeason());
mediaFile.setId(KINOPOISK_PLUGIN_ID, kinopoiskId);
// } else {
// If ID is specified in NFO, set original title to unknown
// mediaFile.setTitle(Movie.UNKNOWN);
}
if (StringTools.isValidString(kinopoiskId)) {
// Replace some movie data by data from Kinopoisk
retval = updateKinopoiskMediaInfo(mediaFile, kinopoiskId);
}
return retval;
}
@Override
public boolean scanNFO(String nfo, Movie movie) {
boolean result = false;
logger.debug(logMessage + "Scanning NFO for Kinopoisk Id");
int beginIndex = nfo.indexOf("kinopoisk.ru/level/1/film/");
StringTokenizer st = null;
if (beginIndex != -1) {
st = new StringTokenizer(nfo.substring(beginIndex + 26), "/");
} else {
beginIndex = nfo.indexOf("kinopoisk.ru/film/");
if (beginIndex != -1) {
st = new StringTokenizer(nfo.substring(beginIndex + 18), "/");
}
}
if (st != null) {
movie.setId(KinopoiskPlugin.KINOPOISK_PLUGIN_ID, st.nextToken());
logger.debug(logMessage + "Kinopoisk Id found in nfo = " + movie.getId(KinopoiskPlugin.KINOPOISK_PLUGIN_ID));
result = true;
} else {
logger.debug(logMessage + "No Kinopoisk Id found in nfo !");
}
super.scanNFO(nfo, movie);
return result;
}
public String getKinopoiskId(Movie movie) {
String kinopoiskId = movie.getId(KINOPOISK_PLUGIN_ID);
if (StringTools.isNotValidString(kinopoiskId)) {
int season = movie.getSeason();
String name = movie.getOriginalTitle();
String year = movie.getYear();
int dash = name.indexOf(titleDivider);
if (dash > -1) {
name = titleLeader.equals("english") ? name.substring(0, dash) : name.substring(dash);
}
getKinopoiskId(movie, name, year, season);
}
return kinopoiskId;
}
public String getKinopoiskId(Movie movie, String name, String year, int season) {
String kinopoiskId = movie.getId(KINOPOISK_PLUGIN_ID);
if (StringTools.isNotValidString(kinopoiskId)) {
kinopoiskId = getKinopoiskId(name, year, season);
if (StringTools.isValidString(year) && StringTools.isNotValidString(kinopoiskId)) {
try {
// Search for year +/-1, since the year is not always correct
int y = Integer.parseInt(year);
kinopoiskId = getKinopoiskId(name, Integer.toString(y - 1) + "-" + Integer.toString(y + 1), season);
} catch (Exception ignore) {
}
if (StringTools.isNotValidString(kinopoiskId)) {
// Trying without specifying the year
kinopoiskId = getKinopoiskId(name, Movie.UNKNOWN, season);
}
}
}
return kinopoiskId;
}
/**
* Retrieve Kinopoisk matching the specified movie name and year. This routine is base on a Google request.
*/
public String getKinopoiskId(String movieName, String year, int season) {
try {
String kinopoiskId;
String sb = movieName;
// Unaccenting letters
sb = Normalizer.normalize(sb, Normalizer.Form.NFD);
sb = sb.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
sb = "&m_act[find]=" + URLEncoder.encode(sb, "UTF-8").replace(" ", "+");
if (season != -1) {
sb = sb + "&m_act[content_find]=serial";
} else {
if (StringTools.isValidString(year)) {
if (year.indexOf('-') > -1) {
String[] years = year.split("-");
sb = sb + "&m_act[from_year]=" + years[0];
sb = sb + "&m_act[to_year]=" + years[1];
} else {
sb = sb + "&m_act[year]=" + year;
}
}
}
sb = "http://kinopoisk.ru/index.php?level=7&from=forma&result=adv&m_act[from]=forma&m_act[what]=content" + sb;
String xml = webBrowser.request(sb);
// Checking for zero results
if (xml.indexOf("class=\"search_results\"") < 0) {
// Checking direct movie page
if (xml.indexOf("class=\"moviename-big\"") > -1) {
return HTMLTools.extractTag(xml, "id_film = ", ";");
}
return Movie.UNKNOWN;
}
// Checking if we got the movie page directly
int beginIndex = xml.indexOf("id_film = ");
if (beginIndex == -1) {
// It's search results page, searching a link to the movie page
//beginIndex = xml.indexOf("