2018-03-01 15:42:47 +01:00
/ * *
* Project - HomeFlix
*
2019-01-08 17:10:33 +01:00
* Copyright 2016 - 2019 < @Seil0 >
2018-03-01 15:42:47 +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 .
* /
2018-12-04 22:31:11 +01:00
2020-04-22 18:29:39 +02:00
package org.mosad.homeflix.controller ;
2018-03-01 15:42:47 +01:00
2019-05-15 17:14:15 +02:00
import java.io.File ;
2018-03-01 15:42:47 +01:00
import java.io.FileNotFoundException ;
import java.io.IOException ;
2018-04-05 12:09:39 +02:00
import java.net.URLConnection ;
2018-03-01 15:42:47 +01:00
import java.sql.Connection ;
2019-01-22 17:29:56 +01:00
import java.sql.Date ;
2018-03-01 15:42:47 +01:00
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Statement ;
2019-06-15 12:09:31 +02:00
import java.time.LocalDate ;
2018-03-01 15:42:47 +01:00
import java.util.ArrayList ;
import java.util.List ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2020-04-22 18:29:39 +02:00
import org.mosad.homeflix.datatypes.DatabaseDataType ;
import org.mosad.homeflix.datatypes.FilmTabelDataType ;
import org.mosad.homeflix.datatypes.OMDbAPIResponseDataType ;
import org.mosad.homeflix.datatypes.PosterModeElement ;
2018-03-01 15:42:47 +01:00
2019-01-14 18:44:36 +01:00
import javafx.collections.FXCollections ;
2018-04-29 14:21:19 +02:00
import javafx.collections.ObservableList ;
2018-03-01 15:42:47 +01:00
import javafx.scene.image.Image ;
public class DBController {
2018-04-02 18:29:59 +02:00
2019-01-14 18:44:36 +01:00
private static DBController instance = null ;
2018-05-16 23:02:07 +02:00
private String DB_PATH ;
2019-01-22 17:29:56 +01:00
private List < DatabaseDataType > databaseStreams = new ArrayList < DatabaseDataType > ( ) ; // contains all films stored in the database
2018-12-04 22:31:11 +01:00
private List < DatabaseDataType > sourceStreams = new ArrayList < DatabaseDataType > ( ) ; // contains all films from the sources
2018-03-01 15:42:47 +01:00
private Connection connection = null ;
private static final Logger LOGGER = LogManager . getLogger ( DBController . class . getName ( ) ) ;
2018-04-02 18:29:59 +02:00
/ * *
* constructor for DBController
* /
2019-01-14 18:44:36 +01:00
public DBController ( ) {
// Auto-generated constructor stub
}
public static DBController getInstance ( ) {
if ( instance = = null ) {
instance = new DBController ( ) ;
}
return instance ;
2018-04-02 18:29:59 +02:00
}
/ * *
2019-06-15 11:09:59 +02:00
* initialize the { @link DBController } with the database connection check if
* there is a need to create a new database refresh the database
2018-04-02 18:29:59 +02:00
* /
2018-03-01 15:42:47 +01:00
public void init ( ) {
2018-03-04 20:07:43 +01:00
initDatabaseConnection ( ) ;
2018-03-01 15:42:47 +01:00
createDatabase ( ) ;
}
2018-04-02 18:29:59 +02:00
/ * *
* create a new connection to the HomeFlix . db database
* AutoCommit is set to false to prevent some issues , so manual commit is active !
* /
2018-03-05 22:37:32 +01:00
private void initDatabaseConnection ( ) {
2019-01-08 17:10:33 +01:00
DB_PATH = XMLController . getDirHomeFlix ( ) + " /Homeflix.db " ;
2018-03-01 15:42:47 +01:00
try {
// create a database connection
connection = DriverManager . getConnection ( " jdbc:sqlite: " + DB_PATH ) ;
2018-04-02 18:29:59 +02:00
connection . setAutoCommit ( false ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
// if the error message is "out of memory", it probably means no database file is found
2019-01-22 17:29:56 +01:00
LOGGER . error ( " error while loading the HomeFlix database " , e ) ;
2018-03-01 15:42:47 +01:00
}
2019-01-22 17:29:56 +01:00
LOGGER . info ( " HomeFlix database loaded successfull " ) ;
2018-03-01 15:42:47 +01:00
}
2018-03-04 20:07:43 +01:00
/ * *
* if tables don ' t exist create them
2018-04-02 18:29:59 +02:00
* films table : streamUrl is primary key
2018-03-04 20:07:43 +01:00
* cache table : streamUrl is primary key
* /
2018-04-29 14:21:19 +02:00
private void createDatabase ( ) {
2020-04-22 18:29:39 +02:00
try ( Statement stmt = connection . createStatement ( ) ; ) {
2018-04-02 02:18:43 +02:00
stmt . executeUpdate ( " create table if not exists films (streamUrl, title, season, episode, favorite, cached, currentTime) " ) ;
2018-03-03 16:51:14 +01:00
stmt . executeUpdate ( " create table if not exists cache ( "
2018-04-29 14:21:19 +02:00
+ " streamUrl, Title, Year, Rated, Released, Season, Episode ,Runtime, Genre, Director, Writer, "
+ " Actors, Plot, Language, Country, Awards, Poster, Metascore, imdbRating, imdbVotes, "
+ " imdbID, Type, dvd, BoxOffice, Website, Response) " ) ;
2019-06-15 11:09:59 +02:00
connection . commit ( ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( e ) ;
2018-03-04 20:07:43 +01:00
}
}
2018-04-02 18:29:59 +02:00
/ * *
* get all database entries
* /
2018-03-04 20:07:43 +01:00
private void loadDatabase ( ) {
2018-03-07 00:20:14 +01:00
// get all entries from the table
2020-04-22 18:29:39 +02:00
try ( Statement stmt = connection . createStatement ( ) ; ) {
try ( ResultSet rs = stmt . executeQuery ( " SELECT * FROM films " ) ; ) {
while ( rs . next ( ) ) {
databaseStreams . add ( new DatabaseDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) ,
rs . getInt ( " favorite " ) , rs . getDouble ( " currentTime " ) ) ) ;
}
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
2018-03-03 16:51:14 +01:00
}
2018-04-02 18:29:59 +02:00
/ * *
2019-06-15 12:09:31 +02:00
* load all source streams
2018-04-02 18:29:59 +02:00
* /
2018-03-04 20:07:43 +01:00
private void loadSources ( ) {
2019-01-12 23:07:25 +01:00
SourcesController sourcesController = new SourcesController ( ) ;
2018-12-04 22:31:11 +01:00
sourceStreams = sourcesController . loadSources ( ) ;
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
/ * *
* try to close the databse connection
* /
public void closeDBConnection ( ) {
try {
connection . commit ( ) ;
connection . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Error while trying to close the databse connection " , e ) ;
}
}
2018-04-02 18:29:59 +02:00
/ * *
2019-06-15 11:09:59 +02:00
* load all streams from the database to a ObservableList , order entries by title
2019-05-15 17:14:15 +02:00
* @return a ObservableList that contains all streams from the database
2018-04-02 18:29:59 +02:00
* /
2019-05-15 17:14:15 +02:00
public ObservableList < FilmTabelDataType > getStreamsList ( ) {
2019-01-14 18:44:36 +01:00
ObservableList < FilmTabelDataType > filmsList = FXCollections . observableArrayList ( ) ;
2019-01-22 17:29:56 +01:00
LOGGER . info ( " loading data from database ... " ) ;
2020-04-22 18:29:39 +02:00
//load local Data
try ( Statement stmt = connection . createStatement ( ) ; ) {
2018-12-04 22:31:11 +01:00
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = stmt . executeQuery ( " SELECT * FROM films ORDER BY title " ) ; ) {
while ( rs . next ( ) ) {
filmsList . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) ) ) ;
}
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
2019-01-14 18:44:36 +01:00
return filmsList ;
2018-03-01 15:42:47 +01:00
}
2018-03-05 22:37:32 +01:00
/ * *
2019-05-15 17:14:15 +02:00
* get one stream from the database with streamUrl = ?
* @param streamUrl of the stream
* @return a FilmTabelDataType Object of the stream with the given streamUrl
2018-03-05 22:37:32 +01:00
* /
2019-05-15 17:14:15 +02:00
public FilmTabelDataType getStream ( String streamUrl ) {
2019-01-14 18:44:36 +01:00
FilmTabelDataType film = null ;
2018-12-04 22:31:11 +01:00
LOGGER . info ( " refresh data for " + streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
2018-03-05 22:37:32 +01:00
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
while ( rs . next ( ) ) {
film = new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) ) ;
}
2018-03-05 22:37:32 +01:00
}
2020-04-22 18:29:39 +02:00
2018-03-05 22:37:32 +01:00
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while refreshing mwc! " , e ) ;
2019-01-14 18:44:36 +01:00
}
return film ;
2018-03-01 15:42:47 +01:00
}
2018-03-04 20:07:43 +01:00
2019-05-15 17:14:15 +02:00
/ * *
2019-06-15 18:44:35 +02:00
* get entries which have no season and episode eg . root or movie of the database as PosterModeElement
2019-05-15 17:14:15 +02:00
* @return a ObservableList of PosterModeElements
* /
public ObservableList < PosterModeElement > getPosterElementsList ( ) {
ObservableList < PosterModeElement > posterElementsList = FXCollections . observableArrayList ( ) ;
LOGGER . info ( " loading data from database ... " ) ;
2020-04-22 18:29:39 +02:00
//load local Data
try ( Statement stmt = connection . createStatement ( ) ; ) {
2019-05-15 17:14:15 +02:00
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE season = '' OR season = '0' ORDER BY title " ) ; ) {
while ( rs . next ( ) ) {
String [ ] cacheData = readCacheLegacy ( rs . getString ( " streamUrl " ) ) ; // get from the cache table
if ( cacheData [ 20 ] ! = null & & cacheData [ 20 ] . length ( ) > 0 ) {
posterElementsList . add ( new PosterModeElement ( rs . getString ( " streamUrl " ) , cacheData [ 0 ] , new Image ( new File ( cacheData [ 20 ] ) . toURI ( ) . toString ( ) ) ) ) ;
} else {
posterElementsList . add ( new PosterModeElement ( rs . getString ( " streamUrl " ) , cacheData [ 0 ] , new Image ( " icons/Homeflix_Poster.png " ) ) ) ;
}
2019-05-15 17:14:15 +02:00
}
}
2020-04-22 18:29:39 +02:00
2019-05-15 17:14:15 +02:00
} catch ( SQLException e ) {
LOGGER . error ( " An error occured while getting all PosterElements! " , e ) ;
}
return posterElementsList ;
}
2018-03-04 20:07:43 +01:00
/ * *
* refresh database to contain all ( new added ) entries
* /
public void refreshDataBase ( ) {
2019-01-22 17:29:56 +01:00
LOGGER . info ( " <========== starting refreshing database ==========> " ) ;
2018-03-04 20:07:43 +01:00
// clean all ArraLists
2019-01-22 17:29:56 +01:00
databaseStreams . clear ( ) ;
2018-12-04 22:31:11 +01:00
sourceStreams . clear ( ) ;
2018-03-04 20:07:43 +01:00
loadSources ( ) ; // reload all sources
loadDatabase ( ) ; // reload all films saved in the DB
2019-05-15 17:14:15 +02:00
LOGGER . info ( " There are {} entries in the Database " , databaseStreams . size ( ) ) ;
2018-05-16 23:02:07 +02:00
2018-03-04 20:07:43 +01:00
try {
checkAddEntry ( ) ;
2018-03-07 00:20:14 +01:00
checkRemoveEntry ( ) ;
2018-03-04 20:07:43 +01:00
} catch ( Exception e ) {
LOGGER . error ( " Error while refreshing the database " , e ) ;
}
2019-01-22 17:29:56 +01:00
LOGGER . info ( " <========== finished refreshing database ==========> " ) ;
2018-03-04 20:07:43 +01:00
}
2018-03-01 15:42:47 +01:00
/ * *
* check if there are any entries that have been removed from the film - directory
2018-12-04 22:31:11 +01:00
* @throws SQLException
2018-03-01 15:42:47 +01:00
* /
2020-04-22 18:29:39 +02:00
private void checkRemoveEntry ( ) {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " checking for entrys to remove to DB ... " ) ;
2018-12-04 22:31:11 +01:00
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " DELETE FROM films WHERE streamUrl = ? " ) ; ) {
for ( DatabaseDataType dbStreamEntry : databaseStreams ) {
// if the directory doen't contain the entry form the database, remove it
// if sourceStreams has a item where StreamUrl equals dbStreamEntry.getStreamUrl() return it, else null
DatabaseDataType result = sourceStreams . stream ( )
. filter ( x - > dbStreamEntry . getStreamUrl ( ) . equals ( x . getStreamUrl ( ) ) )
. findAny ( )
. orElse ( null ) ;
// if the result is null, the file is missing, remove it from the database
if ( result = = null ) {
ps . setString ( 1 , dbStreamEntry . getStreamUrl ( ) ) ;
ps . addBatch ( ) ;
LOGGER . info ( " removed \" " + dbStreamEntry . getTitle ( ) + " \" from database " ) ;
}
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
ps . executeBatch ( ) ;
connection . commit ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " An error occured, while cheking for media to remove " , e ) ;
2018-03-01 15:42:47 +01:00
}
}
/ * *
2020-04-22 18:29:39 +02:00
* check for new media in the source directories
2018-03-01 15:42:47 +01:00
* @throws FileNotFoundException
* @throws IOException
* /
2020-04-22 18:29:39 +02:00
private void checkAddEntry ( ) throws FileNotFoundException , IOException {
2018-03-04 20:07:43 +01:00
LOGGER . info ( " checking for entrys to add to DB ... " ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " insert into films values (?, ?, ?, ?, ?, ?, ?) " ) ; ) {
for ( DatabaseDataType sourceStreamEntry : sourceStreams ) {
// if databaseStream has a item where StreamUrl equals sourceStreamEntry.getStreamUrl() return it, else null
DatabaseDataType result = databaseStreams . stream ( )
. filter ( x - > sourceStreamEntry . getStreamUrl ( ) . equals ( x . getStreamUrl ( ) ) )
. findAny ( )
. orElse ( null ) ;
// if the result is null, the entry is missing, add it to the database
if ( result = = null ) {
ps . setString ( 1 , sourceStreamEntry . getStreamUrl ( ) ) ;
ps . setString ( 2 , sourceStreamEntry . getTitle ( ) ) ;
ps . setString ( 3 , sourceStreamEntry . getSeason ( ) ) ;
ps . setString ( 4 , sourceStreamEntry . getEpisode ( ) ) ;
ps . setInt ( 5 , sourceStreamEntry . getFavorite ( ) ) ;
ps . setDate ( 6 , new Date ( 0 ) ) ;
ps . setDouble ( 7 , sourceStreamEntry . getCurrentTime ( ) ) ;
ps . addBatch ( ) ; // adds the entry
LOGGER . info ( " Added \" " + sourceStreamEntry . getTitle ( ) + " \" to database " ) ;
databaseStreams . add ( sourceStreamEntry ) ;
}
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
ps . executeBatch ( ) ;
connection . commit ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " An error occured, while cheking for new media " , e ) ;
2018-03-01 15:42:47 +01:00
}
2018-11-19 22:49:09 +01:00
}
2018-04-02 18:29:59 +02:00
/ * *
* DEBUG
* prints all entries from the database to the console
* /
2018-03-05 16:51:51 +01:00
public void printAllDBEntriesDEBUG ( ) {
System . out . println ( " Outputting all entries ... \ n " ) ;
2020-04-22 18:29:39 +02:00
try ( Statement stmt = connection . createStatement ( ) ; ) {
try ( ResultSet rs = stmt . executeQuery ( " SELECT * FROM films " ) ; ) {
while ( rs . next ( ) ) {
System . out . println ( rs . getString ( " streamUrl " ) ) ;
System . out . println ( rs . getString ( " title " ) ) ;
System . out . println ( rs . getString ( " season " ) ) ;
System . out . println ( rs . getString ( " episode " ) ) ;
System . out . println ( rs . getString ( " rating " ) ) ;
System . out . println ( rs . getString ( " cached " ) ) ;
System . out . println ( rs . getString ( " currentTime " ) + " \ n " ) ;
}
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
2018-03-05 16:51:51 +01:00
LOGGER . error ( " An error occured, while printing all entries " , e ) ;
2018-03-01 15:42:47 +01:00
}
}
2019-06-16 21:44:42 +02:00
/ * *
2020-04-22 18:29:39 +02:00
* return the favorite status of a media entry
* @param mediaURL URL of the media
2019-06-16 21:44:42 +02:00
* @return 0 if it ' s not a favorite , else 1
* /
2020-04-22 18:29:39 +02:00
public int getFavoriteState ( String mediaURL ) {
2019-06-16 21:44:42 +02:00
int favoriteState = 0 ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT favorite FROM films WHERE streamUrl = ? " ) ; ) {
ps . setString ( 1 , mediaURL ) ;
2019-06-16 21:44:42 +02:00
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
while ( rs . next ( ) ) {
favoriteState = rs . getInt ( " favorite " ) ;
}
2019-06-16 21:44:42 +02:00
}
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
return favoriteState ;
}
public void toggleFavoriteState ( String streamURL ) {
2020-04-22 18:29:39 +02:00
int fav = getFavoriteState ( streamURL ) = = 0 ? 1 : 0 ;
try ( PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET favorite = ? WHERE streamUrl = ? " ) ; ) {
ps . setInt ( 1 , fav ) ;
ps . setString ( 2 , streamURL ) ;
2019-06-16 21:44:42 +02:00
ps . executeUpdate ( ) ;
connection . commit ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-03-05 22:37:32 +01:00
/ * *
2018-03-09 20:12:03 +01:00
* update the database entry for the given film , favorite = 0
2018-03-05 22:37:32 +01:00
* @param streamUrl URL of the film
* /
public void dislike ( String streamUrl ) {
LOGGER . info ( " dislike " + streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET favorite = 0 WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-03-05 22:37:32 +01:00
connection . commit ( ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-03-05 22:37:32 +01:00
/ * *
2018-03-09 20:12:03 +01:00
* update the database entry for the given film , favorite = 1
2018-03-05 22:37:32 +01:00
* @param streamUrl URL of the film
* /
public void like ( String streamUrl ) {
LOGGER . info ( " like " + streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET favorite = 1 WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
} catch ( SQLException e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! An error occured! " , e ) ;
2018-03-01 15:42:47 +01:00
}
2018-03-05 22:37:32 +01:00
}
/ * *
* update the database entry for the given film , cached = 1
* @param streamUrl URL of the film
* /
2018-05-16 23:02:07 +02:00
public void setCached ( String streamUrl ) {
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET cached = ? WHERE streamUrl = ? " ) ; ) {
2019-01-22 17:29:56 +01:00
ps . setDate ( 1 , new Date ( System . currentTimeMillis ( ) ) ) ;
ps . setString ( 2 , streamUrl ) ;
2018-12-04 22:31:11 +01:00
ps . executeUpdate ( ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
} catch ( SQLException e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! An error occured, while setting cached = DATE for {}. " , streamUrl , e ) ;
2018-03-01 15:42:47 +01:00
}
}
2019-06-15 12:09:31 +02:00
/ * *
* get the cached date for the film
* @param streamUrl URL of the film
* /
2019-06-18 01:25:46 +02:00
public LocalDate getCacheDate ( String streamUrl ) {
2019-06-15 12:09:31 +02:00
LocalDate cacheDate = LocalDate . now ( ) . minusDays ( 31 ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT cached FROM films WHERE streamUrl = ? " ) ; ) {
2019-06-15 12:09:31 +02:00
ps . setString ( 1 , streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
while ( rs . next ( ) ) {
cacheDate = rs . getDate ( " cached " ) . toLocalDate ( ) ;
}
2019-06-15 12:09:31 +02:00
}
} catch ( SQLException e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! An error occured, while rerading the cached date for {}. " , streamUrl , e ) ;
2019-06-15 12:09:31 +02:00
}
return cacheDate ;
}
2018-03-05 22:37:32 +01:00
/ * *
* add the received data to the cache table
2018-04-29 14:21:19 +02:00
* @param streamUrl URL of the film
* @param omdbResponse the response data from omdbAPI
2018-03-05 22:37:32 +01:00
* /
2018-05-16 23:02:07 +02:00
public void addCache ( String streamUrl , OMDbAPIResponseDataType omdbResponse ) {
2020-04-22 18:29:39 +02:00
LOGGER . info ( " adding cache for: " + streamUrl ) ;
try ( PreparedStatement ps = connection . prepareStatement ( " insert into cache values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " ) ; ) {
2018-03-08 17:59:28 +01:00
ps . setString ( 1 , streamUrl ) ;
2018-04-29 14:21:19 +02:00
ps . setString ( 2 , omdbResponse . getTitle ( ) ) ;
ps . setString ( 3 , omdbResponse . getYear ( ) ) ;
ps . setString ( 4 , omdbResponse . getRated ( ) ) ;
ps . setString ( 5 , omdbResponse . getReleased ( ) ) ;
ps . setString ( 6 , omdbResponse . getSeason ( ) ) ;
ps . setString ( 7 , omdbResponse . getEpisode ( ) ) ;
ps . setString ( 8 , omdbResponse . getRuntime ( ) ) ;
ps . setString ( 9 , omdbResponse . getGenre ( ) ) ;
ps . setString ( 10 , omdbResponse . getDirector ( ) ) ;
ps . setString ( 11 , omdbResponse . getWriter ( ) ) ;
ps . setString ( 12 , omdbResponse . getActors ( ) ) ;
ps . setString ( 13 , omdbResponse . getPlot ( ) ) ;
ps . setString ( 14 , omdbResponse . getLanguage ( ) ) ;
ps . setString ( 15 , omdbResponse . getCountry ( ) ) ;
ps . setString ( 16 , omdbResponse . getAwards ( ) ) ;
ps . setString ( 17 , omdbResponse . getPoster ( ) ) ;
ps . setString ( 18 , omdbResponse . getMetascore ( ) ) ;
ps . setString ( 19 , omdbResponse . getImdbRating ( ) ) ;
ps . setString ( 20 , omdbResponse . getImdbVotes ( ) ) ;
ps . setString ( 21 , omdbResponse . getImdbID ( ) ) ;
ps . setString ( 22 , omdbResponse . getType ( ) ) ;
ps . setString ( 23 , omdbResponse . getDvd ( ) ) ;
ps . setString ( 24 , omdbResponse . getBoxOffice ( ) ) ;
ps . setString ( 25 , omdbResponse . getWebsite ( ) ) ;
ps . setString ( 26 , omdbResponse . getResponse ( ) ) ;
2018-03-08 17:59:28 +01:00
ps . addBatch ( ) ;
ps . executeBatch ( ) ;
connection . commit ( ) ;
} catch ( Exception e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! An error while adding cache data for {}. " , streamUrl , e ) ;
2018-03-08 17:59:28 +01:00
}
2018-03-01 15:42:47 +01:00
}
2018-04-15 21:30:26 +02:00
/ * *
* checks if there is already a entry with the given streamUrl in the cache
* @param streamUrl URL of the element
* @return true if the element is already cached , else false
* /
2018-04-29 14:21:19 +02:00
public boolean searchCacheByURL ( String streamUrl ) {
2018-04-14 16:14:10 +02:00
boolean retValue = false ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM cache WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
retValue = rs . next ( ) ;
}
2018-04-14 16:14:10 +02:00
} catch ( Exception e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! An error while searching for {} in the cache. " , streamUrl , e ) ;
2018-04-14 16:14:10 +02:00
}
return retValue ;
}
2018-03-05 22:37:32 +01:00
/ * *
2019-01-14 18:44:36 +01:00
* read the cached data from the Database
2018-03-05 22:37:32 +01:00
* @param streamUrl URL of the film
2019-01-14 18:44:36 +01:00
* @return a String array ( length = 21 ) with all cached data
2018-03-05 22:37:32 +01:00
* /
2020-04-22 18:29:39 +02:00
public String [ ] readCacheLegacy ( String streamUrl ) {
2019-01-14 18:44:36 +01:00
String [ ] cacheData = new String [ 21 ] ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM cache WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
2018-05-09 22:40:14 +02:00
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
while ( rs . next ( ) ) {
cacheData [ 0 ] = rs . getString ( " Title " ) ;
cacheData [ 1 ] = rs . getString ( " Year " ) ;
cacheData [ 2 ] = rs . getString ( " Rated " ) ;
cacheData [ 3 ] = rs . getString ( " Released " ) ;
cacheData [ 4 ] = rs . getString ( " Season " ) ;
cacheData [ 5 ] = rs . getString ( " Episode " ) ;
cacheData [ 6 ] = rs . getString ( " Runtime " ) ;
cacheData [ 7 ] = rs . getString ( " Genre " ) ;
cacheData [ 8 ] = rs . getString ( " Director " ) ;
cacheData [ 9 ] = rs . getString ( " Writer " ) ;
cacheData [ 10 ] = rs . getString ( " Actors " ) ;
cacheData [ 11 ] = rs . getString ( " Plot " ) ;
cacheData [ 12 ] = rs . getString ( " Language " ) ;
cacheData [ 13 ] = rs . getString ( " Country " ) ;
cacheData [ 14 ] = rs . getString ( " Awards " ) ;
cacheData [ 15 ] = rs . getString ( " metascore " ) ;
cacheData [ 16 ] = rs . getString ( " imdbRating " ) ;
cacheData [ 17 ] = rs . getString ( " Type " ) ;
cacheData [ 18 ] = rs . getString ( " BoxOffice " ) ;
cacheData [ 19 ] = rs . getString ( " Website " ) ;
cacheData [ 20 ] = rs . getString ( " Poster " ) ;
}
2018-03-01 15:42:47 +01:00
}
2018-04-29 14:21:19 +02:00
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
2019-10-27 14:09:06 +01:00
LOGGER . error ( " Ups! error while getting the current time for {}. " , streamUrl , e ) ;
2018-03-01 15:42:47 +01:00
}
2019-01-14 18:44:36 +01:00
return cacheData ;
2018-03-01 15:42:47 +01:00
}
2020-04-22 18:29:39 +02:00
public OMDbAPIResponseDataType readCache ( String streamUrl ) {
OMDbAPIResponseDataType omdbResponse = new OMDbAPIResponseDataType ( ) ;
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM cache WHERE streamUrl = ? " ) ; ) {
ps . setString ( 1 , streamUrl ) ;
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
while ( rs . next ( ) ) {
omdbResponse . setTitle ( rs . getString ( " Title " ) ) ;
omdbResponse . setYear ( rs . getString ( " Year " ) ) ;
omdbResponse . setRated ( rs . getString ( " Rated " ) ) ;
omdbResponse . setReleased ( rs . getString ( " Released " ) ) ;
omdbResponse . setSeason ( rs . getString ( " Season " ) ) ;
omdbResponse . setEpisode ( rs . getString ( " Episode " ) ) ;
omdbResponse . setRuntime ( rs . getString ( " Runtime " ) ) ;
omdbResponse . setGenre ( rs . getString ( " Genre " ) ) ;
omdbResponse . setDirector ( rs . getString ( " Director " ) ) ;
omdbResponse . setWriter ( rs . getString ( " Writer " ) ) ;
omdbResponse . setActors ( rs . getString ( " Actors " ) ) ;
omdbResponse . setPlot ( rs . getString ( " Plot " ) ) ;
omdbResponse . setLanguage ( rs . getString ( " Language " ) ) ;
omdbResponse . setCountry ( rs . getString ( " Country " ) ) ;
omdbResponse . setAwards ( rs . getString ( " Awards " ) ) ;
omdbResponse . setMetascore ( rs . getString ( " metascore " ) ) ;
omdbResponse . setImdbRating ( rs . getString ( " imdbRating " ) ) ;
omdbResponse . setImdbVotes ( rs . getString ( " imdbVotes " ) ) ;
omdbResponse . setImdbID ( rs . getString ( " imdbID " ) ) ;
omdbResponse . setType ( rs . getString ( " Type " ) ) ;
omdbResponse . setDvd ( rs . getString ( " dvd " ) ) ;
omdbResponse . setBoxOffice ( rs . getString ( " BoxOffice " ) ) ;
//omdbResponse.setProduction(object.getString("Production", ""));
omdbResponse . setWebsite ( rs . getString ( " Website " ) ) ;
omdbResponse . setResponse ( rs . getString ( " Response " ) ) ;
omdbResponse . setPoster ( rs . getString ( " Poster " ) ) ;
}
}
} catch ( SQLException e ) {
LOGGER . error ( " Ups! error while getting the current time for {}. " , streamUrl , e ) ;
}
return omdbResponse ;
}
2018-08-13 23:56:16 +02:00
/ * *
* get all NOT cached entries
* @return a { @link ArrayList } of all NOT cached entries
* /
public ArrayList < FilmTabelDataType > getAllNotCachedEntries ( ) {
ArrayList < FilmTabelDataType > notCachedEntries = new ArrayList < > ( ) ;
2020-04-22 18:29:39 +02:00
try ( Statement stmt = connection . createStatement ( ) ; ) {
try ( ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE cached = 0 " ) ; ) {
while ( rs . next ( ) ) {
notCachedEntries . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) ) ) ;
}
2018-08-13 23:56:16 +02:00
}
2020-04-22 18:29:39 +02:00
2018-08-13 23:56:16 +02:00
} catch ( SQLException e ) {
LOGGER . error ( " An error occured, while getting all NOT cached entries " , e ) ;
}
2018-12-20 13:47:32 +01:00
LOGGER . info ( " There are {} entries not Chached! " , notCachedEntries . size ( ) ) ;
2018-08-13 23:56:16 +02:00
return notCachedEntries ;
}
2018-04-02 18:29:59 +02:00
/ * *
* return the currentTime in ms saved in the database
* @param streamUrl URL of the film
* @return { @link Double } currentTime in ms
* /
2018-04-02 02:18:43 +02:00
public double getCurrentTime ( String streamUrl ) {
2019-01-22 17:29:56 +01:00
LOGGER . info ( " get currentTime for " + streamUrl ) ;
2018-04-02 02:18:43 +02:00
double currentTime = 0 ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
currentTime = rs . getDouble ( " currentTime " ) ;
}
2018-04-02 02:18:43 +02:00
} catch ( Exception e ) {
2018-04-13 11:11:25 +02:00
LOGGER . error ( " Ups! error while getting the current time! " , e ) ;
2018-04-02 02:18:43 +02:00
}
return currentTime ;
}
2018-04-02 18:29:59 +02:00
/ * *
* save the currentTime to the database
* @param streamUrl URL of the film
* @param currentTime currentTime in ms of the film
* /
2018-04-02 02:18:43 +02:00
public void setCurrentTime ( String streamUrl , double currentTime ) {
2019-01-22 17:29:56 +01:00
LOGGER . info ( " set currentTime = " + currentTime + " for " + streamUrl ) ;
2020-04-22 18:29:39 +02:00
try ( PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET currentTime = ? WHERE streamUrl = ? " ) ; ) {
2018-12-04 22:31:11 +01:00
ps . setDouble ( 1 , currentTime ) ;
ps . setString ( 2 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-04-02 02:18:43 +02:00
connection . commit ( ) ;
} catch ( SQLException e ) {
2018-12-04 22:31:11 +01:00
LOGGER . error ( " Ups! error while updateing the current time! " , e ) ;
2018-04-02 02:18:43 +02:00
}
}
2019-06-18 01:25:46 +02:00
/ * *
* return the cache data for all episodes of a series season
* @param title the title of the series
* @param season the season you want
* @return a ArrayList of cache data ( String Array [ 21 ] )
* /
public ArrayList < String [ ] > getEpisodes ( String title , int season ) {
ArrayList < String [ ] > episodePosters = new ArrayList < > ( ) ;
2020-04-22 18:29:39 +02:00
// try to get a all episode of one season
try ( PreparedStatement ps = connection . prepareStatement ( " SELECT streamUrl, episode FROM films WHERE title = ? AND season = ? " ) ; ) {
2019-06-18 01:25:46 +02:00
ps . setString ( 1 , title ) ;
ps . setString ( 2 , Integer . toString ( season ) ) ;
2020-04-22 18:29:39 +02:00
try ( ResultSet rs = ps . executeQuery ( ) ; ) {
// for each episode load cache data
while ( rs . next ( ) ) {
String [ ] cacheData = readCacheLegacy ( rs . getString ( " streamUrl " ) ) ;
String [ ] ret = { rs . getString ( " streamUrl " ) , rs . getString ( " episode " ) , cacheData [ 20 ] } ;
episodePosters . add ( ret ) ;
}
2019-06-18 01:25:46 +02:00
}
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while getting episodes of a season! " , e ) ;
}
return episodePosters ;
}
2018-04-26 15:13:15 +02:00
/ * *
2018-12-04 22:31:11 +01:00
* get the next episode of a series
* @param title title of the film
2019-06-22 21:04:43 +02:00
* @param episode current episode
* @param season season of the current episode
2018-04-03 18:03:43 +02:00
* @return { @link FilmTabelDataType } the next episode as object
2018-04-02 18:29:59 +02:00
* /
2018-04-26 15:13:15 +02:00
public FilmTabelDataType getNextEpisode ( String title , int episode , int season ) {
2018-04-03 18:03:43 +02:00
FilmTabelDataType nextFilm = null ;
2018-04-26 15:13:15 +02:00
2018-04-02 02:18:43 +02:00
try {
2018-12-04 22:31:11 +01:00
// try to get a new episode of the current season
2019-06-18 01:25:46 +02:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE title = ? AND season = ? AND episode = ? " ) ;
2018-12-04 22:31:11 +01:00
ps . setString ( 1 , title ) ;
ps . setString ( 2 , Integer . toString ( season ) ) ;
ps . setString ( 3 , Integer . toString ( episode + 1 ) ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-26 15:13:15 +02:00
2018-12-04 22:31:11 +01:00
/ * if that fails get the next season and try to get a episode there ,
* we need to test only for the next season , first episode ,
* any other entry would not exist in the database
* /
if ( ! rs . next ( ) ) {
ps . setString ( 1 , title ) ;
ps . setString ( 2 , Integer . toString ( season + 1 ) ) ;
ps . setString ( 3 , Integer . toString ( 1 ) ) ;
rs = ps . executeQuery ( ) ;
if ( ! rs . next ( ) ) return nextFilm ; // if we haven't found anything return an empty object
2018-04-26 15:13:15 +02:00
}
2018-12-04 22:31:11 +01:00
// at this point we have found the correct episode
nextFilm = new FilmTabelDataType ( rs . getString ( " streamUrl " ) , rs . getString ( " title " ) ,
2019-06-22 21:04:43 +02:00
rs . getString ( " season " ) , rs . getString ( " episode " ) ) ;
2018-12-04 22:31:11 +01:00
2018-04-02 02:18:43 +02:00
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-04-02 02:18:43 +02:00
} catch ( Exception e ) {
2018-04-03 18:03:43 +02:00
LOGGER . error ( " Ups! error while getting next episode! " , e ) ;
2018-04-15 21:30:26 +02:00
}
2018-04-03 18:03:43 +02:00
return nextFilm ;
2018-04-02 02:18:43 +02:00
}
2019-01-22 17:29:56 +01:00
/ * * TODO rework , we should save the next episode in the root entry
2018-04-14 16:14:10 +02:00
* get the last watched episode
2019-06-22 21:04:43 +02:00
* @param streamURL URL of the stream
* @return the last watched episodes URL
2018-04-14 16:14:10 +02:00
* /
2019-06-22 21:04:43 +02:00
public String getLastWatchedEpisode ( String streamURL ) {
LOGGER . info ( " last watched episode in: " + streamURL ) ;
String lastEp = null ;
2018-04-14 16:14:10 +02:00
double lastCurrentTime = 0 ;
2018-04-13 11:11:25 +02:00
try {
2019-06-22 21:04:43 +02:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE streamUrl LIKE ? " ) ;
ps . setString ( 1 , streamURL + " /% " ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-13 11:11:25 +02:00
while ( rs . next ( ) ) {
2019-01-22 17:29:56 +01:00
// get the first episode where currentTime != 0
2019-06-22 21:04:43 +02:00
if ( rs . getDouble ( " currentTime " ) > lastCurrentTime | | lastEp = = null ) {
lastEp = rs . getString ( " streamUrl " ) ;
2018-04-13 11:11:25 +02:00
}
}
2018-04-14 16:14:10 +02:00
rs . close ( ) ;
2019-06-22 21:04:43 +02:00
ps . close ( ) ;
2018-04-13 11:11:25 +02:00
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while getting the last watched episode! " , e ) ;
}
2019-01-22 17:29:56 +01:00
2019-06-22 21:04:43 +02:00
return lastEp ;
2018-04-13 11:11:25 +02:00
}
2018-04-05 12:09:39 +02:00
/ * *
* check if a file is a video
* @param path the path to the file
* @return true if the file is a video , else false
* /
public static boolean isVideoFile ( String path ) {
String mimeType = URLConnection . guessContentTypeFromName ( path ) ;
return mimeType ! = null & & mimeType . startsWith ( " video " ) ;
}
2018-03-01 15:42:47 +01:00
}