public void Save(MovieDB_Movie obj)
		{
			using (var session = JMMService.SessionFactory.OpenSession())
			{
				Save(session, obj);
			}
		}
Exemple #2
0
		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++;
						}
					}
				}
			}
		}
		public void Save(ISession session, MovieDB_Movie obj)
		{
			// populate the database
			using (var transaction = session.BeginTransaction())
			{
				session.SaveOrUpdate(obj);
				transaction.Commit();
			}
		}
        public static void SaveMovieToDatabase(ISession session, MovieDB_Movie_Result searchResult, bool saveImages,
            bool isTrakt)
        {
            ISessionWrapper sessionWrapper = session.Wrap();

            // save to the DB
            MovieDB_Movie movie = RepoFactory.MovieDb_Movie.GetByOnlineID(searchResult.MovieID);
            if (movie == null) movie = new MovieDB_Movie();
            movie.Populate(searchResult);

            // Only save movie info if source is not trakt, this presents adding tv shows as movies
            // Needs better fix later on

            if (!isTrakt)
            {
                RepoFactory.MovieDb_Movie.Save(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 = RepoFactory.MovieDB_Poster.GetByOnlineID(session, img.URL);
                    if (poster == null) poster = new MovieDB_Poster();
                    poster.Populate(img, movie.MovieId);
                    RepoFactory.MovieDB_Poster.Save(poster);

                    if (!string.IsNullOrEmpty(poster.FullImagePath) && File.Exists(poster.FullImagePath))
                        numPostersDownloaded++;
                }
                else
                {
                    // fanart (backdrop)
                    MovieDB_Fanart fanart = RepoFactory.MovieDB_Fanart.GetByOnlineID(session, img.URL);
                    if (fanart == null) fanart = new MovieDB_Fanart();
                    fanart.Populate(img, movie.MovieId);
                    RepoFactory.MovieDB_Fanart.Save(fanart);

                    if (!string.IsNullOrEmpty(fanart.FullImagePath) && File.Exists(fanart.FullImagePath))
                        numFanartDownloaded++;
                }
            }

            // download the posters
            if (ServerSettings.MovieDB_AutoPosters || isTrakt)
            {
                foreach (MovieDB_Poster poster in RepoFactory.MovieDB_Poster.GetByMovieID(sessionWrapper, 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))
                        {
                            RepoFactory.MovieDB_Poster.Delete(poster.MovieDB_PosterID);
                        }
                    }
                }
            }

            // download the fanart
            if (ServerSettings.MovieDB_AutoFanart || isTrakt)
            {
                foreach (MovieDB_Fanart fanart in RepoFactory.MovieDB_Fanart.GetByMovieID(sessionWrapper, 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))
                        {
                            RepoFactory.MovieDB_Fanart.Delete(fanart.MovieDB_FanartID);
                        }
                    }
                }
            }
        }
Exemple #5
0
        public Contract_AnimeSeries ToContract(AniDB_Anime animeRec, List <CrossRef_AniDB_TvDBV2> tvDBCrossRefs, CrossRef_AniDB_Other movieDBCrossRef,
                                               AnimeSeries_User userRecord, List <TvDB_Series> tvseries, List <CrossRef_AniDB_MAL> malDBCrossRef, bool passedDefaultImages, AniDB_Anime_DefaultImage defPoster,
                                               AniDB_Anime_DefaultImage defFanart, AniDB_Anime_DefaultImage defWideBanner, List <AniDB_Anime_Title> titles, MovieDB_Movie movie, bool forceimages = false)
        {
            Contract_AnimeSeries contract = new Contract_AnimeSeries();

            contract.AniDB_ID                  = this.AniDB_ID;
            contract.AnimeGroupID              = this.AnimeGroupID;
            contract.AnimeSeriesID             = this.AnimeSeriesID;
            contract.DateTimeUpdated           = this.DateTimeUpdated;
            contract.DateTimeCreated           = this.DateTimeCreated;
            contract.DefaultAudioLanguage      = this.DefaultAudioLanguage;
            contract.DefaultSubtitleLanguage   = this.DefaultSubtitleLanguage;
            contract.LatestLocalEpisodeNumber  = this.LatestLocalEpisodeNumber;
            contract.EpisodeAddedDate          = this.EpisodeAddedDate;
            contract.MissingEpisodeCount       = this.MissingEpisodeCount;
            contract.MissingEpisodeCountGroups = this.MissingEpisodeCountGroups;
            contract.SeriesNameOverride        = this.SeriesNameOverride;
            contract.DefaultFolder             = this.DefaultFolder;



            if (userRecord == null)
            {
                contract.PlayedCount           = 0;
                contract.StoppedCount          = 0;
                contract.UnwatchedEpisodeCount = 0;
                contract.WatchedCount          = 0;
                contract.WatchedDate           = null;
                contract.WatchedEpisodeCount   = 0;
            }
            else
            {
                contract.PlayedCount           = userRecord.PlayedCount;
                contract.StoppedCount          = userRecord.StoppedCount;
                contract.UnwatchedEpisodeCount = userRecord.UnwatchedEpisodeCount;
                contract.WatchedCount          = userRecord.WatchedCount;
                contract.WatchedDate           = userRecord.WatchedDate;
                contract.WatchedEpisodeCount   = userRecord.WatchedEpisodeCount;
            }

            // get AniDB data
            contract.AniDBAnime = null;
            if (animeRec != null)
            {
                Contract_AniDBAnime animecontract = animeRec.ToContract(false, titles);

                AniDB_Anime_DefaultImage defaultPoster = null;
                if (passedDefaultImages)
                {
                    defaultPoster = defPoster;
                }
                else
                {
                    defaultPoster = animeRec.GetDefaultPoster();
                }

                if (defaultPoster == null)
                {
                    animecontract.DefaultImagePoster = null;
                }
                else
                {
                    animecontract.DefaultImagePoster = defaultPoster.ToContract();
                }

                if ((animecontract.DefaultImagePoster == null) && (forceimages))
                {
                    ImageDetails im = animeRec.GetDefaultPosterDetailsNoBlanks();
                    if (im != null)
                    {
                        animecontract.DefaultImagePoster           = new Contract_AniDB_Anime_DefaultImage();
                        animecontract.DefaultImagePoster.AnimeID   = im.ImageID;
                        animecontract.DefaultImagePoster.ImageType = (int)im.ImageType;
                    }
                }
                AniDB_Anime_DefaultImage defaultFanart = null;
                if (passedDefaultImages)
                {
                    defaultFanart = defFanart;
                }
                else
                {
                    defaultFanart = animeRec.GetDefaultFanart();
                }

                if (defaultFanart == null)
                {
                    animecontract.DefaultImageFanart = null;
                }
                else
                {
                    animecontract.DefaultImageFanart = defaultFanart.ToContract();
                }

                if ((animecontract.DefaultImageFanart == null) && (forceimages))
                {
                    ImageDetails im = animeRec.GetDefaultFanartDetailsNoBlanks();
                    if (im != null)
                    {
                        animecontract.DefaultImageFanart           = new Contract_AniDB_Anime_DefaultImage();
                        animecontract.DefaultImageFanart.AnimeID   = im.ImageID;
                        animecontract.DefaultImageFanart.ImageType = (int)im.ImageType;
                    }
                }

                AniDB_Anime_DefaultImage defaultWideBanner = null;
                if (passedDefaultImages)
                {
                    defaultWideBanner = defWideBanner;
                }
                else
                {
                    defaultWideBanner = animeRec.GetDefaultWideBanner();
                }

                if (defaultWideBanner == null)
                {
                    animecontract.DefaultImageWideBanner = null;
                }
                else
                {
                    animecontract.DefaultImageWideBanner = defaultWideBanner.ToContract();
                }

                contract.AniDBAnime = animecontract;
            }

            contract.CrossRefAniDBTvDBV2 = new List <Contract_CrossRef_AniDB_TvDBV2>();
            foreach (CrossRef_AniDB_TvDBV2 tvXref in tvDBCrossRefs)
            {
                contract.CrossRefAniDBTvDBV2.Add(tvXref.ToContract());
            }


            contract.TvDB_Series = new List <Contract_TvDB_Series>();
            foreach (TvDB_Series ser in tvseries)
            {
                contract.TvDB_Series.Add(ser.ToContract());
            }

            contract.CrossRefAniDBMovieDB = null;
            if (movieDBCrossRef != null)
            {
                contract.CrossRefAniDBMovieDB = movieDBCrossRef.ToContract();
                contract.MovieDB_Movie        = movie.ToContract();
            }
            contract.CrossRefAniDBMAL = new List <Contract_CrossRef_AniDB_MAL>();
            if (malDBCrossRef != null)
            {
                foreach (CrossRef_AniDB_MAL xref in malDBCrossRef)
                {
                    contract.CrossRefAniDBMAL.Add(xref.ToContract());
                }
            }

            return(contract);
        }