public override void ProcessCommand() { logger.Info("Processing CommandRequest_MovieDBSearchAnime: {0}", AnimeID); try { using (var session = JMMService.SessionFactory.OpenSession()) { // first check if the user wants to use the web cache if (ServerSettings.WebCache_TvDB_Get) { try { MovieDB_MovieRepository repMovies = new MovieDB_MovieRepository(); JMMServer.Providers.Azure.CrossRef_AniDB_Other crossRef = JMMServer.Providers.Azure.AzureWebAPI.Get_CrossRefAniDBOther(AnimeID, CrossRefType.MovieDB); if (crossRef != null) { int movieID = int.Parse(crossRef.CrossRefID); MovieDB_Movie movie = repMovies.GetByOnlineID(session, movieID); if (movie == null) { // update the info from online MovieDBHelper.UpdateMovieInfo(session, movieID, true); movie = repMovies.GetByOnlineID(movieID); } if (movie != null) { // since we are using the web cache result, let's save it MovieDBHelper.LinkAniDBMovieDB(AnimeID, movieID, true); return; } } } catch (Exception) { } } string searchCriteria = ""; AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AniDB_Anime anime = repAnime.GetByAnimeID(session, AnimeID); if (anime == null) { return; } searchCriteria = anime.MainTitle; // if not wanting to use web cache, or no match found on the web cache go to TvDB directly List <MovieDB_Movie_Result> results = MovieDBHelper.Search(searchCriteria); logger.Trace("Found {0} moviedb results for {1} on TheTvDB", results.Count, searchCriteria); if (ProcessSearchResults(session, results, searchCriteria)) { return; } if (results.Count == 0) { foreach (AniDB_Anime_Title title in anime.GetTitles(session)) { if (title.TitleType.ToUpper() != Constants.AnimeTitleType.Official.ToUpper()) { continue; } if (searchCriteria.ToUpper() == title.Title.ToUpper()) { continue; } results = MovieDBHelper.Search(title.Title); logger.Trace("Found {0} moviedb results for search on {1}", results.Count, title.Title); if (ProcessSearchResults(session, results, title.Title)) { return; } } } } } catch (Exception ex) { logger.Error("Error processing CommandRequest_TvDBSearchAnime: {0} - {1}", AnimeID, ex.ToString()); return; } }
public override void Run(IProgress <ICommand> progress = null) { logger.Info("Processing CommandRequest_MovieDBSearchAnime: {0}", AnimeID); try { { ReportInit(progress); // first check if the user wants to use the web cache if (ServerSettings.Instance.WebCache.TvDB_Get) { try { List <WebCache_CrossRef_AniDB_Provider> resultsCache = WebCacheAPI.Instance.GetCrossRef_AniDB_Provider(AnimeID, CrossRefType.MovieDB); ReportUpdate(progress, 30); if (resultsCache != null && resultsCache.Count > 0) { List <WebCache_CrossRef_AniDB_Provider> best = resultsCache.BestProvider(); if (best.Count > 0) { int movieID = int.Parse(best.First().CrossRefID); MovieDB_Movie movie = Repo.Instance.MovieDb_Movie.GetByOnlineID(movieID); if (movie == null) { // update the info from online MovieDBHelper.UpdateMovieInfo(movieID, true); movie = Repo.Instance.MovieDb_Movie.GetByOnlineID(movieID); } ReportUpdate(progress, 20); if (movie != null) { // since we are using the web cache result, let's save it MovieDBHelper.LinkAniDBMovieDB(AnimeID, movieID, true); ReportFinish(progress); return; } return; } } } catch (Exception) { //Ignore } } // Use TvDB setting if (!ServerSettings.Instance.TvDB.AutoLink) { ReportFinish(progress); return; } ReportUpdate(progress, 40); SVR_AniDB_Anime anime = Repo.Instance.AniDB_Anime.GetByAnimeID(AnimeID); if (anime == null) { ReportFinish(progress); return; } string searchCriteria = anime.MainTitle; // if not wanting to use web cache, or no match found on the web cache go to TvDB directly List <MovieDB_Movie_Result> results = MovieDBHelper.Search(searchCriteria); logger.Trace("Found {0} moviedb results for {1} on TheTvDB", results.Count, searchCriteria); ReportUpdate(progress, 60); if (ProcessSearchResults(results, searchCriteria)) { ReportFinish(progress); return; } ReportUpdate(progress, 80); if (results.Count == 0) { foreach (AniDB_Anime_Title title in anime.GetTitles()) { if (!string.Equals(title.TitleType, Shoko.Models.Constants.AnimeTitleType.Official, StringComparison.OrdinalIgnoreCase)) { continue; } if (string.Equals(searchCriteria, title.Title, StringComparison.OrdinalIgnoreCase)) { continue; } results = MovieDBHelper.Search(title.Title); logger.Trace("Found {0} moviedb results for search on {1}", results.Count, title.Title); if (ProcessSearchResults(results, title.Title)) { ReportFinish(progress); return; } } } ReportFinish(progress); } } catch (Exception ex) { ReportError(progress, $"Error processing CommandRequest_TvDBSearchAnime: {AnimeID} - {ex}", ex); } }
public override void ProcessCommand() { logger.Info("Processing CommandRequest_MovieDBSearchAnime: {0}", AnimeID); try { using (var session = DatabaseFactory.SessionFactory.OpenSession()) { ISessionWrapper sessionWrapper = session.Wrap(); // first check if the user wants to use the web cache if (ServerSettings.Instance.WebCache.TvDB_Get) { try { Azure_CrossRef_AniDB_Other crossRef = AzureWebAPI.Get_CrossRefAniDBOther(AnimeID, CrossRefType.MovieDB); if (crossRef != null) { int movieID = int.Parse(crossRef.CrossRefID); MovieDB_Movie movie = RepoFactory.MovieDb_Movie.GetByOnlineID(sessionWrapper, movieID); if (movie == null) { // update the info from online MovieDBHelper.UpdateMovieInfo(session, movieID, true); movie = RepoFactory.MovieDb_Movie.GetByOnlineID(movieID); } if (movie != null) { // since we are using the web cache result, let's save it MovieDBHelper.LinkAniDBMovieDB(AnimeID, movieID, true); return; } } } catch (Exception) { } } // Use TvDB setting if (!ServerSettings.Instance.TvDB.AutoLink) { return; } string searchCriteria = string.Empty; SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(sessionWrapper, AnimeID); if (anime == null) { return; } searchCriteria = anime.MainTitle; // if not wanting to use web cache, or no match found on the web cache go to TvDB directly List <MovieDB_Movie_Result> results = MovieDBHelper.Search(searchCriteria); logger.Trace("Found {0} moviedb results for {1} on TheTvDB", results.Count, searchCriteria); if (ProcessSearchResults(session, results, searchCriteria)) { return; } if (results.Count == 0) { foreach (AniDB_Anime_Title title in anime.GetTitles()) { if (title.TitleType.ToUpper() != Shoko.Models.Constants.AnimeTitleType.Official.ToUpper()) { continue; } if (searchCriteria.ToUpper() == title.Title.ToUpper()) { continue; } results = MovieDBHelper.Search(title.Title); logger.Trace("Found {0} moviedb results for search on {1}", results.Count, title.Title); if (ProcessSearchResults(session, results, title.Title)) { return; } } } } } catch (Exception ex) { logger.Error("Error processing CommandRequest_TvDBSearchAnime: {0} - {1}", AnimeID, ex); } }
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.GetFullImagePath()) && File.Exists(poster.GetFullImagePath())) { 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.GetFullImagePath()) && File.Exists(fanart.GetFullImagePath())) { 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.GetFullImagePath()) && !File.Exists(poster.GetFullImagePath())) { 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.GetFullImagePath())) { 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.GetFullImagePath()) && !File.Exists(fanart.GetFullImagePath())) { 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.GetFullImagePath())) { RepoFactory.MovieDB_Fanart.Delete(fanart.MovieDB_FanartID); } } } } }
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++; } } } }