Пример #1
0
        public static void SaveExtendedShowInfoV2(TraktV2ShowExtended tvshow, List<TraktV2Season> seasons)
        {
            try
            {
                // save this data to the DB for use later
                Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
                Trakt_ShowRepository repShows = new Trakt_ShowRepository();
                Trakt_Show show = repShows.GetByTraktSlug(tvshow.ids.slug);
                if (show == null)
                    show = new Trakt_Show();

                show.Populate(tvshow);
                repShows.Save(show);

                if (tvshow.images != null && tvshow.images.fanart != null)
                {
                    if (!string.IsNullOrEmpty(tvshow.images.fanart.full))
                    {
                        Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1);
                        if (fanart == null)
                        {
                            fanart = new Trakt_ImageFanart();
                            fanart.Enabled = 0;
                        }

                        fanart.ImageURL = tvshow.images.fanart.full;
                        fanart.Season = 1;
                        fanart.Trakt_ShowID = show.Trakt_ShowID;
                        repFanart.Save(fanart);
                    }
                }

                // save the seasons
                Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
                Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
                Trakt_ImagePosterRepository repPosters = new Trakt_ImagePosterRepository();

                // delete episodes if they no longer exist on Trakt
                if (seasons.Count > 0)
                {
                    foreach (Trakt_Episode epTemp in repEpisodes.GetByShowID(show.Trakt_ShowID))
                    {
                        TraktV2Episode ep = null;
                        TraktV2Season sea = seasons.FirstOrDefault(x => x.number == epTemp.Season);
                        if (sea != null)
                            ep = sea.episodes.FirstOrDefault(x => x.number == epTemp.EpisodeNumber);

                        // if the episode is null, it means it doesn't exist on Trakt, so we should delete it
                        if (ep == null)
                            repEpisodes.Delete(epTemp.Trakt_EpisodeID);
                    }
                }

                foreach (TraktV2Season sea in seasons)
                {
                    Trakt_Season season = repSeasons.GetByShowIDAndSeason(show.Trakt_ShowID, sea.number);
                    if (season == null)
                        season = new Trakt_Season();

                    season.Season = sea.number;
                    season.URL = string.Format(TraktURIs.WebsiteSeason, show.TraktID, sea.number);
                    season.Trakt_ShowID = show.Trakt_ShowID;
                    repSeasons.Save(season);

                    if (sea.images != null && sea.images.poster != null)
                    {
                        if (!string.IsNullOrEmpty(sea.images.poster.full))
                        {
                            Trakt_ImagePoster poster = repPosters.GetByShowIDAndSeason(show.Trakt_ShowID, season.Season);
                            if (poster == null)
                            {
                                poster = new Trakt_ImagePoster();
                                poster.Enabled = 0;
                            }

                            poster.ImageURL = sea.images.poster.full;
                            poster.Season = season.Season;
                            poster.Trakt_ShowID = show.Trakt_ShowID;
                            repPosters.Save(poster);
                        }
                    }

                    if (sea.episodes != null)
                    {
                        foreach (TraktV2Episode ep in sea.episodes)
                        {
                            Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, ep.season, ep.number);
                            if (episode == null)
                                episode = new Trakt_Episode();

                            Console.Write(ep.ids.trakt);

                            if (ep.images.screenshot != null)
                                episode.EpisodeImage = ep.images.screenshot.full;
                            else
                                episode.EpisodeImage = string.Empty;

                            episode.TraktID = ep.ids.TraktID;
                            episode.EpisodeNumber = ep.number;
                            episode.Overview = string.Empty; // this is now part of a separate API call for V2, we get this info from TvDB anyway
                            episode.Season = ep.season;
                            episode.Title = ep.title;
                            episode.URL = string.Format(TraktURIs.WebsiteEpisode, show.TraktID, ep.season, ep.number);
                            episode.Trakt_ShowID = show.Trakt_ShowID;
                            repEpisodes.Save(episode);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                logger.ErrorException("Error in TraktTVHelper.SaveExtendedShowInfo: " + ex.ToString(), ex);
            }
        }
Пример #2
0
        public static void RemoveTraktDBEntries(Trakt_Show show)
        {
            Trakt_ShowRepository repShows = new Trakt_ShowRepository();
            CrossRef_AniDB_TraktV2Repository repXRefs = new CrossRef_AniDB_TraktV2Repository();
            Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
            Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
            Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
            Trakt_ImagePosterRepository repPosters = new Trakt_ImagePosterRepository();

            // this means Trakt has no record of this slug.
            // 1. Delete any cross ref links
            foreach (CrossRef_AniDB_TraktV2 xref in repXRefs.GetByTraktID(show.TraktID))
                repXRefs.Delete(xref.CrossRef_AniDB_TraktV2ID);

            // 2. Delete default image links

            // 3. Delete episodes
            foreach (Trakt_Episode epTemp in repEpisodes.GetByShowID(show.Trakt_ShowID))
                repEpisodes.Delete(epTemp.Trakt_EpisodeID);

            // 4. Delete fanart and posters
            foreach (Trakt_ImageFanart fanart in repFanart.GetByShowID(show.Trakt_ShowID))
                repFanart.Delete(fanart.Trakt_ImageFanartID);

            foreach (Trakt_ImagePoster poster in repPosters.GetByShowID(show.Trakt_ShowID))
                repPosters.Delete(poster.Trakt_ImagePosterID);

            // 5. Delete seasons
            foreach (Trakt_Season season in repSeasons.GetByShowID(show.Trakt_ShowID))
                repSeasons.Delete(season.Trakt_SeasonID);

            // 6. Delete the show
            repShows.Delete(show.Trakt_ShowID);
        }
		public override void ProcessCommand()
		{
			logger.Info("Processing CommandRequest_TraktSearchAnime: {0}", AnimeID);

			try
			{
				using (var session = JMMService.SessionFactory.OpenSession())
				{
					// first check if the user wants to use the web cache
					if (ServerSettings.WebCache_Trakt_Get)
					{
						try
						{
                            List<Contract_Azure_CrossRef_AniDB_Trakt> contracts = new List<Contract_Azure_CrossRef_AniDB_Trakt>();

                            List<JMMServer.Providers.Azure.CrossRef_AniDB_Trakt> resultsCache = JMMServer.Providers.Azure.AzureWebAPI.Get_CrossRefAniDBTrakt(AnimeID);
                            if (resultsCache != null && resultsCache.Count > 0)
                            {
                                foreach (JMMServer.Providers.Azure.CrossRef_AniDB_Trakt xref in resultsCache)
                                {

                                    TraktV2ShowExtended showInfo = TraktTVHelper.GetShowInfoV2(xref.TraktID);
                                    if (showInfo != null)
                                    {
                                        logger.Trace("Found trakt match on web cache for {0} - id = {1}", AnimeID, showInfo.title);
                                        TraktTVHelper.LinkAniDBTrakt(AnimeID, (enEpisodeType)xref.AniDBStartEpisodeType, xref.AniDBStartEpisodeNumber, 
                                            xref.TraktID, xref.TraktSeasonNumber, xref.TraktStartEpisodeNumber, true);
                                        return;
                                    }
                                }
                                
                            }
						}
						catch (Exception ex)
						{
							logger.ErrorException(ex.ToString(), ex);
						}
					}


					// lets try to see locally if we have a tvDB link for this anime
					// Trakt allows the use of TvDB ID's or their own Trakt ID's
					CrossRef_AniDB_TvDBV2Repository repCrossRefTvDB = new CrossRef_AniDB_TvDBV2Repository();
					List<CrossRef_AniDB_TvDBV2> xrefTvDBs = repCrossRefTvDB.GetByAnimeID(session, AnimeID);
					if (xrefTvDBs != null && xrefTvDBs.Count > 0) 
					{
                        foreach (CrossRef_AniDB_TvDBV2 tvXRef in xrefTvDBs)
                        {
                            // first search for this show by the TvDB ID
                            List<TraktV2SearchTvDBIDShowResult> searchResults = TraktTVHelper.SearchShowByIDV2(TraktSearchIDType.tvdb, tvXRef.TvDBID.ToString());
                            if (searchResults != null && searchResults.Count > 0)
                            {
                                // since we are searching by ID, there will only be one 'show' result
                                TraktV2Show resShow = null;
                                foreach (TraktV2SearchTvDBIDShowResult res in searchResults)
                                {
                                    if (res.ResultType == SearchIDType.Show)
                                    {
                                        resShow = res.show;
                                        break;
                                    }
                                }

                                if (resShow != null)
                                {
                                    TraktV2ShowExtended showInfo = TraktTVHelper.GetShowInfoV2(resShow.ids.slug);
                                    if (showInfo != null && showInfo.ids != null)
                                    {
                                        // make sure the season specified by TvDB also exists on Trakt
                                        Trakt_ShowRepository repShow = new Trakt_ShowRepository();
                                        Trakt_Show traktShow = repShow.GetByTraktSlug(session, showInfo.ids.slug);
                                        if (traktShow != null)
                                        {
                                            Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
                                            Trakt_Season traktSeason = repSeasons.GetByShowIDAndSeason(session, traktShow.Trakt_ShowID, xrefTvDBs[0].TvDBSeasonNumber);
                                            if (traktSeason != null)
                                            {
                                                logger.Trace("Found trakt match using TvDBID locally {0} - id = {1}", AnimeID, showInfo.title);
                                                TraktTVHelper.LinkAniDBTrakt(AnimeID, (AniDBAPI.enEpisodeType)tvXRef.AniDBStartEpisodeType,
                                                    tvXRef.AniDBStartEpisodeNumber, showInfo.ids.slug, tvXRef.TvDBSeasonNumber, tvXRef.TvDBStartEpisodeNumber, true);
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
					}

					// finally lets try searching Trakt directly
					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<TraktV2SearchShowResult> results = TraktTVHelper.SearchShowV2(searchCriteria);
					logger.Trace("Found {0} trakt results for {1} ", 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 = TraktTVHelper.SearchShowV2(searchCriteria);
							logger.Trace("Found {0} trakt 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 ProcessCommand()
		{
			logger.Info("Processing CommandRequest_TraktSearchAnime: {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
						{
							CrossRef_AniDB_TraktResult crossRef = XMLService.Get_CrossRef_AniDB_Trakt(AnimeID);
							if (crossRef != null)
							{
								TraktTVShow showInfo = TraktTVHelper.GetShowInfo(crossRef.TraktID);
								if (showInfo != null)
								{
									logger.Trace("Found trakt match on web cache for {0} - id = {1}", AnimeID, showInfo.title);
									TraktTVHelper.LinkAniDBTrakt(AnimeID, crossRef.TraktID, crossRef.TraktSeasonNumber, true);
									return;
								}
							}
						}
						catch (Exception ex)
						{
							logger.ErrorException(ex.ToString(), ex);
						}
					}


					// lets try to see locally if we have a tvDB link for this anime
					// Trakt allows the use of TvDB ID's or their own Trakt ID's
					CrossRef_AniDB_TvDBV2Repository repCrossRefTvDB = new CrossRef_AniDB_TvDBV2Repository();
					List<CrossRef_AniDB_TvDBV2> xrefTvDBs = repCrossRefTvDB.GetByAnimeID(session, AnimeID);
					if (xrefTvDBs != null && xrefTvDBs.Count == 1)  //TODO this is temporary code, until trakt also allows multiple links
					{
						TraktTVShow showInfo = TraktTVHelper.GetShowInfo(xrefTvDBs[0].TvDBID);
						if (showInfo != null)
						{
							// make sure the season specified by TvDB also exists on Trakt
							Trakt_ShowRepository repShow = new Trakt_ShowRepository();
							Trakt_Show traktShow = repShow.GetByTraktID(session, showInfo.TraktID);
							if (traktShow != null)
							{
								Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
								Trakt_Season traktSeason = repSeasons.GetByShowIDAndSeason(session, traktShow.Trakt_ShowID, xrefTvDBs[0].TvDBSeasonNumber);
								if (traktSeason != null)
								{
									logger.Trace("Found trakt match using TvDBID locally {0} - id = {1}", AnimeID, showInfo.title);
									TraktTVHelper.LinkAniDBTrakt(AnimeID, showInfo.TraktID, traktSeason.Season, true);
									return;
								}
							}
						}
					}

					// if not lets try the tvdb web cache based on the same reasoning
					if (ServerSettings.WebCache_TvDB_Get)
					{
						List<JMMServer.Providers.Azure.CrossRef_AniDB_TvDB> cacheResults = JMMServer.Providers.Azure.AzureWebAPI.Get_CrossRefAniDBTvDB(AnimeID);
						if (cacheResults != null && cacheResults.Count > 0)
						{
							TraktTVShow showInfo = TraktTVHelper.GetShowInfo(cacheResults[0].TvDBID);
							if (showInfo != null)
							{
								// make sure the season specified by TvDB also exists on Trakt
								Trakt_ShowRepository repShow = new Trakt_ShowRepository();
								Trakt_Show traktShow = repShow.GetByTraktID(session, showInfo.TraktID);
								if (traktShow != null)
								{
									Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
									Trakt_Season traktSeason = repSeasons.GetByShowIDAndSeason(session, traktShow.Trakt_ShowID, cacheResults[0].TvDBSeasonNumber);
									if (traktSeason != null)
									{
										logger.Trace("Found trakt match on web cache by using TvDBID {0} - id = {1}", AnimeID, showInfo.title);
										TraktTVHelper.LinkAniDBTrakt(AnimeID, showInfo.TraktID, traktSeason.Season, true);
										return;
									}
								}
							}
						}
					}

					// finally lets try searching Trakt directly
					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<TraktTVShow> results = TraktTVHelper.SearchShow(searchCriteria);
					logger.Trace("Found {0} trakt results for {1} ", 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 = TraktTVHelper.SearchShow(searchCriteria);
							logger.Trace("Found {0} trakt 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;
			}
		}
Пример #5
0
		public static void SaveExtendedShowInfo(TraktTVShow tvshow)
		{ 
			try
			{
				// save this data to the DB for use later
				Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(tvshow.TraktID);
				if (show == null)
					show = new Trakt_Show();

				show.Populate(tvshow);
				repShows.Save(show);


				if (tvshow.images != null)
				{
					if (!string.IsNullOrEmpty(tvshow.images.fanart))
					{
						Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1);
						if (fanart == null)
						{
							fanart = new Trakt_ImageFanart();
							fanart.Enabled = 0;
						}

						fanart.ImageURL = tvshow.images.fanart;
						fanart.Season = 1;
						fanart.Trakt_ShowID = show.Trakt_ShowID;
						repFanart.Save(fanart);
					}
				}


				// save the seasons
				Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
				Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
				Trakt_ImagePosterRepository repPosters = new Trakt_ImagePosterRepository();

				foreach (TraktTVSeason sea in tvshow.seasons)
				{
					Trakt_Season season = repSeasons.GetByShowIDAndSeason(show.Trakt_ShowID, int.Parse(sea.season));
					if (season == null)
						season = new Trakt_Season();

					season.Season = int.Parse(sea.season);
					season.URL = sea.url;
					season.Trakt_ShowID = show.Trakt_ShowID;
					repSeasons.Save(season);

					if (sea.images != null)
					{
						if (!string.IsNullOrEmpty(sea.images.poster))
						{
							Trakt_ImagePoster poster = repPosters.GetByShowIDAndSeason(show.Trakt_ShowID, season.Season);
							if (poster == null)
							{
								poster = new Trakt_ImagePoster();
								poster.Enabled = 0;
							}

							poster.ImageURL = sea.images.poster;
							poster.Season = season.Season;
							poster.Trakt_ShowID = show.Trakt_ShowID;
							repPosters.Save(poster);
						}
					}

					foreach (TraktTVEpisode ep in sea.episodes)
					{
						Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, int.Parse(ep.season), int.Parse(ep.episode));
						if (episode == null)
							episode = new Trakt_Episode();

						episode.EpisodeImage = ep.screen;
						episode.EpisodeNumber = int.Parse(ep.episode);
						episode.Overview = ep.overview;
						episode.Season = int.Parse(ep.season);
						episode.Title = ep.title;
						episode.URL = ep.url;
						episode.Trakt_ShowID = show.Trakt_ShowID;
						repEpisodes.Save(episode);
					}
				}


			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.SaveExtendedShowInfo: " + ex.ToString(), ex);
			}
		}