Skip to content

Commit

Permalink
Added Type of Movie and its handling
Browse files Browse the repository at this point in the history
  • Loading branch information
REDNBLACK committed May 7, 2016
1 parent 9df9980 commit 8ffb427
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 60 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* Если всего поровну или вам лень что-либо настраивать - `Смешанный`; поиск выполняется по цепочке `XML -> JSON -> HTML`

#### Сравнение:
* Если не хотите разбираться в тонкостях настройки или это ваш первый запуск, то оставьте как есть, а именно: `Год с отклонением` и `Интеллектуальное сравнение названий`.
* Если не хотите разбираться в тонкостях настройки или это ваш первый запуск, то оставьте как есть, а именно: `Год с отклонением`, `Тип фильма с полным совпадением` и `Интеллектуальное сравнение названий`.

#### Чистый запуск:
* Включайте только если случайно удалили оценки/список на сайте IMDB, и хотите импортировать список с Кинопоиска заново.
Expand All @@ -51,6 +51,8 @@
- `Сравнение` - каким способом сравнивать названия фильмов из таблицы Кинопоиска с найденными в IMDB.
- (По умолчанию) `Год с отклонением` - сравнить год, используя отклонение в +/-1 год
- `Год с полным совпадением` - проверить идентичность по годам.
- (По умолчанию) `Тип фильма с полным совпадением` - индентичное сравнение типов фильмов (обычный/сериал/документальный/короткометражка)
- `Любой тип фильма` - не сравнивать типы фильмов
- (По умолчанию) `Интеллектуальное сравнение названий` - сравнить названия, используя уникальный алгоритм.
- `Полное совпадение названий` - проверить на идентичность названий.
- `Одно название начинается с другого` - сравнить находится ли название фильма из таблицы Кинопоиска в начале названия с IMDB.
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.7.8"
version = "0.9.0"

ext {
guiceVersion = "4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ enum Type {
TITLE_EQUALS,
TITLE_SMART,
YEAR_DEVIATION,
YEAR_EQUALS
YEAR_EQUALS,
TYPE_EQUALS,
TYPE_ANY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.inject.Inject;
import com.typesafe.config.Config;
import org.f0w.k2i.core.comparator.title.*;
import org.f0w.k2i.core.comparator.type.AnyTypeComparator;
import org.f0w.k2i.core.comparator.type.EqualsTypeComparator;
import org.f0w.k2i.core.comparator.year.DeviationYearComparator;
import org.f0w.k2i.core.comparator.year.EqualsYearComparator;

Expand Down Expand Up @@ -41,6 +43,10 @@ public MovieComparator make(MovieComparator.Type type) {
return new DeviationYearComparator(config);
case YEAR_EQUALS:
return new EqualsYearComparator();
case TYPE_EQUALS:
return new EqualsTypeComparator();
case TYPE_ANY:
return new AnyTypeComparator();
default:
throw new IllegalArgumentException("Comparator of this type not found!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public final class SmartTitleComparator extends AbstractMovieComparator {
list.add(s -> s);

// Original string without one of symbols
val symbolsToRemove = Arrays.asList(",", ":", "-", " ");
symbolsToRemove.forEach(symbol -> list.add(s -> replace(s, symbol, "")));
val symbolsToRemove = Arrays.asList(",", ":", "-", " ", "3-D", "3D", "4D", "4-D");
symbolsToRemove.forEach(symbol -> list.add(s -> replace(s, symbol, "").trim()));

// Original string without apostrophes and quotes
list.add(s -> {
Expand All @@ -50,13 +50,20 @@ public final class SmartTitleComparator extends AbstractMovieComparator {
// Original string without special symbols like unicode etc
list.add(s -> javaLetterOrDigit().or(WHITESPACE).or(isNot('.')).precomputed().retainFrom(s));

// Original string with part before one of separating symbols
val separatingSymbols = Collections.singletonList("-");
separatingSymbols.forEach(separator -> list.add(s -> Arrays.stream(splitByWholeSeparator(s, separator))
.findFirst()
.map(String::trim)
.orElse(s)
));
// Original string with part before or after one of separating symbols
val separatingSymbols = Arrays.asList("-", ":");
separatingSymbols.forEach(separator -> {
list.add(s -> Arrays.stream(splitByWholeSeparator(s, separator))
.findFirst()
.map(String::trim)
.orElse(s)
);
list.add(s -> Arrays.stream(splitByWholeSeparator(s, separator))
.reduce((s1, s2) -> s2)
.map(String::trim)
.orElse(s)
);
});

// One of the prefixes + original string
val prefixes = Collections.singletonList("The ");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.f0w.k2i.core.comparator.type;

import lombok.NonNull;
import org.f0w.k2i.core.comparator.AbstractMovieComparator;
import org.f0w.k2i.core.model.entity.Movie;

public final class AnyTypeComparator extends AbstractMovieComparator {
/**
* {@inheritDoc}
*/
@Override
public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) {
LOG.debug(
"Comparing types disabled, tried to compare type '{}' with type '{}''",
movie1.getType(),
movie2.getType()
);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.f0w.k2i.core.comparator.type;

import lombok.NonNull;
import org.f0w.k2i.core.comparator.AbstractMovieComparator;
import org.f0w.k2i.core.model.entity.Movie;

public final class EqualsTypeComparator extends AbstractMovieComparator {
/**
* {@inheritDoc}
*/
@Override
public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) {
boolean result = movie1.getType().equals(movie2.getType());

LOG.debug(
"Comparing type '{}' with type '{}', matches = '{}'",
movie1.getType(),
movie2.getType(),
result
);

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.jsoup.Connection;

import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.*;

@Slf4j
public final class MixedMovieFinder implements MovieFinder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.NonNull;
import org.f0w.k2i.core.model.entity.Movie;
import org.f0w.k2i.core.util.MovieUtils;

import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -35,23 +36,34 @@ public interface ExchangeStrategy {
* Used in {@link this#parseSearchResult(String)}
*
* @param <R> Root element
* @param <T> Title element
* @param <Y> Year element
* @param <I> IMDB ID element
* @param <TITLE> Title element
* @param <YEAR> Year element
* @param <YEAR> Type element
* @param <ID> IMDB ID element
*/
interface MovieParser<R, T, Y, I> {
default Movie parse(R rootElement, Function<R, T> title, Function<R, Y> year, Function<R, I> imdbID) {
interface MovieParser<R, TITLE, YEAR, TYPE, ID> {
default Movie parse(
R rootElement,
Function<R, TITLE> title,
Function<R, YEAR> year,
Function<R, TYPE> type,
Function<R, ID> imdbID
) {
return new Movie(
parseTitle(prepareTitle(title.apply(rootElement))),
parseYear(prepareYear(year.apply(rootElement))),
parseType(type.apply(rootElement)),
null,
parseIMDBId(prepareImdbId(imdbID.apply(rootElement)))
);
}

String prepareTitle(T element);
String prepareTitle(TITLE element);

String prepareYear(Y element);
String prepareYear(YEAR element);

String prepareImdbId(I element);
Movie.Type parseType(TYPE element);

String prepareImdbId(ID element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public List<Movie> parseSearchResult(@NonNull final String data) {
e,
t -> t,
Element::text,
t -> t,
t -> t.getElementsByTag("a").first()
))
.collect(Collectors.toList());
}

private static final class HTMLMovieParser implements MovieParser<Element, Element, String, Element> {
private static final class HTMLMovieParser implements MovieParser<Element, Element, String, Element, Element> {
@Override
public String prepareTitle(Element element) {
String elementText = element.text();
Expand Down Expand Up @@ -86,6 +87,21 @@ public String prepareYear(String element) {
return preparedYear;
}

@Override
public Movie.Type parseType(Element element) {
val elementText = element.text();

if (elementText.contains("(TV Series)") || elementText.contains("(TV Mini-Series)")) {
return Movie.Type.SERIES;
} else if (elementText.contains("(Short)")) {
return Movie.Type.SHORT;
} else if (elementText.contains("(Video Game)")) {
return Movie.Type.VIDEO_GAME;
}

return Movie.Type.MOVIE;
}

@Override
public String prepareImdbId(Element element) {
return Optional.ofNullable(element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public List<Movie> parseSearchResult(@NonNull final String data) {
e.getAsJsonObject(),
t -> t.get("title"),
t -> t.get("description"),
t -> t.get("description"),
t -> t.get("id")
))
.collect(Collectors.toList());
}

private static final class JSONMovieParser implements MovieParser<JsonObject, JsonElement, JsonElement, JsonElement> {
private static final class JSONMovieParser implements MovieParser<JsonObject, JsonElement, JsonElement, JsonElement, JsonElement> {
@Override
public String prepareTitle(JsonElement element) {
return getStringOrNull(element);
Expand All @@ -71,6 +72,23 @@ public String prepareYear(JsonElement element) {
return getStringOrNull(element);
}

@Override
public Movie.Type parseType(JsonElement element) {
val stringValue = Optional.ofNullable(element).map(JsonElement::getAsString).orElse("");

if (stringValue.contains("TV series")) {
return Movie.Type.SERIES;
} else if (stringValue.contains("documentary")) {
return Movie.Type.DOCUMENTARY;
} else if (stringValue.contains("short")) {
return Movie.Type.SHORT;
} else if (stringValue.contains("video game")) {
return Movie.Type.VIDEO_GAME;
}

return Movie.Type.MOVIE;
}

@Override
public String prepareImdbId(JsonElement element) {
return getStringOrNull(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ public List<Movie> parseSearchResult(@NonNull final String data) {
e,
Element::ownText,
t -> t.getElementsByTag("Description").first(),
t -> t.getElementsByTag("Description").first(),
t -> t.attr("id")
))
.collect(Collectors.toList());
}

private static final class XMLMovieParser implements MovieParser<Element, String, Element, String> {
private static final class XMLMovieParser implements MovieParser<Element, String, Element, Element, String> {
@Override
public String prepareTitle(String element) {
return element;
Expand All @@ -62,6 +63,23 @@ public String prepareYear(Element element) {
.orElse(null);
}

@Override
public Movie.Type parseType(Element element) {
val stringValue = Optional.ofNullable(element).map(Element::text).orElse("");

if (stringValue.contains("TV series")) {
return Movie.Type.SERIES;
} else if (stringValue.contains("documentary")) {
return Movie.Type.DOCUMENTARY;
} else if (stringValue.contains("short")) {
return Movie.Type.SHORT;
} else if (stringValue.contains("video game")) {
return Movie.Type.VIDEO_GAME;
}

return Movie.Type.MOVIE;
}

@Override
public String prepareImdbId(String element) {
return element;
Expand Down
33 changes: 25 additions & 8 deletions core/src/main/java/org/f0w/k2i/core/model/entity/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import lombok.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.*;

@Entity
@Table(name = "MOVIES", uniqueConstraints = @UniqueConstraint(columnNames = {"TITLE", "YEAR"}))
Expand All @@ -21,25 +18,45 @@ public class Movie extends BaseEntity {
@Column(name = "YEAR", nullable = false)
private int year;

@Column(name = "TYPE", nullable = false)
@Enumerated(EnumType.STRING)
@NonNull
private Type type;

@Column(name = "RATING")
private Integer rating;

@Column(name = "IMDB_ID")
private String imdbId;

public Movie(String title, int year) {
this(title, year, null, null);
this(title, year, Type.MOVIE, null, null);
}

public Movie(String title, int year, Integer rating) {
this(title, year, rating, null);
this(title, year, Type.MOVIE, rating, null);
}

public Movie(String title, int year, Type type) {
this(title, year, type, null, null);
}

public Movie(String title, int year, String imdbId) {
this(title, year, null, imdbId);
this(title, year, Type.MOVIE, null, imdbId);
}

public Movie(Movie movie) {
this(movie.getTitle(), movie.getYear(), movie.getRating(), movie.getImdbId());
this(movie.getTitle(), movie.getYear(), movie.getType(), movie.getRating(), movie.getImdbId());
}

/**
* Type of movie
*/
public enum Type {
MOVIE,
DOCUMENTARY,
SHORT,
SERIES,
VIDEO_GAME
}
}
Loading

0 comments on commit 8ffb427

Please sign in to comment.