public static void SaveMovieToDatabase(ISession session, MovieDB_Movie_Result searchResult, bool saveImages) { MovieDB_MovieRepository repMovies = new MovieDB_MovieRepository(); MovieDB_FanartRepository repFanart = new MovieDB_FanartRepository(); MovieDB_PosterRepository repPosters = new MovieDB_PosterRepository(); // save to the DB MovieDB_Movie movie = repMovies.GetByOnlineID(searchResult.MovieID); if (movie == null) { movie = new MovieDB_Movie(); } movie.Populate(searchResult); repMovies.Save(session, movie); if (!saveImages) { return; } int numFanartDownloaded = 0; int numPostersDownloaded = 0; // save data to the DB and determine the number of images we already have foreach (MovieDB_Image_Result img in searchResult.Images) { if (img.ImageType.Equals("poster", StringComparison.InvariantCultureIgnoreCase)) { MovieDB_Poster poster = repPosters.GetByOnlineID(session, img.URL); if (poster == null) { poster = new MovieDB_Poster(); } poster.Populate(img, movie.MovieId); repPosters.Save(session, poster); if (!string.IsNullOrEmpty(poster.FullImagePath) && File.Exists(poster.FullImagePath)) { numPostersDownloaded++; } } else { // fanart (backdrop) MovieDB_Fanart fanart = repFanart.GetByOnlineID(session, img.URL); if (fanart == null) { fanart = new MovieDB_Fanart(); } fanart.Populate(img, movie.MovieId); repFanart.Save(session, fanart); if (!string.IsNullOrEmpty(fanart.FullImagePath) && File.Exists(fanart.FullImagePath)) { numFanartDownloaded++; } } } // download the posters if (ServerSettings.MovieDB_AutoPosters) { foreach (MovieDB_Poster poster in repPosters.GetByMovieID(session, movie.MovieId)) { if (numPostersDownloaded < ServerSettings.MovieDB_AutoPostersAmount) { // download the image if (!string.IsNullOrEmpty(poster.FullImagePath) && !File.Exists(poster.FullImagePath)) { CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(poster.MovieDB_PosterID, JMMImageType.MovieDB_Poster, false); cmd.Save(session); numPostersDownloaded++; } } else { //The MovieDB_AutoPostersAmount should prevent from saving image info without image // we should clean those image that we didn't download because those dont exists in local repo // first we check if file was downloaded if (!File.Exists(poster.FullImagePath)) { repPosters.Delete(poster.MovieDB_PosterID); } } } } // download the fanart if (ServerSettings.MovieDB_AutoFanart) { foreach (MovieDB_Fanart fanart in repFanart.GetByMovieID(session, movie.MovieId)) { if (numFanartDownloaded < ServerSettings.MovieDB_AutoFanartAmount) { // download the image if (!string.IsNullOrEmpty(fanart.FullImagePath) && !File.Exists(fanart.FullImagePath)) { CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(fanart.MovieDB_FanartID, JMMImageType.MovieDB_FanArt, false); cmd.Save(session); numFanartDownloaded++; } } else { //The MovieDB_AutoFanartAmount should prevent from saving image info without image // we should clean those image that we didn't download because those dont exists in local repo // first we check if file was downloaded if (!File.Exists(fanart.FullImagePath)) { repFanart.Delete(fanart.MovieDB_FanartID); } } } } }
private static void SaveMovieToDatabase(ISession session, MovieDB_Movie_Result searchResult, bool saveImages) { MovieDB_MovieRepository repMovies = new MovieDB_MovieRepository(); MovieDB_FanartRepository repFanart = new MovieDB_FanartRepository(); MovieDB_PosterRepository repPosters = new MovieDB_PosterRepository(); // save to the DB MovieDB_Movie movie = repMovies.GetByOnlineID(searchResult.MovieID); if (movie == null) { movie = new MovieDB_Movie(); } movie.Populate(searchResult); repMovies.Save(session, movie); if (!saveImages) { return; } int numFanartDownloaded = 0; int numPostersDownloaded = 0; foreach (MovieDB_Image_Result img in searchResult.Images) { if (img.ImageType.Equals("poster", StringComparison.InvariantCultureIgnoreCase)) { MovieDB_Poster poster = repPosters.GetByOnlineID(session, img.ImageID, img.ImageSize); if (poster == null) { poster = new MovieDB_Poster(); } poster.Populate(img, movie.MovieId); repPosters.Save(session, poster); if (ServerSettings.MovieDB_AutoPosters && numPostersDownloaded < ServerSettings.MovieDB_AutoPostersAmount) { // download the image if (!string.IsNullOrEmpty(poster.FullImagePath) && !File.Exists(poster.FullImagePath)) { CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(poster.MovieDB_PosterID, JMMImageType.MovieDB_Poster, false); cmd.Save(session); numPostersDownloaded++; } } } else { // fanart (backdrop) MovieDB_Fanart fanart = repFanart.GetByOnlineID(session, img.ImageID, img.ImageSize); if (fanart == null) { fanart = new MovieDB_Fanart(); } fanart.Populate(img, movie.MovieId); repFanart.Save(session, fanart); if (ServerSettings.MovieDB_AutoFanart && numFanartDownloaded < ServerSettings.MovieDB_AutoPostersAmount) { // download the image if (!string.IsNullOrEmpty(fanart.FullImagePath) && !File.Exists(fanart.FullImagePath)) { CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(fanart.MovieDB_FanartID, JMMImageType.MovieDB_FanArt, false); cmd.Save(session); numFanartDownloaded++; } } } } }