Commit dcb7f010 authored by Sam McCollum's avatar Sam McCollum

sm - get basic functionality to use library

parent 3fc36187
Pipeline #78 failed with stages
in 22 seconds
......@@ -2,15 +2,14 @@ package enterprises.mccollum.home.media.control;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.apache.commons.text.similarity.LevenshteinDistance;
import enterprises.mccollum.home.media.model.MovieMetadata;
import enterprises.mccollum.home.themoviedb.TheMoviedbAPIClient;
import enterprises.mccollum.icing.tmdb.TheMoviedbAPIClient;
public class MovieSearchService {
@Inject
......@@ -28,7 +27,28 @@ public class MovieSearchService {
public List<MovieMetadata> searchMovies(String filePath) {
String fileName = getFileName(filePath);
String title = getTitle(fileName);
List<MovieMetadata> results = movieDb.searchMovies(title, getYear(fileName));
List<MovieMetadata> results = movieDb.searchMovies(title, getYear(fileName)).stream().map((item) -> {
MovieMetadata mmd = new MovieMetadata();
mmd.setId(item.getId());
mmd.setTitle(item.getTitle());
mmd.setOriginal_title(item.getOriginal_title());
mmd.setVote_average(item.getVote_average());
mmd.setVote_count(item.getVote_count());
mmd.setVideo(item.getVideo());
mmd.setPoster_path(item.getPoster_path());
mmd.setAdult(item.getAdult());
mmd.setOverview(item.getOverview());
mmd.setOriginal_language(item.getOriginal_language());
mmd.setBackdrop_path(item.getBackdrop_path());
mmd.setPopularity(item.getPopularity());
mmd.setRelease_date(item.getRelease_date());
/*
TODO: Add genre id's to the conversion
List<Integer> genre_ids;
*/
return mmd;
}).collect(Collectors.toList());
Collections.sort(results, (a, b) ->
lDistance.apply(title, a.getTitle())
.compareTo(lDistance.apply(title, b.getTitle())
......
package enterprises.mccollum.home.media.model;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class MediaSearchResultContainer {
Integer page;
Integer total_results;
Integer total_pages;
List<MovieMetadata> results = null;
//Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getTotal_results() {
return total_results;
}
public void setTotal_results(Integer total_results) {
this.total_results = total_results;
}
public Integer getTotal_pages() {
return total_pages;
}
public void setTotal_pages(Integer total_pages) {
this.total_pages = total_pages;
}
public List<MovieMetadata> getResults() {
return results;
}
public void setResults(List<MovieMetadata> results) {
this.results = results;
}
// public Map<String, Object> getAdditionalProperties() {
// return this.additionalProperties;
// }
//
// public void setAdditionalProperty(String name, Object value) {
// this.additionalProperties.put(name, value);
// }
}
......@@ -10,6 +10,7 @@ import javax.persistence.ManyToMany;
import javax.xml.bind.annotation.XmlRootElement;
import enterprises.mccollum.home.media.model.MovieMetadata;
import enterprises.mccollum.icing.tmdb.genre.SerializableGenre;
@Entity
@XmlRootElement
......@@ -25,6 +26,11 @@ public class Genre {
public Genre(){}
public Genre(SerializableGenre genre) {
this.id = genre.getId();
this.name = genre.getName();
}
public Genre(JsonObject jsonGenre){
this.id = (long)jsonGenre.getInt("id");
this.name = jsonGenre.getString("name");
......
package enterprises.mccollum.home.themoviedb;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import enterprises.mccollum.icing.tmdb.TheMoviedbAPIClient;
import enterprises.mccollum.icing.tmdb.required.GenericRestClient;
public class TmdbProducer {
@Inject
GenericRestClient restClient;
@Produces
public TheMoviedbAPIClient getClient() {// throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
return new TheMoviedbAPIClient(restClient);
}
}
package enterprises.mccollum.home.themoviedb;
import java.io.StringReader;
import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.ejb.Stateless;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import enterprises.mccollum.home.media.model.MovieMetadata;
import enterprises.mccollum.home.media.model.MediaSearchResultContainer;
import enterprises.mccollum.icing.tmdb.required.GenericRestClient;
public class TheMoviedbAPIClient {
public static final String API_KEY = "c8b6dbadc2817d249f4c587da01533b7"; //kodi: "6889f6089877fd092454d00edb44a84d"
public static final String MOVIE_GENRE_URL = "https://api.themoviedb.org/3/genre/movie/list?api_key="+API_KEY+"&language=en-US";
public static final String TV_GENRE_URL = "https://api.themoviedb.org/3/genre/tv/list?api_key="+API_KEY+"&language=en-US";
public List<MovieMetadata> searchMovies(String query, Integer year){
MediaSearchResultContainer result = null;
Logger.getLogger("TmDbClient").log(Level.INFO,
String.format("Search Url: %s", buildSearchUrl(query, year)));
result = makeMovieDbRequest(() -> {
return ClientBuilder.newClient().target(buildSearchUrl(query, year)).request().get(MediaSearchResultContainer.class);
});
return result.getResults();
}
private String buildSearchUrl(String query, Integer year) {
StringBuilder sb = new StringBuilder(String.format("http://api.themoviedb.org/3/search/movie?api_key=%s&query=%s", API_KEY, query));
if(year != null)
sb.append("&year="+year);
return sb.toString();
}
private void wait(int waitTime) {
try {
Thread.sleep(waitTime);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
Genre getGenreById(Long id) {
JsonObject movieGenres = makeMovieDbRequest(() -> {
String json = ClientBuilder.newClient().target(MOVIE_GENRE_URL).request().get(String.class);
try(JsonReader reader = Json.createReader(new StringReader(json))) {
return reader.readObject();
}
@Stateless
public class TmdbRestClient implements GenericRestClient {
@Override
public <T> T getRequest(final Class<T> klasse, final String url) {
return makeMovieDbRequest(() -> {
return ClientBuilder.newClient().target(url).request().get(klasse);
});
JsonArray genres = movieGenres.getJsonArray("genres");
for(int i = 0; i < genres.size(); ++i) {
JsonObject genreJson = genres.getJsonObject(i);
Genre genre = new Genre(genreJson);
if(genre.id == id)
return genre;
}
return null;
}
/**
......@@ -70,7 +23,7 @@ public class TheMoviedbAPIClient {
* @param requestLambda The lambda that actually performs the request
* @return
*/
private <T> T makeMovieDbRequest(Supplier<T> requestLambda) {
protected <T> T makeMovieDbRequest(Supplier<T> requestLambda) {
T result = null;
while(result == null) {
try {
......@@ -89,7 +42,11 @@ public class TheMoviedbAPIClient {
if(value != null && value.matches("[0-9]*")) {
waitTime += (1000*Integer.parseInt(value));
}
wait(waitTime);
try {
wait(waitTime);
} catch (InterruptedException error) {
error.printStackTrace();
}
}
}
return result;
......
......@@ -15,8 +15,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import com.google.gson.Gson;
import enterprises.mccollum.home.media.model.MovieMetadata;
import enterprises.mccollum.home.themoviedb.TheMoviedbAPIClient;
import enterprises.mccollum.home.media.model.MediaSearchResultContainer;
import enterprises.mccollum.icing.tmdb.TheMoviedbAPIClient;
import enterprises.mccollum.icing.tmdb.film.MovieSearchResultContainer;
import enterprises.mccollum.home.media.model.MediaSource;
@RunWith(MockitoJUnitRunner.class)
......@@ -40,7 +40,7 @@ public class MovieSearchServiceTest {
when(movieSearch.movieDb.searchMovies("The Note", 2007))
.thenReturn(new Gson().fromJson(
"{\"page\":1,\"total_results\":6,\"total_pages\":1,\"results\":[{\"vote_count\":155,\"id\":16007,\"video\":false,\"vote_average\":6.9,\"title\":\"Death Note\",\"popularity\":5.228005,\"poster_path\":\"\\/jXaT3k2GuW9WTFmoCyskZyWfKlw.jpg\",\"original_language\":\"ja\",\"original_title\":\"デスノート\",\"genre_ids\":[14,9648,53],\"backdrop_path\":\"\\/jtWLpxu3zZzTNECLImUxo4NhzuF.jpg\",\"adult\":false,\"overview\":\"Light Yagami finds the \\\"Death Note,\\\" a notebook with the power to kill, and decides to create a Utopia by killing the world's criminals, and soon the world's greatest detective, \\\"L,\\\" is hired to find the mysterious murderer. An all out battle between the two greatest minds on earth begins and the winner will control the world.\",\"release_date\":\"2006-06-17\"},{\"vote_count\":3,\"id\":30569,\"video\":false,\"vote_average\":4.3,\"title\":\"The Note\",\"popularity\":1.305184,\"poster_path\":\"\\/4TA0uYGhPV5zxU7JF17DZITCNpW.jpg\",\"original_language\":\"en\",\"original_title\":\"The Note\",\"genre_ids\":[10770,18,10749],\"backdrop_path\":\"\\/fEb9zDthAj7lgBd7y8ErQSuJWQb.jpg\",\"adult\":false,\"overview\":\"Newspaper columnist Peyton MacGruder (Genie Francis) finds a note addressed simply to 'T', washed up on shore. It appears to be from the victim of a recent plane crash, and carries a message of hope and forgiveness from a father to his child. MacGruder's readership is down on her column (called \\\"Heart Healer\\\"), and the paper is going to dump it unless she starts to write from the heart.\",\"release_date\":\"2007-12-08\"},{\"vote_count\":108,\"id\":16140,\"video\":false,\"vote_average\":6.7,\"title\":\"Death Note: The Last Name\",\"popularity\":5.813076,\"poster_path\":\"\\/hxsKIv5U10eZBrlVRzS6hUiiK97.jpg\",\"original_language\":\"ja\",\"original_title\":\"デスノート the Last name\",\"genre_ids\":[14,53,9648],\"backdrop_path\":\"\\/qb9afBMhlrTVqJ1yyuofhGKDc7f.jpg\",\"adult\":false,\"overview\":\"In the second installment of the Death Note film franchise, Light Yagami meets a second Kira and faithful follower Misa Amane and her Shinigami named Rem. Light attempts to defeat L along with Teru Mikami (a Kira follower) and Kiyomi Takada (another Kira follower) but in the end will Light win? or will a Shinigami named Ryuk make all the difference in Light's victory or his ultimate death?\",\"release_date\":\"2006-10-28\"},{\"vote_count\":3,\"id\":27182,\"video\":false,\"vote_average\":7.3,\"title\":\"Note by Note: The Making of Steinway L1037\",\"popularity\":1.017991,\"poster_path\":\"\\/vxF0ow8yFXxUDrY50X27sRCTj87.jpg\",\"original_language\":\"en\",\"original_title\":\"Note by Note: The Making of Steinway L1037\",\"genre_ids\":[99],\"backdrop_path\":\"\\/3HzISgYhyjMgiJySpfIybaTHmA7.jpg\",\"adult\":false,\"overview\":\"A feature-length independent documentary that follows the creation of a Steinway concert grand, #L1037- from forest floor to concert hall.\",\"release_date\":\"2007-11-07\"},{\"vote_count\":0,\"id\":344778,\"video\":false,\"vote_average\":0,\"title\":\"Avishai Cohen - As Is...Live at the Blue Note\",\"popularity\":1.009261,\"poster_path\":\"\\/eW7DHEkPCliDFkoZ0TcGhtxxwJr.jpg\",\"original_language\":\"en\",\"original_title\":\"Avishai Cohen - As Is...Live at the Blue Note\",\"genre_ids\":[],\"backdrop_path\":null,\"adult\":false,\"overview\":\"Avishai Cohen, who became well known in the jazz world during his period with Chick Corea, is one of the top bassists in the world. His virtuosity and constant creativity in both a modern mainstream format and on funkier grooves seem effortless. As Is...Live at the Blue Note contains a CD (the first seven selections) and a DVD. \\\"Smash,\\\" \\\"Feediop,\\\" the ballad \\\"Remembering\\\" and an overlong \\\"Caravan\\\" (the one non-original) are featured in both formats while three songs are different. Cohen takes many solos and is always a prominent force in the ensembles. Pianist-keyboardist Sam Barsh and drummer Mark Guiliana communicate very well with Cohen while trumpeter Diego Urcola and tenor soprano saxophonist Jimmy Greene add plenty of fire during their guest appearances\",\"release_date\":\"2007-04-24\"},{\"vote_count\":0,\"id\":352822,\"video\":false,\"vote_average\":0,\"title\":\"Notes from the Rogues Gallery\",\"popularity\":1.005655,\"poster_path\":null,\"original_language\":\"en\",\"original_title\":\"Notes from the Rogues Gallery\",\"genre_ids\":[35],\"backdrop_path\":null,\"adult\":false,\"overview\":\"What if you decided to become... a super hero?\",\"release_date\":\"2007-12-31\"}]}",
MediaSearchResultContainer.class).getResults());
MovieSearchResultContainer.class).getResults());
List<MovieMetadata> results = movieSearch.searchMovies("The Note (2007).mkv");
......
package enterprises.mccollum.home.themoviedb;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import enterprises.mccollum.home.media.model.MovieMetadata;
import static org.junit.Assert.*;
import java.util.List;
@RunWith(MockitoJUnitRunner.class)
public class TheMovieDbApiClientTest {
@InjectMocks
TheMoviedbAPIClient tmDb;
@Before
public void setupTests() {
//TODO: Do Stuff
}
@Test
public void testSearch() {
List<MovieMetadata> results = tmDb.searchMovies("Serenity", 2005);
assertEquals(2, results.size()); //should contain two
MovieMetadata real = null;
FIND_REAL_RESULT: for(MovieMetadata result : results) {
if(result.getId() == 16320) {
real = result;
break FIND_REAL_RESULT;
}
}
assertNotNull(real);
}
@Test
public void testGenres() {
final Long ADVENTURE_GENRE_ID = 12L;
final String ADVENTURE_GENRE_NAME = "Adventure";
Genre adventureGenre = tmDb.getGenreById(ADVENTURE_GENRE_ID);
assertNotNull(adventureGenre);
assertEquals(ADVENTURE_GENRE_ID, adventureGenre.getId());
assertEquals(ADVENTURE_GENRE_NAME, adventureGenre.getName());
}
}
......@@ -9,7 +9,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import enterprises.mccollum.home.media.control.MovieSearchService;
import enterprises.mccollum.home.themoviedb.TheMoviedbAPIClient;
import enterprises.mccollum.icing.tmdb.TheMoviedbAPIClient;
@RunWith(MockitoJUnitRunner.class)
public class MovieSearchServiceTest {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment