2016-12-01 18:24:58 +01:00
/ * *
* apiQuery for Project HomeFlix
* sends a query to the omdb api
* /
package application ;
2017-04-09 15:07:56 +02:00
import java.awt.Graphics2D ;
import java.awt.image.BufferedImage ;
2017-03-06 17:59:44 +01:00
import java.io.BufferedReader ;
2017-04-09 15:07:56 +02:00
import java.io.File ;
2016-12-01 18:24:58 +01:00
import java.io.InputStream ;
2017-03-06 17:59:44 +01:00
import java.io.InputStreamReader ;
2016-12-01 18:24:58 +01:00
import java.net.URL ;
2017-03-09 22:26:37 +01:00
import java.util.ArrayList ;
2016-12-01 18:24:58 +01:00
import java.util.Scanner ;
2017-04-09 15:07:56 +02:00
import javax.imageio.ImageIO ;
2016-12-01 18:24:58 +01:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonObject ;
import javafx.scene.image.Image ;
2017-03-05 18:29:24 +01:00
import javafx.scene.text.Font ;
import javafx.scene.text.FontWeight ;
import javafx.scene.text.Text ;
2016-12-01 18:24:58 +01:00
2017-01-11 20:24:29 +01:00
public class apiQuery {
2016-12-01 18:24:58 +01:00
2017-04-06 12:07:34 +02:00
public apiQuery ( MainWindowController m , DBController db , Main main ) {
2016-12-01 18:24:58 +01:00
mainWindowController = m ;
2017-03-06 17:59:44 +01:00
dbController = db ;
2017-04-06 12:07:34 +02:00
this . main = main ;
2016-12-01 18:24:58 +01:00
}
private MainWindowController mainWindowController ;
2017-03-06 17:59:44 +01:00
private DBController dbController ;
2017-04-06 12:07:34 +02:00
private Main main ;
2017-01-26 16:21:27 +01:00
private Image im ;
2017-03-09 22:26:37 +01:00
private String [ ] responseString = new String [ 20 ] ;
2017-04-09 15:07:56 +02:00
private String posterCache ;
2017-04-30 23:17:55 +02:00
private String apiURL = " https://www.omdbapi.com/? " ;
2017-03-09 22:26:37 +01:00
ArrayList < Text > responseText = new ArrayList < Text > ( ) ;
ArrayList < Text > nameText = new ArrayList < Text > ( ) ;
2016-12-01 18:24:58 +01:00
2017-03-06 17:59:44 +01:00
void startQuery ( String titel , String streamUrl ) {
2017-04-30 23:17:55 +02:00
URL queryURL = null ;
2016-12-01 18:24:58 +01:00
Scanner sc = null ;
String moviename = null ;
String retdata = null ;
2017-04-09 15:07:56 +02:00
String posterPath = null ;
2016-12-01 18:24:58 +01:00
InputStream is = null ;
2017-03-06 17:59:44 +01:00
BufferedReader br = null ;
2017-04-06 12:07:34 +02:00
String fontFamily = main . getFONT_FAMILY ( ) ;
2017-04-09 15:07:56 +02:00
posterCache = main . getPosterCache ( ) . toString ( ) ;
2017-03-09 22:26:37 +01:00
int fontSize = ( int ) Math . round ( mainWindowController . size ) ;
responseText . removeAll ( responseText ) ;
nameText . removeAll ( nameText ) ;
2016-12-01 18:24:58 +01:00
try {
//get film title
sc = new Scanner ( System . in ) ;
2017-03-06 17:59:44 +01:00
moviename = titel ;
2016-12-01 18:24:58 +01:00
// in case of no or "" Film title
if ( moviename = = null | | moviename . equals ( " " ) ) {
System . out . println ( " No movie found " ) ;
}
//remove unwanted blank
moviename = moviename . trim ( ) ;
2017-04-30 23:17:55 +02:00
//replace blank with +
2016-12-01 18:24:58 +01:00
moviename = moviename . replace ( " " , " + " ) ;
2017-04-30 23:17:55 +02:00
//queryURL is apiURL and additional parameters, response-types: http,json,xml (must be json, since the response is processed with minimal-json )
queryURL = new URL ( apiURL + " t= " + moviename + " &plot=full&r=json " ) ;
is = queryURL . openStream ( ) ;
2017-03-06 17:59:44 +01:00
br = new BufferedReader ( new InputStreamReader ( is , " UTF-8 " ) ) ;
2016-12-01 18:24:58 +01:00
2017-04-30 23:17:55 +02:00
//read data from response Stream
2017-03-06 17:59:44 +01:00
while ( ( retdata = br . readLine ( ) ) ! = null ) {
2017-03-09 22:26:37 +01:00
//cut the json response into separate strings
2016-12-01 18:24:58 +01:00
System . out . println ( retdata ) ;
JsonObject object = Json . parse ( retdata ) . asObject ( ) ;
2017-03-05 18:29:24 +01:00
2017-03-09 22:26:37 +01:00
responseString [ 0 ] = object . getString ( " Title " , " " ) ;
responseString [ 1 ] = object . getString ( " Year " , " " ) ;
responseString [ 2 ] = object . getString ( " Rated " , " " ) ;
responseString [ 3 ] = object . getString ( " Released " , " " ) ;
responseString [ 4 ] = object . getString ( " Runtime " , " " ) ;
responseString [ 5 ] = object . getString ( " Genre " , " " ) ;
responseString [ 6 ] = object . getString ( " Director " , " " ) ;
responseString [ 7 ] = object . getString ( " Writer " , " " ) ;
responseString [ 8 ] = object . getString ( " Actors " , " " ) ;
responseString [ 9 ] = object . getString ( " Plot " , " " ) ;
responseString [ 10 ] = object . getString ( " Language " , " " ) ;
responseString [ 11 ] = object . getString ( " Country " , " " ) ;
responseString [ 12 ] = object . getString ( " Awards " , " " ) ;
responseString [ 13 ] = object . getString ( " Metascore " , " " ) ;
responseString [ 14 ] = object . getString ( " imdbRating " , " " ) ;
responseString [ 15 ] = object . getString ( " Type " , " " ) ;
responseString [ 16 ] = object . getString ( " imdbVotes " , " " ) ;
responseString [ 17 ] = object . getString ( " imdbID " , " " ) ;
responseString [ 18 ] = object . getString ( " Poster " , " " ) ;
responseString [ 19 ] = object . getString ( " Response " , " " ) ;
2016-12-01 18:24:58 +01:00
2017-04-09 15:07:56 +02:00
//adding poster to cache
BufferedImage originalImage = ImageIO . read ( new URL ( responseString [ 18 ] ) ) ; //change path to where file is located
int type = originalImage . getType ( ) = = 0 ? BufferedImage . TYPE_INT_ARGB : originalImage . getType ( ) ;
BufferedImage resizeImagePNG = resizeImage ( originalImage , type , 198 , 297 ) ;
if ( System . getProperty ( " os.name " ) . equals ( " Linux " ) ) {
posterPath = posterCache + " / " + titel + " .png " ;
ImageIO . write ( resizeImagePNG , " png " , new File ( posterCache + " / " + titel + " .png " ) ) ; //change path where you want it saved
} else {
ImageIO . write ( resizeImagePNG , " png " , new File ( posterCache + " \\ " + titel + " .png " ) ) ; //change path where you want it saved
posterPath = posterCache + " \\ " + titel + " .png " ;
}
System . out . println ( " adding poster to cache: " + posterPath ) ;
2017-03-09 22:26:37 +01:00
//adding strings to the cache
dbController . addCache ( streamUrl , responseString [ 0 ] , responseString [ 1 ] , responseString [ 2 ] , responseString [ 3 ] , responseString [ 4 ] , responseString [ 5 ] ,
responseString [ 6 ] , responseString [ 7 ] , responseString [ 8 ] , responseString [ 9 ] , responseString [ 10 ] , responseString [ 11 ] , responseString [ 12 ] ,
2017-04-09 15:07:56 +02:00
responseString [ 13 ] , responseString [ 14 ] , responseString [ 15 ] , responseString [ 16 ] , responseString [ 17 ] , posterPath ,
2017-03-09 22:26:37 +01:00
responseString [ 19 ] ) ;
2017-03-06 17:59:44 +01:00
dbController . setCached ( streamUrl ) ;
2017-03-05 18:29:24 +01:00
2017-03-09 22:26:37 +01:00
for ( int i = 0 ; i < 20 ; i + + ) {
Text text = new Text ( responseString [ i ] + " \ n " ) ;
responseText . add ( text ) ;
responseText . get ( i ) . setFont ( Font . font ( fontFamily , fontSize ) ) ;
}
2016-12-01 18:24:58 +01:00
2017-03-09 22:26:37 +01:00
//if response == false then show mainWindowController.noFilmFound else create new Texts and add them to flowText
if ( responseString [ 19 ] . equals ( " False " ) ) {
mainWindowController . textFlow . getChildren ( ) . add ( new Text ( mainWindowController . noFilmFound ) ) ;
2017-06-04 20:24:08 +02:00
im = new Image ( " resources/icons/close_black_2048x2048.png " ) ;
2017-01-26 16:21:27 +01:00
mainWindowController . image1 . setImage ( im ) ;
2016-12-01 18:24:58 +01:00
} else {
2017-03-09 22:26:37 +01:00
nameText . add ( 0 , new Text ( mainWindowController . title + " : " ) ) ;
nameText . add ( 1 , new Text ( mainWindowController . year + " : " ) ) ;
nameText . add ( 2 , new Text ( mainWindowController . rating + " : " ) ) ;
nameText . add ( 3 , new Text ( mainWindowController . publishedOn + " : " ) ) ;
nameText . add ( 4 , new Text ( mainWindowController . duration + " : " ) ) ;
nameText . add ( 5 , new Text ( mainWindowController . genre + " : " ) ) ;
nameText . add ( 6 , new Text ( mainWindowController . director + " : " ) ) ;
nameText . add ( 7 , new Text ( mainWindowController . writer + " : " ) ) ;
nameText . add ( 8 , new Text ( mainWindowController . actors + " : " ) ) ;
nameText . add ( 9 , new Text ( mainWindowController . plot + " : " ) ) ;
nameText . add ( 10 , new Text ( mainWindowController . language + " : " ) ) ;
nameText . add ( 11 , new Text ( mainWindowController . country + " : " ) ) ;
nameText . add ( 12 , new Text ( mainWindowController . awards + " : " ) ) ;
nameText . add ( 13 , new Text ( mainWindowController . metascore + " : " ) ) ;
nameText . add ( 14 , new Text ( mainWindowController . imdbRating + " : " ) ) ;
nameText . add ( 15 , new Text ( mainWindowController . type + " : " ) ) ;
for ( int i = 0 ; i < nameText . size ( ) ; i + + ) {
nameText . get ( i ) . setFont ( Font . font ( fontFamily , FontWeight . BOLD , fontSize ) ) ;
}
2017-03-05 18:29:24 +01:00
2017-03-09 22:26:37 +01:00
mainWindowController . textFlow . getChildren ( ) . remove ( 0 , mainWindowController . textFlow . getChildren ( ) . size ( ) ) ;
2017-03-05 18:29:24 +01:00
2017-03-09 22:26:37 +01:00
for ( int i = 0 ; i < nameText . size ( ) ; i + + ) {
mainWindowController . textFlow . getChildren ( ) . addAll ( nameText . get ( i ) , responseText . get ( i ) ) ;
}
//if there is no poster
if ( responseString [ 18 ] . equals ( " N/A " ) ) {
2017-06-04 20:24:08 +02:00
im = new Image ( " resources/icons/close_black_2048x2048.png " ) ;
2017-01-26 16:21:27 +01:00
} else {
2017-03-09 22:26:37 +01:00
im = new Image ( responseString [ 18 ] ) ;
2017-01-26 16:21:27 +01:00
}
mainWindowController . image1 . setImage ( im ) ;
2016-12-01 18:24:58 +01:00
}
}
} catch ( Exception e ) {
2017-06-06 02:12:59 +02:00
mainWindowController . textFlow . getChildren ( ) . remove ( 0 , mainWindowController . textFlow . getChildren ( ) . size ( ) ) ;
2017-03-09 22:26:37 +01:00
mainWindowController . textFlow . getChildren ( ) . add ( new Text ( e . toString ( ) ) ) ;
2016-12-01 18:24:58 +01:00
System . out . println ( e ) ;
} finally {
//closes datainputStream, InputStream,Scanner if not already done
try {
2017-03-06 17:59:44 +01:00
if ( br ! = null ) {
br . close ( ) ;
2016-12-01 18:24:58 +01:00
}
if ( is ! = null ) {
is . close ( ) ;
}
if ( sc ! = null ) {
sc . close ( ) ;
}
2017-04-30 23:17:55 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2016-12-01 18:24:58 +01:00
}
}
}
2017-04-09 15:07:56 +02:00
private static BufferedImage resizeImage ( BufferedImage originalImage , int type , int IMG_WIDTH , int IMG_HEIGHT ) {
BufferedImage resizedImage = new BufferedImage ( IMG_WIDTH , IMG_HEIGHT , type ) ;
Graphics2D g = resizedImage . createGraphics ( ) ;
g . drawImage ( originalImage , 0 , 0 , IMG_WIDTH , IMG_HEIGHT , null ) ;
g . dispose ( ) ;
return resizedImage ;
}
2016-12-01 18:24:58 +01:00
}