2018-03-10 14:16:37 +01:00
/ * *
* Project - HomeFlix
*
* Copyright 2018 < @Seil0 >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2018-03-08 17:59:28 +01:00
package kellerkinder.HomeFlix.controller ;
import java.awt.image.BufferedImage ;
import java.io.BufferedReader ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.URL ;
import javax.imageio.ImageIO ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonObject ;
2018-04-26 15:13:15 +02:00
import com.eclipsesource.json.JsonValue ;
2018-03-08 17:59:28 +01:00
import javafx.application.Platform ;
import kellerkinder.HomeFlix.application.Main ;
import kellerkinder.HomeFlix.application.MainWindowController ;
2018-04-29 14:21:19 +02:00
import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType ;
2018-03-08 17:59:28 +01:00
public class OMDbAPIController implements Runnable {
private MainWindowController mainWindowController ;
private DBController dbController ;
private Main main ;
private String URL = " https://www.omdbapi.com/?apikey= " ;
2018-05-09 22:40:14 +02:00
private boolean useEpisode = true ;
2018-03-08 17:59:28 +01:00
private static final Logger LOGGER = LogManager . getLogger ( MainWindowController . class . getName ( ) ) ;
2018-04-19 12:13:18 +02:00
/ * *
* constructor for the OMDbAPIController
* @param mainWindowController the MainWindowController object
* @param dbController the DBController object
* @param main the Main object
* /
2018-03-08 17:59:28 +01:00
public OMDbAPIController ( MainWindowController mainWindowController , DBController dbController , Main main ) {
this . mainWindowController = mainWindowController ;
this . dbController = dbController ;
this . main = main ;
}
@Override
public void run ( ) {
2018-04-29 14:28:12 +02:00
LOGGER . info ( " Querying omdbAPI ... " ) ;
2018-04-29 14:21:19 +02:00
JsonObject object ;
object = getByTitle ( mainWindowController . getCurrentTitle ( ) ) ;
if ( object = = null ) return ;
2018-03-08 17:59:28 +01:00
2018-04-29 14:21:19 +02:00
if ( object . getString ( " Error " , " " ) . contains ( " not found! " ) ) {
String title = searchByTitle ( mainWindowController . getCurrentTitle ( ) ) ;
if ( title . length ( ) > 0 ) {
object = getByTitle ( title ) ;
2018-05-09 22:40:14 +02:00
if ( object . getString ( " Error " , " " ) . contains ( " Series or episode not found! " ) ) {
useEpisode = false ;
object = getByTitle ( title ) ;
}
2018-04-27 15:06:40 +02:00
} else {
return ;
2018-04-19 11:45:11 +02:00
}
}
2018-04-29 14:21:19 +02:00
OMDbAPIResponseDataType omdbResponse = new OMDbAPIResponseDataType ( ) ;
omdbResponse . setTitle ( object . getString ( " Title " , " " ) ) ;
omdbResponse . setYear ( object . getString ( " Year " , " " ) ) ;
omdbResponse . setRated ( object . getString ( " Rated " , " " ) ) ;
omdbResponse . setReleased ( object . getString ( " Release " , " " ) ) ;
omdbResponse . setSeason ( object . getString ( " Season " , " " ) ) ;
omdbResponse . setEpisode ( object . getString ( " Episode " , " " ) ) ;
omdbResponse . setRuntime ( object . getString ( " Runtime " , " " ) ) ;
omdbResponse . setGenre ( object . getString ( " Genre " , " " ) ) ;
omdbResponse . setDirector ( object . getString ( " Director " , " " ) ) ;
omdbResponse . setWriter ( object . getString ( " Writer " , " " ) ) ;
omdbResponse . setActors ( object . getString ( " Actors " , " " ) ) ;
omdbResponse . setPlot ( object . getString ( " Plot " , " " ) ) ;
omdbResponse . setLanguage ( object . getString ( " Language " , " " ) ) ;
omdbResponse . setCountry ( object . getString ( " Country " , " " ) ) ;
omdbResponse . setAwards ( object . getString ( " Awards " , " " ) ) ;
omdbResponse . setMetascore ( object . getString ( " Metascore " , " " ) ) ;
omdbResponse . setImdbRating ( object . getString ( " imdbRating " , " " ) ) ;
omdbResponse . setImdbVotes ( object . getString ( " imdbVotes " , " " ) ) ;
omdbResponse . setImdbID ( object . getString ( " imdbID " , " " ) ) ;
omdbResponse . setType ( object . getString ( " Type " , " " ) ) ;
omdbResponse . setDvd ( object . getString ( " DVD " , " " ) ) ;
omdbResponse . setBoxOffice ( object . getString ( " BoxOffice " , " " ) ) ;
omdbResponse . setProduction ( object . getString ( " Production " , " " ) ) ;
omdbResponse . setWebsite ( object . getString ( " Website " , " " ) ) ;
omdbResponse . setResponse ( object . getString ( " Response " , " " ) ) ;
// resize the image to fit in the posterImageView and add it to the cache
try {
BufferedImage originalImage = ImageIO . read ( new URL ( object . getString ( " Poster " , " " ) ) ) ;
// change path to where file is located
2018-05-04 18:53:51 +02:00
omdbResponse . setPoster ( main . getPosterCache ( ) + " / " + omdbResponse . getTitle ( ) + " .png " ) ;
2018-04-29 14:21:19 +02:00
ImageIO . write ( originalImage , " png " , new File ( omdbResponse . getPoster ( ) ) ) ;
LOGGER . info ( " adding poster to cache: " + omdbResponse . getPoster ( ) ) ;
2018-03-08 17:59:28 +01:00
} catch ( Exception e ) {
LOGGER . error ( e ) ;
}
2018-04-29 14:21:19 +02:00
// adding to cache
dbController . addCache ( mainWindowController . getCurrentStreamUrl ( ) , omdbResponse ) ;
2018-04-06 20:33:56 +02:00
dbController . setCached ( mainWindowController . getCurrentStreamUrl ( ) ) ;
2018-03-08 17:59:28 +01:00
// load data to the MainWindowController
Platform . runLater ( ( ) - > {
2018-04-06 20:33:56 +02:00
dbController . readCache ( mainWindowController . getCurrentStreamUrl ( ) ) ;
2018-03-08 17:59:28 +01:00
} ) ;
2018-04-29 14:21:19 +02:00
return ;
}
2018-07-22 23:30:52 +02:00
/ * *
* get a movie / series by its title
* @param title of the movie / series
* @return a jsonObject of the API answer
* /
2018-04-29 14:21:19 +02:00
private JsonObject getByTitle ( String title ) {
String output = null ;
URL apiUrl ;
try {
2018-05-09 22:40:14 +02:00
if ( mainWindowController . getCurrentTableFilm ( ) . getSeason ( ) . length ( ) > 0 & & useEpisode ) {
2018-04-29 14:21:19 +02:00
apiUrl = new URL ( URL + mainWindowController . getOmdbAPIKey ( ) + " &t= "
+ title . replace ( " " , " %20 " )
+ " &Season= " + mainWindowController . getCurrentTableFilm ( ) . getSeason ( )
+ " &Episode= " + mainWindowController . getCurrentTableFilm ( ) . getEpisode ( ) ) ;
} else {
apiUrl = new URL ( URL + mainWindowController . getOmdbAPIKey ( ) + " &t= "
+ title . replace ( " " , " %20 " ) ) ;
}
BufferedReader ina = new BufferedReader ( new InputStreamReader ( apiUrl . openStream ( ) ) ) ;
output = ina . readLine ( ) ;
ina . close ( ) ;
LOGGER . info ( " response from ' " + URL + " &t= " + title + " ' was: " + output ) ;
} catch ( IOException e ) {
LOGGER . error ( " error while making api request or reading response " ) ;
LOGGER . error ( " response from ' " + URL + " &t= " + title + " ' was: " + output , e ) ;
return null ;
}
return Json . parse ( output ) . asObject ( ) ;
}
2018-05-09 22:40:14 +02:00
/ * *
2018-05-04 18:53:51 +02:00
* search for a movie / series title
* @param title the movie / series title
* @return the correct title if found
* /
2018-04-29 14:21:19 +02:00
private String searchByTitle ( String title ) {
String output = null ;
// 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 search for
* English name use tmdb
* /
try {
URL apiUrl = new URL ( URL + mainWindowController . getOmdbAPIKey ( ) + " &s= " + title . replace ( " " , " %20 " ) ) ;
BufferedReader ina = new BufferedReader ( new InputStreamReader ( apiUrl . openStream ( ) ) ) ;
output = ina . readLine ( ) ;
ina . close ( ) ;
LOGGER . info ( " response from ' " + URL + " &s= " + title + " ' was: " + output ) ;
} catch ( Exception e ) {
LOGGER . error ( " error while making api request or reading response " ) ;
LOGGER . error ( " response from ' " + URL + " &s= " + title + " ' was: " + output , e ) ;
return " " ;
}
JsonObject searchObject = Json . parse ( output ) . asObject ( ) ;
if ( searchObject . getString ( " Response " , " " ) . equals ( " True " ) ) {
for ( JsonValue movie : searchObject . get ( " Search " ) . asArray ( ) ) {
// get first entry from the array and set object = movie
2018-04-29 14:28:12 +02:00
// TODO if the search was successful, we should add the wrong and correct title to a list, for later speedup
2018-04-29 14:21:19 +02:00
return movie . asObject ( ) . getString ( " Title " , " " ) ;
}
} else {
LOGGER . warn ( " Movie not found! Not adding cache! " ) ;
}
return " " ;
2018-03-08 17:59:28 +01:00
}
}