2018-03-10 14:16:37 +01:00
/ * *
* Project - HomeFlix
*
2019-01-08 17:10:33 +01:00
* Copyright 2018 - 2019 < @Seil0 >
2018-03-10 14:16:37 +01:00
*
* 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 .
*
* /
2019-01-08 17:10:33 +01:00
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
2018-10-01 21:12:53 +02:00
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType ;
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 {
2018-10-01 21:12:53 +02:00
private DBController dbController ;
private FilmTabelDataType currentTableFilm ;
private String omdbAPIKey ;
2018-03-08 17:59:28 +01:00
private String URL = " https://www.omdbapi.com/?apikey= " ;
2019-01-14 18:44:36 +01:00
private static final Logger LOGGER = LogManager . getLogger ( OMDbAPIController . class . getName ( ) ) ;
2018-03-08 17:59:28 +01:00
2018-04-19 12:13:18 +02:00
/ * *
* constructor for the OMDbAPIController
* @param main the Main object
2018-10-01 21:12:53 +02:00
* @param dbController the DBController object
* @param currentTableFilm the current film object
* @param omdbAPIKey the omdbAPI key
2018-04-19 12:13:18 +02:00
* /
2019-06-22 21:04:43 +02:00
public OMDbAPIController ( FilmTabelDataType currentTableFilm ) {
2018-10-01 21:12:53 +02:00
this . currentTableFilm = currentTableFilm ;
2019-06-22 21:04:43 +02:00
omdbAPIKey = XMLController . getOmdbAPIKey ( ) ;
2019-06-19 16:31:49 +02:00
dbController = DBController . getInstance ( ) ;
2018-03-08 17:59:28 +01:00
}
@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 ;
2019-06-15 18:44:35 +02:00
if ( currentTableFilm . getSeason ( ) ! = null & & Integer . parseInt ( currentTableFilm . getSeason ( ) + 0 ) > 0 ) {
object = getByTitle ( currentTableFilm . getTitle ( ) , true ) ;
} else {
object = getByTitle ( currentTableFilm . getTitle ( ) , false ) ;
}
2018-12-20 13:47:32 +01:00
if ( object = = null ) {
LOGGER . error ( " Fatal error while querying omdbAPI! " ) ;
return ;
}
2018-03-08 17:59:28 +01:00
2018-12-20 13:47:32 +01:00
// if the answer contains "not found!" try to search by title
2018-04-29 14:21:19 +02:00
if ( object . getString ( " Error " , " " ) . contains ( " not found! " ) ) {
2018-10-01 21:12:53 +02:00
String title = searchByTitle ( currentTableFilm . getTitle ( ) ) ;
2018-04-29 14:21:19 +02:00
if ( title . length ( ) > 0 ) {
2019-06-15 18:44:35 +02:00
// we have at least one answer, get info by search title now
if ( currentTableFilm . getSeason ( ) ! = null & & Integer . parseInt ( currentTableFilm . getSeason ( ) + 0 ) > 0 ) {
object = getByTitle ( title , true ) ;
} else {
object = getByTitle ( title , false ) ;
2018-05-09 22:40:14 +02:00
}
2018-04-27 15:06:40 +02:00
} else {
2019-01-22 17:29:56 +01:00
// add default poster and cache
LOGGER . warn ( " Adding default poster and cache entries for \" {} \" ! " , currentTableFilm . getTitle ( ) ) ;
OMDbAPIResponseDataType omdbResponse = new OMDbAPIResponseDataType ( ) ;
omdbResponse . setTitle ( currentTableFilm . getTitle ( ) ) ;
omdbResponse . setSeason ( currentTableFilm . getSeason ( ) ) ;
omdbResponse . setEpisode ( currentTableFilm . getEpisode ( ) ) ;
synchronized ( this ) {
// adding to cache
dbController . addCache ( currentTableFilm . getStreamUrl ( ) , omdbResponse ) ;
dbController . setCached ( currentTableFilm . getStreamUrl ( ) ) ;
}
2018-04-27 15:06:40 +02:00
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 " , " " ) ) ;
2019-06-15 12:09:31 +02:00
// if a poster exist try resizing it to fit in the posterImageView and add it to the cache, else use the default
2019-06-15 18:44:35 +02:00
try {
BufferedImage originalImage = ImageIO . read ( new URL ( object . getString ( " Poster " , " " ) ) ) ;
// change path to where file is located
omdbResponse . setPoster ( XMLController . getPosterCache ( ) + " / " + omdbResponse . getTitle ( ) + " .png " ) ;
ImageIO . write ( originalImage , " png " , new File ( omdbResponse . getPoster ( ) ) ) ;
LOGGER . info ( " adding poster to cache: " + omdbResponse . getPoster ( ) ) ;
} catch ( Exception e ) {
LOGGER . warn ( " could not load poster, seting null -> using default " ) ;
2018-03-08 17:59:28 +01:00
}
2018-04-29 14:21:19 +02:00
2018-12-20 13:47:32 +01:00
synchronized ( this ) {
// adding to cache
dbController . addCache ( currentTableFilm . getStreamUrl ( ) , omdbResponse ) ;
dbController . setCached ( currentTableFilm . getStreamUrl ( ) ) ;
2018-12-08 23:44:17 +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
* /
2019-06-15 18:44:35 +02:00
private JsonObject getByTitle ( String title , boolean useEpisode ) {
2018-04-29 14:21:19 +02:00
String output = null ;
URL apiUrl ;
2019-06-15 18:44:35 +02:00
try {
if ( useEpisode ) {
2018-10-01 21:12:53 +02:00
apiUrl = new URL ( URL + omdbAPIKey + " &t= "
2018-04-29 14:21:19 +02:00
+ title . replace ( " " , " %20 " )
2018-10-01 21:12:53 +02:00
+ " &Season= " + currentTableFilm . getSeason ( )
2019-06-23 15:23:03 +02:00
+ " &Episode= " + currentTableFilm . getEpisode ( )
+ " &plot=full " ) ;
2018-04-29 14:21:19 +02:00
} else {
2018-10-01 21:12:53 +02:00
apiUrl = new URL ( URL + omdbAPIKey + " &t= "
2019-06-23 15:23:03 +02:00
+ title . replace ( " " , " %20 " ) + " &plot=full " ) ;
2018-04-29 14:21:19 +02:00
}
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 {
2018-10-01 21:12:53 +02:00
URL apiUrl = new URL ( URL + omdbAPIKey + " &s= " + title . replace ( " " , " %20 " ) ) ;
2018-04-29 14:21:19 +02:00
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
return movie . asObject ( ) . getString ( " Title " , " " ) ;
}
} else {
2019-01-22 17:29:56 +01:00
LOGGER . warn ( " Movie \" {} \" not found! " , title ) ;
2018-04-29 14:21:19 +02:00
}
return " " ;
2018-03-08 17:59:28 +01:00
}
}