public static string LinkAniDBTrakt(ISession session, int animeID, enEpisodeType aniEpType, int aniEpNumber, string traktID, int seasonNumber, int traktEpNumber, bool excludeFromWebCache) { CrossRef_AniDB_TraktV2Repository repCrossRef = new CrossRef_AniDB_TraktV2Repository(); List<CrossRef_AniDB_TraktV2> xrefTemps = repCrossRef.GetByAnimeIDEpTypeEpNumber(session, animeID, (int)aniEpType, aniEpNumber); if (xrefTemps != null && xrefTemps.Count > 0) { foreach (CrossRef_AniDB_TraktV2 xrefTemp in xrefTemps) { // delete the existing one if we are updating TraktTVHelper.RemoveLinkAniDBTrakt(xrefTemp.AnimeID, (enEpisodeType)xrefTemp.AniDBStartEpisodeType, xrefTemp.AniDBStartEpisodeNumber, xrefTemp.TraktID, xrefTemp.TraktSeasonNumber, xrefTemp.TraktStartEpisodeNumber); } } // check if we have this information locally // if not download it now Trakt_ShowRepository repSeries = new Trakt_ShowRepository(); Trakt_Show traktShow = repSeries.GetByTraktSlug(traktID); if (traktShow == null) { // we download the series info here just so that we have the basic info in the // database before the queued task runs later TraktV2ShowExtended tvshow = GetShowInfoV2(traktID); } // download and update series info, episode info and episode images // will also download fanart, posters and wide banners // download fanart, posters DownloadAllImages(traktID); CrossRef_AniDB_TraktV2 xref = repCrossRef.GetByTraktID(session, traktID, seasonNumber, traktEpNumber, animeID, (int)aniEpType, aniEpNumber); if (xref == null) xref = new CrossRef_AniDB_TraktV2(); xref.AnimeID = animeID; xref.AniDBStartEpisodeType = (int)aniEpType; xref.AniDBStartEpisodeNumber = aniEpNumber; xref.TraktID = traktID; xref.TraktSeasonNumber = seasonNumber; xref.TraktStartEpisodeNumber = traktEpNumber; if (traktShow != null) xref.TraktTitle = traktShow.Title; if (excludeFromWebCache) xref.CrossRefSource = (int)CrossRefSource.WebCache; else xref.CrossRefSource = (int)CrossRefSource.User; repCrossRef.Save(xref); StatsCache.Instance.UpdateUsingAnime(animeID); logger.Trace("Changed trakt association: {0}", animeID); if (!excludeFromWebCache && ServerSettings.WebCache_Trakt_Send) { CommandRequest_WebCacheSendXRefAniDBTrakt req = new CommandRequest_WebCacheSendXRefAniDBTrakt(xref.CrossRef_AniDB_TraktV2ID); req.Save(); } return ""; }
public static void MigrateTraktLinks_V1_to_V2() { try { AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); Trakt_EpisodeRepository repEps = new Trakt_EpisodeRepository(); Trakt_ShowRepository repShows = new Trakt_ShowRepository(); CrossRef_AniDB_TraktRepository repCrossRefTrakt = new CrossRef_AniDB_TraktRepository(); CrossRef_AniDB_TraktV2Repository repCrossRefTraktNew = new CrossRef_AniDB_TraktV2Repository(); using (var session = JMMService.SessionFactory.OpenSession()) { List<CrossRef_AniDB_Trakt> xrefsTrakt = repCrossRefTrakt.GetAll(); foreach (CrossRef_AniDB_Trakt xrefTrakt in xrefsTrakt) { CrossRef_AniDB_TraktV2 xrefNew = new CrossRef_AniDB_TraktV2(); xrefNew.AnimeID = xrefTrakt.AnimeID; xrefNew.CrossRefSource = xrefTrakt.CrossRefSource; xrefNew.TraktID = xrefTrakt.TraktID; xrefNew.TraktSeasonNumber = xrefTrakt.TraktSeasonNumber; Trakt_Show show = xrefTrakt.GetByTraktShow(session); if (show != null) xrefNew.TraktTitle = show.Title; // determine start ep type if (xrefTrakt.TraktSeasonNumber == 0) xrefNew.AniDBStartEpisodeType = (int)AniDBAPI.enEpisodeType.Special; else xrefNew.AniDBStartEpisodeType = (int)AniDBAPI.enEpisodeType.Episode; xrefNew.AniDBStartEpisodeNumber = 1; xrefNew.TraktStartEpisodeNumber = 1; repCrossRefTraktNew.Save(xrefNew); } // create cross ref's for specials foreach (CrossRef_AniDB_Trakt xrefTrakt in xrefsTrakt) { AniDB_Anime anime = repAnime.GetByAnimeID(xrefTrakt.AnimeID); if (anime == null) continue; Trakt_Show show = xrefTrakt.GetByTraktShow(session); if (show == null) continue; // this anime has specials if (anime.EpisodeCountSpecial <= 0) continue; // this Trakt series has a season 0 (specials) List<int> seasons = repEps.GetSeasonNumbersForSeries(show.Trakt_ShowID); if (!seasons.Contains(0)) continue; //make sure we are not doubling up CrossRef_AniDB_TraktV2 temp = repCrossRefTraktNew.GetByTraktID(xrefTrakt.TraktID, 0, 1, xrefTrakt.AnimeID, (int)AniDBAPI.enEpisodeType.Special, 1); if (temp != null) continue; CrossRef_AniDB_TraktV2 xrefNew = new CrossRef_AniDB_TraktV2(); xrefNew.AnimeID = xrefTrakt.AnimeID; xrefNew.CrossRefSource = xrefTrakt.CrossRefSource; xrefNew.TraktID = xrefTrakt.TraktID; xrefNew.TraktSeasonNumber = 0; xrefNew.TraktStartEpisodeNumber = 1; xrefNew.AniDBStartEpisodeType = (int)AniDBAPI.enEpisodeType.Special; xrefNew.AniDBStartEpisodeNumber = 1; xrefNew.TraktTitle = show.Title; repCrossRefTraktNew.Save(xrefNew); } } } catch (Exception ex) { logger.ErrorException("Could not MigrateTraktLinks_V1_to_V2: " + ex.ToString(), ex); } }