Exemple #1
0
        public static void DownloadAutomaticImages(List <TvDB_ImageWideBanner> images, int seriesID, bool forceDownload)
        {
            // find out how many images we already have locally
            int imageCount = RepoFactory.TvDB_ImageWideBanner.GetBySeriesID(seriesID).Count(banner =>
                                                                                            !string.IsNullOrEmpty(banner.GetFullImagePath()) && File.Exists(banner.GetFullImagePath()));

            foreach (TvDB_ImageWideBanner img in images)
            {
                if (ServerSettings.Instance.TvDB.AutoWideBanners && imageCount < ServerSettings.Instance.TvDB.AutoWideBannersAmount &&
                    !string.IsNullOrEmpty(img.GetFullImagePath()))
                {
                    bool fileExists = File.Exists(img.GetFullImagePath());
                    if (fileExists && !forceDownload)
                    {
                        continue;
                    }
                    CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageWideBannerID,
                                                                                        ImageEntityType.TvDB_Banner, forceDownload);
                    cmd.Save();
                    imageCount++;
                }
                else
                {
                    //The TvDB_AutoFanartAmount point to download less images than its available
                    // 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 (string.IsNullOrEmpty(img.GetFullImagePath()) || !File.Exists(img.GetFullImagePath()))
                    {
                        RepoFactory.TvDB_ImageWideBanner.Delete(img);
                    }
                }
            }
        }
Exemple #2
0
        public void DownloadAutomaticImages(XmlDocument doc, int seriesID, bool forceDownload)
        {
            List <object> banners = ParseBanners(seriesID, doc);

            int numFanartDownloaded  = 0;
            int numPostersDownloaded = 0;
            int numBannersDownloaded = 0;

            foreach (object obj in banners)
            {
                if (obj.GetType() == typeof(TvDB_ImageFanart))
                {
                    TvDB_ImageFanart img = obj as TvDB_ImageFanart;
                    if (ServerSettings.TvDB_AutoFanart && numFanartDownloaded < ServerSettings.TvDB_AutoFanartAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageFanartID, JMMImageType.TvDB_FanArt, forceDownload);
                            cmd.Save();
                            numFanartDownloaded++;
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImagePoster))
                {
                    TvDB_ImagePoster img = obj as TvDB_ImagePoster;
                    if (ServerSettings.TvDB_AutoPosters && numPostersDownloaded < ServerSettings.TvDB_AutoPostersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImagePosterID, JMMImageType.TvDB_Cover, forceDownload);
                            cmd.Save();
                            numPostersDownloaded++;
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImageWideBanner))
                {
                    TvDB_ImageWideBanner img = obj as TvDB_ImageWideBanner;
                    if (ServerSettings.TvDB_AutoWideBanners && numBannersDownloaded < ServerSettings.TvDB_AutoWideBannersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageWideBannerID, JMMImageType.TvDB_Banner, forceDownload);
                            cmd.Save();
                            numBannersDownloaded++;
                        }
                    }
                }
            }
        }
Exemple #3
0
        public static void DownloadAutomaticImages(List <TvDB_ImagePoster> images, int seriesID, bool forceDownload)
        {
            int imageCount = 0;

            // find out how many images we already have locally
            using (var session = DatabaseFactory.SessionFactory.OpenSession())
            {
                ISessionWrapper sessionWrapper = session.Wrap();

                foreach (TvDB_ImagePoster fanart in RepoFactory.TvDB_ImagePoster
                         .GetBySeriesID(seriesID))
                {
                    if (!string.IsNullOrEmpty(fanart.GetFullImagePath()) && File.Exists(fanart.GetFullImagePath()))
                    {
                        imageCount++;
                    }
                }
            }

            foreach (TvDB_ImagePoster img in images)
            {
                if (ServerSettings.TvDB_AutoFanart && imageCount < ServerSettings.TvDB_AutoFanartAmount)
                {
                    bool fileExists = File.Exists(img.GetFullImagePath());
                    if (!fileExists || (fileExists && forceDownload))
                    {
                        CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImagePosterID,
                                                                                            JMMImageType.TvDB_Cover, forceDownload);
                        cmd.Save();
                        imageCount++;
                    }
                }
                else
                {
                    //The TvDB_AutoFanartAmount point to download less images than its available
                    // 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(img.GetFullImagePath()))
                    {
                        RepoFactory.TvDB_ImageFanart.Delete(img.TvDB_ImagePosterID);
                    }
                }
            }
        }
Exemple #4
0
        public static async Task <TvDB_Episode> QueueEpisodeImageDownloadAsync(int tvDBEpisodeID, bool downloadImages, bool forceRefresh)
        {
            try
            {
                TvDB_Episode ep = RepoFactory.TvDB_Episode.GetByTvDBID(tvDBEpisodeID);
                if (ep == null || forceRefresh)
                {
                    EpisodeRecord episode = await GetEpisodeDetailsAsync(tvDBEpisodeID);

                    if (episode == null)
                    {
                        return(null);
                    }

                    if (ep == null)
                    {
                        ep = new TvDB_Episode();
                    }
                    ep.Populate(episode);
                    RepoFactory.TvDB_Episode.Save(ep);
                }

                if (downloadImages)
                {
                    if (!string.IsNullOrEmpty(ep.Filename))
                    {
                        bool fileExists = File.Exists(ep.GetFullImagePath());
                        if (!fileExists || forceRefresh)
                        {
                            CommandRequest_DownloadImage cmd =
                                new CommandRequest_DownloadImage(ep.TvDB_EpisodeID,
                                                                 ImageEntityType.TvDB_Episode, forceRefresh);
                            cmd.Save();
                        }
                    }
                }
                return(ep);
            }
            catch (TvDbServerException exception)
            {
                if (exception.StatusCode == (int)HttpStatusCode.Unauthorized)
                {
                    client.Authentication.Token = null;
                    await CheckAuthorizationAsync();

                    if (!string.IsNullOrEmpty(client.Authentication.Token))
                    {
                        return(await QueueEpisodeImageDownloadAsync(tvDBEpisodeID, downloadImages, forceRefresh));
                    }
                    // suppress 404 and move on
                }
                else if (exception.StatusCode == (int)HttpStatusCode.NotFound)
                {
                    return(null);
                }
                logger.Error(exception,
                             $"TvDB returned an error code: {exception.StatusCode}\n        {exception.Message}");
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"Error in TVDBHelper.GetEpisodes: {ex}");
            }
            return(null);
        }
Exemple #5
0
        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 #6
0
        private AniDB_Anime SaveResultsForAnimeXML(ISession session, int animeID, bool downloadRelations, AniDBHTTPCommand_GetFullAnime getAnimeCmd)
        {
            AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
            AniDB_Anime           anime    = null;

            logger.Trace("cmdResult.Anime: {0}", getAnimeCmd.Anime);

            anime = repAnime.GetByAnimeID(session, animeID);
            if (anime == null)
            {
                anime = new AniDB_Anime();
            }
            anime.PopulateAndSaveFromHTTP(session, getAnimeCmd.Anime, getAnimeCmd.Episodes, getAnimeCmd.Titles, getAnimeCmd.Categories, getAnimeCmd.Tags,
                                          getAnimeCmd.Characters, getAnimeCmd.Relations, getAnimeCmd.SimilarAnime, getAnimeCmd.Recommendations, downloadRelations);

            // Request an image download
            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(anime.AniDB_AnimeID, JMMImageType.AniDB_Cover, false);

            cmd.Save(session);
            // create AnimeEpisode records for all episodes in this anime
            // only if we have a series
            AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
            AnimeSeries           ser       = repSeries.GetByAnimeID(session, animeID);

            if (ser != null)
            {
                ser.CreateAnimeEpisodes(session);
            }

            // update any files, that may have been linked

            /*CrossRef_File_EpisodeRepository repCrossRefs = new CrossRef_File_EpisodeRepository();
             * repCrossRefs.GetByAnimeID(*/

            // update cached stats
            StatsCache.Instance.UpdateUsingAnime(session, anime.AnimeID);
            StatsCache.Instance.UpdateAnimeContract(session, anime.AnimeID);

            // download character images
            foreach (AniDB_Anime_Character animeChar in anime.GetAnimeCharacters(session))
            {
                AniDB_Character chr = animeChar.GetCharacter(session);
                if (chr == null)
                {
                    continue;
                }

                if (ServerSettings.AniDB_DownloadCharacters)
                {
                    if (!string.IsNullOrEmpty(chr.PosterPath) && !File.Exists(chr.PosterPath))
                    {
                        logger.Debug("Downloading character image: {0} - {1}({2}) - {3}", anime.MainTitle, chr.CharName, chr.CharID, chr.PosterPath);
                        cmd = new CommandRequest_DownloadImage(chr.AniDB_CharacterID, JMMImageType.AniDB_Character, false);
                        cmd.Save();
                    }
                }

                if (ServerSettings.AniDB_DownloadCreators)
                {
                    AniDB_Seiyuu seiyuu = chr.GetSeiyuu(session);
                    if (seiyuu == null || string.IsNullOrEmpty(seiyuu.PosterPath))
                    {
                        continue;
                    }

                    if (!File.Exists(seiyuu.PosterPath))
                    {
                        logger.Debug("Downloading seiyuu image: {0} - {1}({2}) - {3}", anime.MainTitle, seiyuu.SeiyuuName, seiyuu.SeiyuuID, seiyuu.PosterPath);
                        cmd = new CommandRequest_DownloadImage(seiyuu.AniDB_SeiyuuID, JMMImageType.AniDB_Creator, false);
                        cmd.Save();
                    }
                }
            }

            return(anime);
        }
Exemple #7
0
        /// <summary>
        /// Updates the followung
        /// 1. Series Info
        /// 2. Episode Info
        /// 3. Episode Images
        /// 4. Fanart, Poster and Wide Banner Images
        /// </summary>
        /// <param name="seriesID"></param>
        /// <param name="forceRefresh"></param>
        public void UpdateAllInfoAndImages(int seriesID, bool forceRefresh, bool downloadImages)
        {
            string fileName = string.Format("{0}.xml", ServerSettings.TvDB_Language);

            Dictionary <string, XmlDocument> docSeries = GetFullSeriesInfo(seriesID);

            if (docSeries.ContainsKey(fileName))
            {
                try
                {
                    // update the series info
                    XmlDocument xmlDoc = docSeries[fileName];
                    if (xmlDoc != null)
                    {
                        TvDB_Series tvSeries = RepoFactory.TvDB_Series.GetByTvDBID(seriesID);
                        if (tvSeries == null)
                        {
                            tvSeries = new TvDB_Series();
                        }

                        tvSeries.PopulateFromSeriesInfo(xmlDoc);
                        RepoFactory.TvDB_Series.Save(tvSeries);
                    }

                    if (downloadImages)
                    {
                        // get all fanart, posters and wide banners
                        if (docSeries.ContainsKey("banners.xml"))
                        {
                            XmlDocument xmlDocBanners = docSeries["banners.xml"];
                            if (xmlDocBanners != null)
                            {
                                DownloadAutomaticImages(xmlDocBanners, seriesID, forceRefresh);
                            }
                        }
                    }

                    // update all the episodes and download episode images
                    XmlNodeList episodeItems = xmlDoc["Data"].GetElementsByTagName("Episode");
                    logger.Trace("Found {0} Episode nodes", episodeItems.Count.ToString());

                    List <int> existingEpIds = new List <int>();
                    foreach (XmlNode node in episodeItems)
                    {
                        try
                        {
                            // the episode id
                            int id = int.Parse(node["id"].InnerText.Trim());
                            existingEpIds.Add(id);

                            TvDB_Episode ep = RepoFactory.TvDB_Episode.GetByTvDBID(id);
                            if (ep == null)
                            {
                                ep = new TvDB_Episode();
                            }
                            ep.Populate(node);
                            RepoFactory.TvDB_Episode.Save(ep);

                            //BaseConfig.MyAnimeLog.Write("Refreshing episode info for: {0}", ep.ToString());

                            if (downloadImages)
                            {
                                // download the image for this episode
                                if (!string.IsNullOrEmpty(ep.Filename))
                                {
                                    bool fileExists = File.Exists(ep.FullImagePath);
                                    if (!fileExists || (fileExists && forceRefresh))
                                    {
                                        CommandRequest_DownloadImage cmd =
                                            new CommandRequest_DownloadImage(ep.TvDB_EpisodeID,
                                                                             JMMImageType.TvDB_Episode, forceRefresh);
                                        cmd.Save();
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex, "Error in TVDBHelper.GetEpisodes: " + ex.ToString());
                        }
                    }

                    // get all the existing tvdb episodes, to see if any have been deleted
                    List <TvDB_Episode> allEps = RepoFactory.TvDB_Episode.GetBySeriesID(seriesID);
                    foreach (TvDB_Episode oldEp in allEps)
                    {
                        if (!existingEpIds.Contains(oldEp.Id))
                        {
                            RepoFactory.TvDB_Episode.Delete(oldEp.TvDB_EpisodeID);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "Error in TVDBHelper.GetEpisodes: " + ex.ToString());
                }
            }
        }
Exemple #8
0
        public void DownloadAutomaticImages(XmlDocument doc, int seriesID, bool forceDownload)
        {
            List <object> banners = ParseBanners(seriesID, doc);

            int numFanartDownloaded  = 0;
            int numPostersDownloaded = 0;
            int numBannersDownloaded = 0;

            // find out how many images we already have locally



            using (var session = DatabaseFactory.SessionFactory.OpenSession())
            {
                ISessionWrapper sessionWrapper = session.Wrap();

                foreach (TvDB_ImageFanart fanart in RepoFactory.TvDB_ImageFanart.GetBySeriesID(sessionWrapper, seriesID))
                {
                    if (!string.IsNullOrEmpty(fanart.FullImagePath) && File.Exists(fanart.FullImagePath))
                    {
                        numFanartDownloaded++;
                    }
                }

                foreach (TvDB_ImagePoster poster in RepoFactory.TvDB_ImagePoster.GetBySeriesID(sessionWrapper, seriesID))
                {
                    if (!string.IsNullOrEmpty(poster.FullImagePath) && File.Exists(poster.FullImagePath))
                    {
                        numPostersDownloaded++;
                    }
                }

                foreach (TvDB_ImageWideBanner banner in RepoFactory.TvDB_ImageWideBanner.GetBySeriesID(session, seriesID))
                {
                    if (!string.IsNullOrEmpty(banner.FullImagePath) && File.Exists(banner.FullImagePath))
                    {
                        numBannersDownloaded++;
                    }
                }
            }


            foreach (object obj in banners)
            {
                if (obj.GetType() == typeof(TvDB_ImageFanart))
                {
                    TvDB_ImageFanart img = obj as TvDB_ImageFanart;
                    if (ServerSettings.TvDB_AutoFanart && numFanartDownloaded < ServerSettings.TvDB_AutoFanartAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageFanartID,
                                                                                                JMMImageType.TvDB_FanArt, forceDownload);
                            cmd.Save();
                            numFanartDownloaded++;
                        }
                    }
                    else
                    {
                        //The TvDB_AutoFanartAmount point to download less images than its available
                        // 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(img.FullImagePath))
                        {
                            RepoFactory.TvDB_ImageFanart.Delete(img.TvDB_ImageFanartID);
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImagePoster))
                {
                    TvDB_ImagePoster img = obj as TvDB_ImagePoster;
                    if (ServerSettings.TvDB_AutoPosters && numPostersDownloaded < ServerSettings.TvDB_AutoPostersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImagePosterID,
                                                                                                JMMImageType.TvDB_Cover, forceDownload);
                            cmd.Save();
                            numPostersDownloaded++;
                        }
                    }
                    else
                    {
                        //The TvDB_AutoPostersAmount point to download less images than its available
                        // 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(img.FullImagePath))
                        {
                            RepoFactory.TvDB_ImagePoster.Delete(img.TvDB_ImagePosterID);
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImageWideBanner))
                {
                    TvDB_ImageWideBanner img = obj as TvDB_ImageWideBanner;
                    if (ServerSettings.TvDB_AutoWideBanners &&
                        numBannersDownloaded < ServerSettings.TvDB_AutoWideBannersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd =
                                new CommandRequest_DownloadImage(img.TvDB_ImageWideBannerID,
                                                                 JMMImageType.TvDB_Banner, forceDownload);
                            cmd.Save();
                            numBannersDownloaded++;
                        }
                    }
                    else
                    {
                        //The TvDB_AutoWideBannersAmount point to download less images than its available
                        // 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(img.FullImagePath))
                        {
                            RepoFactory.TvDB_ImageWideBanner.Delete(img.TvDB_ImageWideBannerID);
                        }
                    }
                }
            }
        }
Exemple #9
0
        public static async Task QueueEpisodeImageDownloadAsync(BasicEpisode item, List <int> existingEpIds, bool downloadImages, bool forceRefresh)
        {
            try
            {
                // the episode id
                int id = item.Id;
                existingEpIds.Add(id);

                TvDB_Episode ep = RepoFactory.TvDB_Episode.GetByTvDBID(id);
                if (ep == null)
                {
                    ep = new TvDB_Episode();
                }

                EpisodeRecord episode = await GetEpisodeDetailsAsync(id);

                if (episode == null)
                {
                    return;
                }
                ep.Populate(episode);
                RepoFactory.TvDB_Episode.Save(ep);

                if (downloadImages)
                {
                    // download the image for this episode
                    if (!string.IsNullOrEmpty(ep.Filename))
                    {
                        bool fileExists = File.Exists(ep.GetFullImagePath());
                        if (!fileExists || forceRefresh)
                        {
                            CommandRequest_DownloadImage cmd =
                                new CommandRequest_DownloadImage(ep.TvDB_EpisodeID,
                                                                 JMMImageType.TvDB_Episode, forceRefresh);
                            cmd.Save();
                        }
                    }
                }
            }
            catch (TvDbSharper.Errors.TvDbServerException exception)
            {
                if (exception.StatusCode == HttpStatusCode.Unauthorized)
                {
                    client.Authentication.Token = null;
                    await _checkAuthorizationAsync();

                    if (client.Authentication.Token != null)
                    {
                        await QueueEpisodeImageDownloadAsync(item, existingEpIds, downloadImages, forceRefresh);

                        return;
                    }
                    // suppress 404 and move on
                }
                else if (exception.StatusCode == HttpStatusCode.NotFound)
                {
                    return;
                }
                logger.Error(exception, "TvDB returned an error code: " + exception.StatusCode + "\n        " + exception.Message);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error in TVDBHelper.GetEpisodes: " + ex.ToString());
            }
        }
Exemple #10
0
        public void DownloadAutomaticImages(XmlDocument doc, int seriesID, bool forceDownload)
        {
            List <object> banners = ParseBanners(seriesID, doc);

            int numFanartDownloaded  = 0;
            int numPostersDownloaded = 0;
            int numBannersDownloaded = 0;

            // find out how many images we already have locally
            TvDB_ImageFanartRepository     repFanart  = new TvDB_ImageFanartRepository();
            TvDB_ImagePosterRepository     repPosters = new TvDB_ImagePosterRepository();
            TvDB_ImageWideBannerRepository repBanners = new TvDB_ImageWideBannerRepository();

            using (var session = JMMService.SessionFactory.OpenSession())
            {
                foreach (TvDB_ImageFanart fanart in repFanart.GetBySeriesID(session, seriesID))
                {
                    if (!string.IsNullOrEmpty(fanart.FullImagePath) && File.Exists(fanart.FullImagePath))
                    {
                        numFanartDownloaded++;
                    }
                }

                foreach (TvDB_ImagePoster poster in repPosters.GetBySeriesID(session, seriesID))
                {
                    if (!string.IsNullOrEmpty(poster.FullImagePath) && File.Exists(poster.FullImagePath))
                    {
                        numPostersDownloaded++;
                    }
                }

                foreach (TvDB_ImageWideBanner banner in repBanners.GetBySeriesID(session, seriesID))
                {
                    if (!string.IsNullOrEmpty(banner.FullImagePath) && File.Exists(banner.FullImagePath))
                    {
                        numBannersDownloaded++;
                    }
                }
            }



            foreach (object obj in banners)
            {
                if (obj.GetType() == typeof(TvDB_ImageFanart))
                {
                    TvDB_ImageFanart img = obj as TvDB_ImageFanart;
                    if (ServerSettings.TvDB_AutoFanart && numFanartDownloaded < ServerSettings.TvDB_AutoFanartAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageFanartID, JMMImageType.TvDB_FanArt, forceDownload);
                            cmd.Save();
                            numFanartDownloaded++;
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImagePoster))
                {
                    TvDB_ImagePoster img = obj as TvDB_ImagePoster;
                    if (ServerSettings.TvDB_AutoPosters && numPostersDownloaded < ServerSettings.TvDB_AutoPostersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImagePosterID, JMMImageType.TvDB_Cover, forceDownload);
                            cmd.Save();
                            numPostersDownloaded++;
                        }
                    }
                }

                if (obj.GetType() == typeof(TvDB_ImageWideBanner))
                {
                    TvDB_ImageWideBanner img = obj as TvDB_ImageWideBanner;
                    if (ServerSettings.TvDB_AutoWideBanners && numBannersDownloaded < ServerSettings.TvDB_AutoWideBannersAmount)
                    {
                        bool fileExists = File.Exists(img.FullImagePath);
                        if (!fileExists || (fileExists && forceDownload))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(img.TvDB_ImageWideBannerID, JMMImageType.TvDB_Banner, forceDownload);
                            cmd.Save();
                            numBannersDownloaded++;
                        }
                    }
                }
            }
        }
Exemple #11
0
		public static void RunImport_GetImages()
		{
			// AniDB posters
			AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
			foreach (AniDB_Anime anime in repAnime.GetAll())
			{
				if (anime.AnimeID == 8580)
					Console.Write("");

				if (string.IsNullOrEmpty(anime.PosterPath)) continue;

				bool fileExists = File.Exists(anime.PosterPath);
				if (!fileExists)
				{
					CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(anime.AniDB_AnimeID, JMMImageType.AniDB_Cover, false);
					cmd.Save();
				}
			}

			// TvDB Posters
			if (ServerSettings.TvDB_AutoPosters)
			{
				TvDB_ImagePosterRepository repTvPosters = new TvDB_ImagePosterRepository();
				Dictionary<int, int> postersCount = new Dictionary<int, int>();

				// build a dictionary of series and how many images exist
				List<TvDB_ImagePoster> allPosters = repTvPosters.GetAll();
				foreach (TvDB_ImagePoster tvPoster in allPosters)
				{
					if (string.IsNullOrEmpty(tvPoster.FullImagePath)) continue;
					bool fileExists = File.Exists(tvPoster.FullImagePath);

					if (fileExists)
					{
						if (postersCount.ContainsKey(tvPoster.SeriesID))
							postersCount[tvPoster.SeriesID] = postersCount[tvPoster.SeriesID] + 1;
						else
							postersCount[tvPoster.SeriesID] = 1;
					}
				}

				foreach (TvDB_ImagePoster tvPoster in allPosters)
				{
					if (string.IsNullOrEmpty(tvPoster.FullImagePath)) continue;
					bool fileExists = File.Exists(tvPoster.FullImagePath);

					int postersAvailable = 0;
					if (postersCount.ContainsKey(tvPoster.SeriesID))
						postersAvailable = postersCount[tvPoster.SeriesID];

					if (!fileExists && postersAvailable < ServerSettings.TvDB_AutoPostersAmount)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(tvPoster.TvDB_ImagePosterID, JMMImageType.TvDB_Cover, false);
						cmd.Save();

                        if (postersCount.ContainsKey(tvPoster.SeriesID))
                            postersCount[tvPoster.SeriesID] = postersCount[tvPoster.SeriesID] + 1;
                        else
                            postersCount[tvPoster.SeriesID] = 1;
					}
				}
			}

			// TvDB Fanart
			if (ServerSettings.TvDB_AutoFanart)
			{
				Dictionary<int, int> fanartCount = new Dictionary<int, int>();
				TvDB_ImageFanartRepository repTvFanart = new TvDB_ImageFanartRepository();

				List<TvDB_ImageFanart> allFanart = repTvFanart.GetAll();
				foreach (TvDB_ImageFanart tvFanart in allFanart)
				{
					// build a dictionary of series and how many images exist
					if (string.IsNullOrEmpty(tvFanart.FullImagePath)) continue;
					bool fileExists = File.Exists(tvFanart.FullImagePath);

					if (fileExists)
					{
						if (fanartCount.ContainsKey(tvFanart.SeriesID))
							fanartCount[tvFanart.SeriesID] = fanartCount[tvFanart.SeriesID] + 1;
						else
							fanartCount[tvFanart.SeriesID] = 1;
					}
				}

				foreach (TvDB_ImageFanart tvFanart in allFanart)
				{
					if (string.IsNullOrEmpty(tvFanart.FullImagePath)) continue;
					bool fileExists = File.Exists(tvFanart.FullImagePath);

					int fanartAvailable = 0;
					if (fanartCount.ContainsKey(tvFanart.SeriesID))
						fanartAvailable = fanartCount[tvFanart.SeriesID];

					if (!fileExists && fanartAvailable < ServerSettings.TvDB_AutoFanartAmount)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(tvFanart.TvDB_ImageFanartID, JMMImageType.TvDB_FanArt, false);
						cmd.Save();

                        if (fanartCount.ContainsKey(tvFanart.SeriesID))
                            fanartCount[tvFanart.SeriesID] = fanartCount[tvFanart.SeriesID] + 1;
                        else
                            fanartCount[tvFanart.SeriesID] = 1;
					}
				}
			}

			// TvDB Wide Banners
			if (ServerSettings.TvDB_AutoWideBanners)
			{
				TvDB_ImageWideBannerRepository repTvBanners = new TvDB_ImageWideBannerRepository();
				Dictionary<int, int> fanartCount = new Dictionary<int, int>();

				// build a dictionary of series and how many images exist
				List<TvDB_ImageWideBanner> allBanners = repTvBanners.GetAll();
				foreach (TvDB_ImageWideBanner tvBanner in allBanners)
				{
					if (string.IsNullOrEmpty(tvBanner.FullImagePath)) continue;
					bool fileExists = File.Exists(tvBanner.FullImagePath);

					if (fileExists)
					{
						if (fanartCount.ContainsKey(tvBanner.SeriesID))
							fanartCount[tvBanner.SeriesID] = fanartCount[tvBanner.SeriesID] + 1;
						else
							fanartCount[tvBanner.SeriesID] = 1;
					}
				}

				foreach (TvDB_ImageWideBanner tvBanner in allBanners)
				{
					if (string.IsNullOrEmpty(tvBanner.FullImagePath)) continue;
					bool fileExists = File.Exists(tvBanner.FullImagePath);

					int bannersAvailable = 0;
					if (fanartCount.ContainsKey(tvBanner.SeriesID))
						bannersAvailable = fanartCount[tvBanner.SeriesID];

					if (!fileExists && bannersAvailable < ServerSettings.TvDB_AutoWideBannersAmount)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(tvBanner.TvDB_ImageWideBannerID, JMMImageType.TvDB_Banner, false);
						cmd.Save();

                        if (fanartCount.ContainsKey(tvBanner.SeriesID))
                            fanartCount[tvBanner.SeriesID] = fanartCount[tvBanner.SeriesID] + 1;
                        else
                            fanartCount[tvBanner.SeriesID] = 1;
					}
				}
			}

			// TvDB Episodes
			TvDB_EpisodeRepository repTvEpisodes = new TvDB_EpisodeRepository();
			foreach (TvDB_Episode tvEpisode in repTvEpisodes.GetAll())
			{
				if (string.IsNullOrEmpty(tvEpisode.FullImagePath)) continue;
				bool fileExists = File.Exists(tvEpisode.FullImagePath);
				if (!fileExists)
				{
					CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(tvEpisode.TvDB_EpisodeID, JMMImageType.TvDB_Episode, false);
					cmd.Save();
				}
			}

			// MovieDB Posters
			if (ServerSettings.MovieDB_AutoPosters)
			{
				MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository();
				Dictionary<int, int> postersCount = new Dictionary<int, int>();

				// build a dictionary of series and how many images exist
				List<MovieDB_Poster> allPosters = repMoviePosters.GetAll();
				foreach (MovieDB_Poster moviePoster in allPosters)
				{
					if (string.IsNullOrEmpty(moviePoster.FullImagePath)) continue;
					bool fileExists = File.Exists(moviePoster.FullImagePath);

					if (fileExists)
					{
						if (postersCount.ContainsKey(moviePoster.MovieId))
							postersCount[moviePoster.MovieId] = postersCount[moviePoster.MovieId] + 1;
						else
							postersCount[moviePoster.MovieId] = 1;
					}
				}

				foreach (MovieDB_Poster moviePoster in allPosters)
				{
					if (string.IsNullOrEmpty(moviePoster.FullImagePath)) continue;
					bool fileExists = File.Exists(moviePoster.FullImagePath);

					int postersAvailable = 0;
					if (postersCount.ContainsKey(moviePoster.MovieId))
						postersAvailable = postersCount[moviePoster.MovieId];

					if (!fileExists && postersAvailable < ServerSettings.MovieDB_AutoPostersAmount)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(moviePoster.MovieDB_PosterID, JMMImageType.MovieDB_Poster, false);
						cmd.Save();

                        if (postersCount.ContainsKey(moviePoster.MovieId))
                            postersCount[moviePoster.MovieId] = postersCount[moviePoster.MovieId] + 1;
                        else
                            postersCount[moviePoster.MovieId] = 1;
					}
				}
			}

			// MovieDB Fanart
			if (ServerSettings.MovieDB_AutoFanart)
			{
				MovieDB_FanartRepository repMovieFanarts = new MovieDB_FanartRepository();
				Dictionary<int, int> fanartCount = new Dictionary<int, int>();

				// build a dictionary of series and how many images exist
				List<MovieDB_Fanart> allFanarts = repMovieFanarts.GetAll();
				foreach (MovieDB_Fanart movieFanart in allFanarts)
				{
					if (string.IsNullOrEmpty(movieFanart.FullImagePath)) continue;
					bool fileExists = File.Exists(movieFanart.FullImagePath);

					if (fileExists)
					{
						if (fanartCount.ContainsKey(movieFanart.MovieId))
							fanartCount[movieFanart.MovieId] = fanartCount[movieFanart.MovieId] + 1;
						else
							fanartCount[movieFanart.MovieId] = 1;
					}
				}

				foreach (MovieDB_Fanart movieFanart in repMovieFanarts.GetAll())
				{
					if (string.IsNullOrEmpty(movieFanart.FullImagePath)) continue;
					bool fileExists = File.Exists(movieFanart.FullImagePath);

					int fanartAvailable = 0;
					if (fanartCount.ContainsKey(movieFanart.MovieId))
						fanartAvailable = fanartCount[movieFanart.MovieId];

					if (!fileExists && fanartAvailable < ServerSettings.MovieDB_AutoFanartAmount)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(movieFanart.MovieDB_FanartID, JMMImageType.MovieDB_FanArt, false);
						cmd.Save();

                        if (fanartCount.ContainsKey(movieFanart.MovieId))
                            fanartCount[movieFanart.MovieId] = fanartCount[movieFanart.MovieId] + 1;
                        else
                            fanartCount[movieFanart.MovieId] = 1;
					}
				}
			}

			// Trakt Posters
			if (ServerSettings.Trakt_DownloadPosters)
			{
				Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository();
				foreach (Trakt_ImagePoster traktPoster in repTraktPosters.GetAll())
				{
					if (string.IsNullOrEmpty(traktPoster.FullImagePath)) continue;
					bool fileExists = File.Exists(traktPoster.FullImagePath);
					if (!fileExists)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(traktPoster.Trakt_ImagePosterID, JMMImageType.Trakt_Poster, false);
						cmd.Save();
					}
				}
			}

			// Trakt Fanart
			if (ServerSettings.Trakt_DownloadFanart)
			{
				Trakt_ImageFanartRepository repTraktFanarts = new Trakt_ImageFanartRepository();
				foreach (Trakt_ImageFanart traktFanart in repTraktFanarts.GetAll())
				{
					if (string.IsNullOrEmpty(traktFanart.FullImagePath)) continue;
					bool fileExists = File.Exists(traktFanart.FullImagePath);
					if (!fileExists)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(traktFanart.Trakt_ImageFanartID, JMMImageType.Trakt_Fanart, false);
						cmd.Save();
					}
				}
			}

			// Trakt Episode
			if (ServerSettings.Trakt_DownloadEpisodes)
			{
				Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository();
				foreach (Trakt_Episode traktEp in repTraktEpisodes.GetAll())
				{
					if (string.IsNullOrEmpty(traktEp.FullImagePath)) continue;
                    if (!traktEp.TraktID.HasValue) continue; // if it doesn't have a TraktID it means it is old data

					bool fileExists = File.Exists(traktEp.FullImagePath);
					if (!fileExists)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(traktEp.Trakt_EpisodeID, JMMImageType.Trakt_Episode, false);
						cmd.Save();
					}
				}
			}

			// AniDB Characters
			if (ServerSettings.AniDB_DownloadCharacters)
			{
				AniDB_CharacterRepository repChars = new AniDB_CharacterRepository();
				foreach (AniDB_Character chr in repChars.GetAll())
				{

                    if (chr.CharID == 75250)
                    {
                        Console.WriteLine("test");
                    }

					if (string.IsNullOrEmpty(chr.PosterPath)) continue;
					bool fileExists = File.Exists(chr.PosterPath);
					if (!fileExists)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(chr.AniDB_CharacterID, JMMImageType.AniDB_Character, false);
						cmd.Save();
					}
				}
			}

			// AniDB Creators
			if (ServerSettings.AniDB_DownloadCreators)
			{
				AniDB_SeiyuuRepository repSeiyuu = new AniDB_SeiyuuRepository();
				foreach (AniDB_Seiyuu seiyuu in repSeiyuu.GetAll())
				{
					if (string.IsNullOrEmpty(seiyuu.PosterPath)) continue;
					bool fileExists = File.Exists(seiyuu.PosterPath);
					if (!fileExists)
					{
						CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(seiyuu.AniDB_SeiyuuID, JMMImageType.AniDB_Creator, false);
						cmd.Save();
					}
				}
			}
		}
Exemple #12
0
        public static void UpdateSeriesInfoAndImages(int seriesID, bool forceRefresh, bool downloadImages)
        {
            try
            {
                // update the series info
                TvDB_Series tvSeries = GetSeriesInfoOnline(seriesID, forceRefresh);
                if (tvSeries == null)
                {
                    return;
                }

                if (downloadImages)
                {
                    DownloadAutomaticImages(seriesID, forceRefresh);
                }

                List <EpisodeRecord> episodeItems = GetEpisodesOnline(seriesID);
                logger.Trace($"Found {episodeItems.Count} Episode nodes");

                List <int> existingEpIds = new List <int>();
                foreach (EpisodeRecord item in episodeItems)
                {
                    if (!existingEpIds.Contains(item.Id))
                    {
                        existingEpIds.Add(item.Id);
                    }

                    var ep = RepoFactory.TvDB_Episode.GetByTvDBID(item.Id) ?? new TvDB_Episode();
                    ep.Populate(item);
                    RepoFactory.TvDB_Episode.Save(ep);

                    if (downloadImages)
                    {
                        if (!string.IsNullOrEmpty(ep.Filename))
                        {
                            bool fileExists = File.Exists(ep.GetFullImagePath());
                            if (!fileExists || forceRefresh)
                            {
                                CommandRequest_DownloadImage cmd =
                                    new CommandRequest_DownloadImage(ep.TvDB_EpisodeID,
                                                                     ImageEntityType.TvDB_Episode, forceRefresh);
                                cmd.Save();
                            }
                        }
                    }
                }

                // get all the existing tvdb episodes, to see if any have been deleted
                List <TvDB_Episode> allEps = RepoFactory.TvDB_Episode.GetBySeriesID(seriesID);
                foreach (TvDB_Episode oldEp in allEps)
                {
                    if (!existingEpIds.Contains(oldEp.Id))
                    {
                        RepoFactory.TvDB_Episode.Delete(oldEp.TvDB_EpisodeID);
                    }
                }

                // Updating stats as it will not happen with the episodes
                RepoFactory.CrossRef_AniDB_TvDB.GetByTvDBID(seriesID).Select(a => a.AniDBID).Distinct()
                .ForEach(SVR_AniDB_Anime.UpdateStatsByAnimeID);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"Error in TVDBHelper.GetEpisodes: {ex}");
                Analytics.PostException(ex);
            }
        }
        public static void SaveMovieToDatabase(MovieDB_Movie_Result searchResult, bool saveImages,
                                               bool isTrakt)
        {
            // save to the DB
            MovieDB_Movie movie;

            using (var upd = Repo.Instance.MovieDb_Movie.BeginAddOrUpdate(() => Repo.Instance.MovieDb_Movie.GetByMovieID(searchResult.MovieID).FirstOrDefault()))
            {
                upd.Entity.Populate_RA(searchResult);
                // Only save movie info if source is not trakt, this presents adding tv shows as movies
                // Needs better fix later on
                if (!isTrakt)
                {
                    movie = upd.Commit();
                }
                else
                {
                    movie = upd.Entity;
                }
            }

            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;
                    using (var upd = Repo.Instance.MovieDB_Poster.BeginAddOrUpdate(() => Repo.Instance.MovieDB_Poster.GetByOnlineID(img.URL)))
                    {
                        upd.Entity.Populate_RA(img, movie.MovieId);
                        poster = upd.Commit();
                    }
                    if (!string.IsNullOrEmpty(poster.GetFullImagePath()) && File.Exists(poster.GetFullImagePath()))
                    {
                        numPostersDownloaded++;
                    }
                }
                else
                {
                    // fanart (backdrop)
                    MovieDB_Fanart fanart;
                    using (var upd = Repo.Instance.MovieDB_Fanart.BeginAddOrUpdate(() => Repo.Instance.MovieDB_Fanart.GetByOnlineID(img.URL)))
                    {
                        upd.Entity.Populate_RA(img, movie.MovieId);
                        fanart = upd.Commit();
                    }
                    if (!string.IsNullOrEmpty(fanart.GetFullImagePath()) && File.Exists(fanart.GetFullImagePath()))
                    {
                        numFanartDownloaded++;
                    }
                }
            }

            // download the posters
            if (ServerSettings.Instance.MovieDb.AutoPosters || isTrakt)
            {
                foreach (MovieDB_Poster poster in Repo.Instance.MovieDB_Poster.GetByMovieID(movie.MovieId))
                {
                    if (numPostersDownloaded < ServerSettings.Instance.MovieDb.AutoPostersAmount)
                    {
                        // download the image
                        if (!string.IsNullOrEmpty(poster.GetFullImagePath()) && !File.Exists(poster.GetFullImagePath()))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(poster.MovieDB_PosterID,
                                                                                                ImageEntityType.MovieDB_Poster, false);
                            cmd.Save();
                            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.GetFullImagePath()))
                        {
                            Repo.Instance.MovieDB_Poster.Delete(poster.MovieDB_PosterID);
                        }
                    }
                }
            }

            // download the fanart
            if (ServerSettings.Instance.MovieDb.AutoFanart || isTrakt)
            {
                foreach (MovieDB_Fanart fanart in Repo.Instance.MovieDB_Fanart.GetByMovieID(movie.MovieId))
                {
                    if (numFanartDownloaded < ServerSettings.Instance.MovieDb.AutoFanartAmount)
                    {
                        // download the image
                        if (!string.IsNullOrEmpty(fanart.GetFullImagePath()) && !File.Exists(fanart.GetFullImagePath()))
                        {
                            CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(fanart.MovieDB_FanartID,
                                                                                                ImageEntityType.MovieDB_FanArt, false);
                            cmd.Save();
                            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.GetFullImagePath()))
                        {
                            Repo.Instance.MovieDB_Fanart.Delete(fanart.MovieDB_FanartID);
                        }
                    }
                }
            }
        }
Exemple #14
0
        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)
                    {
                        break;
                    }

                    // 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++;
                    }
                }
            }

            // download the fanart
            if (ServerSettings.MovieDB_AutoFanart)
            {
                foreach (MovieDB_Fanart fanart in repFanart.GetByMovieID(session, movie.MovieId))
                {
                    if (numFanartDownloaded >= ServerSettings.MovieDB_AutoFanartAmount)
                    {
                        break;
                    }

                    // 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++;
                    }
                }
            }
        }