From 9a4eae0be9eda798db3b3417faaf1ec181f9acd9 Mon Sep 17 00:00:00 2001 From: Jannik Date: Thu, 26 Apr 2018 15:13:15 +0200 Subject: [PATCH] autoplay fixes * HomeFlix should atoplay the next episode evene the jump between episodes or seasons is greater than 1 * if a movie is not found at the omdb we are searching for it, this shoul work for the most movies --- .../HomeFlix/controller/DBController.java | 65 ++++++++++++------- .../controller/OMDbAPIController.java | 21 ++++-- .../HomeFlix/player/PlayerController.java | 2 +- 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/main/java/kellerkinder/HomeFlix/controller/DBController.java b/src/main/java/kellerkinder/HomeFlix/controller/DBController.java index 6c74049..8ef2aac 100644 --- a/src/main/java/kellerkinder/HomeFlix/controller/DBController.java +++ b/src/main/java/kellerkinder/HomeFlix/controller/DBController.java @@ -335,7 +335,7 @@ public class DBController { if (source.getMode().equals("local")) { for (File file : new File(source.getPath()).listFiles()) { String mimeType = URLConnection.guessContentTypeFromName(file.getPath()); - // if file is file and has mime type "video" TODO needs testing + // if file is file and has mime type "video" if (file.isFile() && mimeType != null && mimeType.contains("video")) { // get all files (films) if (!filmsdbStreamURL.contains(file.getPath())) { @@ -667,40 +667,59 @@ public class DBController { } } - /** TODO check if we relay need to separate between favorites and none favorites + /** * get the next episode of a * @param title URL of the film * @param nextEp number of the next episode * @return {@link FilmTabelDataType} the next episode as object */ - public FilmTabelDataType getNextEpisode(String title, int nextEp, int season) { + public FilmTabelDataType getNextEpisode(String title, int episode, int season) { FilmTabelDataType nextFilm = null; + ResultSet rs; + int nextEpisode = 3000; + try { Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\" AND episode = \"" - + nextEp + "\" AND season = \"" + season + "\";"); - if (rs.next()) { - if (rs.getBoolean("favorite") == true) { + + rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\" AND season = \"" + season + "\";"); + while(rs.next()) { + int rsEpisode = Integer.parseInt(rs.getString("episode")); + if (rsEpisode > episode && rsEpisode < nextEpisode) { + // fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode + nextEpisode = rsEpisode; + System.out.println("next episode is: " + nextEpisode); + // favorite image is black nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), rs.getBoolean("cached"), new ImageView(favorite_black)); - } else { - nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), - rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), - rs.getBoolean("cached"), new ImageView(favorite_border_black)); } - } else { - rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title - + "\" AND episode = \"1\" AND season = \"" + (season + 1) + "\";"); - while (rs.next()) { - if (rs.getBoolean("favorite") == true) { - nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), - rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), - rs.getBoolean("cached"), new ImageView(favorite_black)); - } else { - nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), - rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), - rs.getBoolean("cached"), new ImageView(favorite_border_black)); + } + + if (nextFilm == null) { + int nextSeason = 3000; + System.out.println("searching next season"); + rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\";"); + while(rs.next()) { + int rsSeason = Integer.parseInt(rs.getString("season")); + if (rsSeason > season && rsSeason < nextSeason) { + nextSeason = rsSeason; + } + } + + if (nextSeason != 3000) { + System.out.println("next season is: " + nextSeason); + rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\" AND season = \"" + season + "\";"); + while(rs.next()) { + int rsEpisode = Integer.parseInt(rs.getString("episode")); + if (rsEpisode > episode && rsEpisode < nextEpisode) { + // fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode + nextEpisode = rsEpisode; + System.out.println("next episode is: " + nextEpisode); + // favorite image is black + nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), + rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), + rs.getBoolean("cached"), new ImageView(favorite_black)); + } } } } diff --git a/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java b/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java index e60db8f..a02d6fb 100644 --- a/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java +++ b/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java @@ -34,6 +34,7 @@ import org.apache.logging.log4j.Logger; import com.eclipsesource.json.Json; import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; import javafx.application.Platform; import kellerkinder.HomeFlix.application.Main; @@ -75,6 +76,7 @@ public class OMDbAPIController implements Runnable { output = ina.readLine(); ina.close(); LOGGER.info("response from " + URL + " was valid"); + LOGGER.info("Title was: " + mainWindowController.getCurrentTitle()); LOGGER.info(output); } catch (IOException e) { LOGGER.error("error while making api request or reading response"); @@ -86,6 +88,7 @@ public class OMDbAPIController implements Runnable { if (object.getString("Error", "").equals("Movie not found!")) { // if the movie was not found try to search it + LOGGER.warn("Movie was not found at first try, searching again!"); // TODO split the name intelligent as it may contain the film title // query the api try { @@ -94,8 +97,8 @@ public class OMDbAPIController implements Runnable { BufferedReader ina = new BufferedReader(new InputStreamReader(apiUrl.openStream())); output = ina.readLine(); ina.close(); - System.out.println(apiUrl); LOGGER.info("response from search " + URL + " was valid"); + LOGGER.info("Title was: " + mainWindowController.getCurrentTitle()); LOGGER.info(output); } catch (Exception e) { LOGGER.error("error while making api request or reading response"); @@ -104,13 +107,17 @@ public class OMDbAPIController implements Runnable { } JsonObject searchObject = Json.parse(output).asObject(); - // TODO new query with the actual title - if (object.getString("Response", "").equals("True")) { - System.out.println(searchObject.getString("Title", "")); + if (searchObject.getString("Response", "").equals("True")) { + for (JsonValue movie : searchObject.get("Search").asArray()) { + // get first entry from the array and set object = movie + // TODO probably we have a NullPointerException here! + object = (JsonObject) movie; + System.out.println(movie.toString()); + break; + + } + System.out.println(object.getString("Title", "")); } - - System.out.println("Movie not found, not setting cache"); - return; } // add the response to the responseString[] diff --git a/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java b/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java index e6b090d..40fcee6 100644 --- a/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java +++ b/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java @@ -149,7 +149,7 @@ public class PlayerController { if ((duration - currentTime) < 10000 && episode != 0 && autoplay) { autoplay = false; mainWCon.getDbController().setCurrentTime(film.getStreamUrl(), 0); // reset old video start time - FilmTabelDataType nextFilm = mainWCon.getDbController().getNextEpisode(film.getTitle(), (episode + 1), season); + FilmTabelDataType nextFilm = mainWCon.getDbController().getNextEpisode(film.getTitle(), episode, season); if (nextFilm != null) { mediaPlayer.stop(); init(mainWCon, player, nextFilm);