public List<Contract_AnimeEpisode> GetEpisodesForSeries(int animeSeriesID, int userID) { List<Contract_AnimeEpisode> eps = new List<Contract_AnimeEpisode>(); try { DateTime start = DateTime.Now; AnimeEpisodeRepository repEps = new AnimeEpisodeRepository(); AnimeEpisode_UserRepository repEpUsers = new AnimeEpisode_UserRepository(); AnimeSeriesRepository repAnimeSer = new AnimeSeriesRepository(); VideoLocalRepository repVids = new VideoLocalRepository(); CrossRef_File_EpisodeRepository repCrossRefs = new CrossRef_File_EpisodeRepository(); // get all the data first // we do this to reduce the amount of database calls, which makes it a lot faster AnimeSeries series = repAnimeSer.GetByID(animeSeriesID); if (series == null) return eps; List<AnimeEpisode> epList = repEps.GetBySeriesID(animeSeriesID); List<AnimeEpisode_User> userRecordList = repEpUsers.GetByUserIDAndSeriesID(userID, animeSeriesID); Dictionary<int, AnimeEpisode_User> dictUserRecords = new Dictionary<int, AnimeEpisode_User>(); foreach (AnimeEpisode_User epuser in userRecordList) dictUserRecords[epuser.AnimeEpisodeID] = epuser; AniDB_EpisodeRepository repAniEps = new AniDB_EpisodeRepository(); List<AniDB_Episode> aniEpList = repAniEps.GetByAnimeID(series.AniDB_ID); Dictionary<int, AniDB_Episode> dictAniEps = new Dictionary<int, AniDB_Episode>(); foreach (AniDB_Episode aniep in aniEpList) dictAniEps[aniep.EpisodeID] = aniep; // get all the video local records and cross refs List<VideoLocal> vids = repVids.GetByAniDBAnimeID(series.AniDB_ID); List<CrossRef_File_Episode> crossRefs = repCrossRefs.GetByAnimeID(series.AniDB_ID); TimeSpan ts = DateTime.Now - start; logger.Info("GetEpisodesForSeries: {0} (Database) in {1} ms", series.GetAnime().MainTitle, ts.TotalMilliseconds); start = DateTime.Now; foreach (AnimeEpisode ep in epList) { if (dictAniEps.ContainsKey(ep.AniDB_EpisodeID)) { List<VideoLocal> epVids = new List<VideoLocal>(); foreach (CrossRef_File_Episode xref in crossRefs) { if (xref.EpisodeID == dictAniEps[ep.AniDB_EpisodeID].EpisodeID) { // don't add the same file twice, this will occur when // one file appears over more than one episodes Dictionary<string, string> addedFiles = new Dictionary<string, string>(); foreach (VideoLocal vl in vids) { if (string.Equals(xref.Hash, vl.Hash, StringComparison.InvariantCultureIgnoreCase)) { if (!addedFiles.ContainsKey(xref.Hash.Trim().ToUpper())) { addedFiles[xref.Hash.Trim().ToUpper()] = xref.Hash.Trim().ToUpper(); epVids.Add(vl); } } } } } AnimeEpisode_User epuser = null; if (dictUserRecords.ContainsKey(ep.AnimeEpisodeID)) epuser = dictUserRecords[ep.AnimeEpisodeID]; eps.Add(ep.ToContract(dictAniEps[ep.AniDB_EpisodeID], epVids, epuser, null)); } } ts = DateTime.Now - start; logger.Info("GetEpisodesForSeries: {0} (Contracts) in {1} ms", series.GetAnime().MainTitle, ts.TotalMilliseconds); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return eps; }
public List<Contract_AnimeEpisode> GetEpisodesForSeriesOld(int animeSeriesID) { List<Contract_AnimeEpisode> eps = new List<Contract_AnimeEpisode>(); try { DateTime start = DateTime.Now; AnimeEpisodeRepository repEps = new AnimeEpisodeRepository(); AnimeSeriesRepository repAnimeSer = new AnimeSeriesRepository(); CrossRef_File_EpisodeRepository repCrossRefs = new CrossRef_File_EpisodeRepository(); // get all the data first // we do this to reduce the amount of database calls, which makes it a lot faster AnimeSeries series = repAnimeSer.GetByID(animeSeriesID); if (series == null) return eps; List<AnimeEpisode> epList = repEps.GetBySeriesID(animeSeriesID); AniDB_EpisodeRepository repAniEps = new AniDB_EpisodeRepository(); List<AniDB_Episode> aniEpList = repAniEps.GetByAnimeID(series.AniDB_ID); Dictionary<int, AniDB_Episode> dictAniEps = new Dictionary<int, AniDB_Episode>(); foreach (AniDB_Episode aniep in aniEpList) dictAniEps[aniep.EpisodeID] = aniep; List<CrossRef_File_Episode> crossRefList = repCrossRefs.GetByAnimeID(series.AniDB_ID); TimeSpan ts = DateTime.Now - start; logger.Info("GetEpisodesForSeries: {0} (Database) in {1} ms", series.GetAnime().MainTitle, ts.TotalMilliseconds); start = DateTime.Now; foreach (AnimeEpisode ep in epList) { List<CrossRef_File_Episode> xrefs = new List<CrossRef_File_Episode>(); foreach (CrossRef_File_Episode xref in crossRefList) { if (ep.AniDB_EpisodeID == xref.EpisodeID) xrefs.Add(xref); } if (dictAniEps.ContainsKey(ep.AniDB_EpisodeID)) eps.Add(ep.ToContractOld(dictAniEps[ep.AniDB_EpisodeID])); } ts = DateTime.Now - start; logger.Info("GetEpisodesForSeries: {0} (Contracts) in {1} ms", series.GetAnime().MainTitle, ts.TotalMilliseconds); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return eps; }
public void UpdateUsingGroup(ISession session, int animeGroupID) { try { DateTime start = DateTime.Now; AnimeGroupRepository repGroups = new AnimeGroupRepository(); AnimeGroup thisgrp = repGroups.GetByID(session, animeGroupID); if (thisgrp == null) return; AdhocRepository repAdHoc = new AdhocRepository(); // get a list of all the groups including this one and everthing above it the heirarchy List<AnimeGroup> allgroups = new List<AnimeGroup>(); allgroups.Add(thisgrp); int? groupID = thisgrp.AnimeGroupParentID; while (groupID.HasValue) { AnimeGroup grpTemp = repGroups.GetByID(session, groupID.Value); if (grpTemp != null) { allgroups.Add(grpTemp); groupID = grpTemp.AnimeGroupParentID; } else groupID = null; } TimeSpan ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP - STEP 1 ({0}) in {1} ms", thisgrp.GroupName, ts.TotalMilliseconds); start = DateTime.Now; VideoLocalRepository repVids = new VideoLocalRepository(); CrossRef_File_EpisodeRepository repXrefs = new CrossRef_File_EpisodeRepository(); foreach (AnimeGroup grp in allgroups) { StatGroupCategories[grp.AnimeGroupID] = grp.CategoriesString; StatGroupTitles[grp.AnimeGroupID] = grp.TitlesString; StatGroupVideoQuality[grp.AnimeGroupID] = grp.VideoQualityString; ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP - STEP 2 ({0}) in {1} ms", grp.GroupName, ts.TotalMilliseconds); start = DateTime.Now; DateTime? airDate_Min = null; DateTime? airDate_Max = null; DateTime? endDate = new DateTime(1980, 1, 1); DateTime? seriesCreatedDate = null; bool isComplete = false; bool hasFinishedAiring = false; bool isCurrentlyAiring = false; string videoQualityEpisodes = ""; List<string> audioLanguages = new List<string>(); List<string> subtitleLanguages = new List<string>(); bool hasTvDB = true; bool hasMAL = true; bool hasMovieDB = true; bool hasMovieDBOrTvDB = true; int seriesCount = 0; int epCount = 0; foreach (AnimeSeries series in grp.GetAllSeries(session)) { seriesCount++; List<VideoLocal> vidsTemp = repVids.GetByAniDBAnimeID(session, series.AniDB_ID); List<CrossRef_File_Episode> crossRefs = repXrefs.GetByAnimeID(session, series.AniDB_ID); Dictionary<int, List<CrossRef_File_Episode>> dictCrossRefs = new Dictionary<int, List<CrossRef_File_Episode>>(); foreach (CrossRef_File_Episode xref in crossRefs) { if (!dictCrossRefs.ContainsKey(xref.EpisodeID)) dictCrossRefs[xref.EpisodeID] = new List<CrossRef_File_Episode>(); dictCrossRefs[xref.EpisodeID].Add(xref); } Dictionary<string, VideoLocal> dictVids = new Dictionary<string, VideoLocal>(); foreach (VideoLocal vid in vidsTemp) dictVids[vid.Hash] = vid; // All Video Quality Episodes // Try to determine if this anime has all the episodes available at a certain video quality // e.g. the series has all episodes in blu-ray // Also look at languages Dictionary<string, int> vidQualEpCounts = new Dictionary<string,int>(); // video quality, count of episodes foreach (AnimeEpisode ep in series.GetAnimeEpisodes(session)) { if (ep.EpisodeTypeEnum != AniDBAPI.enEpisodeType.Episode) continue; List<VideoLocal> epVids = new List<VideoLocal>(); if (dictCrossRefs.ContainsKey(ep.AniDB_EpisodeID)) { foreach (CrossRef_File_Episode xref in dictCrossRefs[ep.AniDB_EpisodeID]) { if (xref.EpisodeID == ep.AniDB_EpisodeID) { if (dictVids.ContainsKey(xref.Hash)) epVids.Add(dictVids[xref.Hash]); } } } List<string> qualityAddedSoFar = new List<string>(); // handle mutliple files of the same quality for one episode foreach (VideoLocal vid in epVids) { AniDB_File anifile = vid.GetAniDBFile(session); if (anifile == null) continue; if (!qualityAddedSoFar.Contains(anifile.File_Source)) { if (!vidQualEpCounts.ContainsKey(anifile.File_Source)) vidQualEpCounts[anifile.File_Source] = 1; else vidQualEpCounts[anifile.File_Source]++; qualityAddedSoFar.Add(anifile.File_Source); } } } ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP/Series - STEP 3 ({0}/{1}) in {2} ms",grp.GroupName, series.AnimeSeriesID, ts.TotalMilliseconds); start = DateTime.Now; AniDB_Anime anime = series.GetAnime(session); epCount = epCount + anime.EpisodeCountNormal; foreach (KeyValuePair<string, int> kvp in vidQualEpCounts) { int index = videoQualityEpisodes.IndexOf(kvp.Key, 0, StringComparison.InvariantCultureIgnoreCase); if (index > -1) continue; // don't add if we already have it if (anime.EpisodeCountNormal == kvp.Value) { if (videoQualityEpisodes.Length > 0) videoQualityEpisodes += ","; videoQualityEpisodes += kvp.Key; } } ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP/Series - STEP 4 ({0}/{1}) in {2} ms", grp.GroupName, series.AnimeSeriesID, ts.TotalMilliseconds); start = DateTime.Now; // audio languages Dictionary<int, LanguageStat> dicAudio = repAdHoc.GetAudioLanguageStatsByAnime(session, anime.AnimeID); foreach (KeyValuePair<int, LanguageStat> kvp in dicAudio) { foreach (string lanName in kvp.Value.LanguageNames) { if (!audioLanguages.Contains(lanName)) audioLanguages.Add(lanName); } } ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP/Series - STEP 5 ({0}/{1}) in {2} ms", grp.GroupName, series.AnimeSeriesID, ts.TotalMilliseconds); start = DateTime.Now; // subtitle languages Dictionary<int, LanguageStat> dicSubtitle = repAdHoc.GetSubtitleLanguageStatsByAnime(session, anime.AnimeID); foreach (KeyValuePair<int, LanguageStat> kvp in dicSubtitle) { foreach (string lanName in kvp.Value.LanguageNames) { if (!subtitleLanguages.Contains(lanName)) subtitleLanguages.Add(lanName); } } ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP/Series - STEP 6 ({0}/{1}) in {2} ms", grp.GroupName, series.AnimeSeriesID, ts.TotalMilliseconds); start = DateTime.Now; // Calculate Air Date DateTime? thisDate = series.AirDate; if (thisDate.HasValue) { if (airDate_Min.HasValue) { if (thisDate.Value < airDate_Min.Value) airDate_Min = thisDate; } else airDate_Min = thisDate; if (airDate_Max.HasValue) { if (thisDate.Value > airDate_Max.Value) airDate_Max = thisDate; } else airDate_Max = thisDate; } // calculate end date // if the end date is NULL it actually means it is ongoing, so this is the max possible value thisDate = series.EndDate; if (thisDate.HasValue && endDate.HasValue) { if (thisDate.Value > endDate.Value) endDate = thisDate; } else endDate = null; // Note - only one series has to be finished airing to qualify if (series.EndDate.HasValue && series.EndDate.Value < DateTime.Now) hasFinishedAiring = true; // Note - only one series has to be finished airing to qualify if (!series.EndDate.HasValue || series.EndDate.Value > DateTime.Now) isCurrentlyAiring = true; // We evaluate IsComplete as true if // 1. series has finished airing // 2. user has all episodes locally // Note - only one series has to be complete for the group to be considered complete if (series.EndDate.HasValue) { if (series.EndDate.Value < DateTime.Now && series.MissingEpisodeCount == 0 && series.MissingEpisodeCountGroups == 0) { isComplete = true; } } // Calculate Series Created Date thisDate = series.DateTimeCreated; if (thisDate.HasValue) { if (seriesCreatedDate.HasValue) { if (thisDate.Value < seriesCreatedDate.Value) seriesCreatedDate = thisDate; } else seriesCreatedDate = thisDate; } ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP/Series - STEP 7 ({0}/{1}) in {2} ms", grp.GroupName, series.AnimeSeriesID, ts.TotalMilliseconds); start = DateTime.Now; // for the group, if any of the series don't have a tvdb link // we will consider the group as not having a tvdb link List<CrossRef_AniDB_TvDBV2> tvXrefs = series.GetCrossRefTvDBV2(); if (tvXrefs == null || tvXrefs.Count == 0) hasTvDB = false; if (series.CrossRefMovieDB == null) hasMovieDB = false; if (series.CrossRefMAL == null) hasMAL = false; if ((tvXrefs == null || tvXrefs.Count == 0) && series.CrossRefMovieDB == null) hasMovieDBOrTvDB = false; } StatGroupIsComplete[grp.AnimeGroupID] = isComplete; StatGroupIsFinishedAiring[grp.AnimeGroupID] = hasFinishedAiring; StatGroupIsCurrentlyAiring[grp.AnimeGroupID] = isCurrentlyAiring; StatGroupHasTvDB[grp.AnimeGroupID] = hasTvDB; StatGroupHasMAL[grp.AnimeGroupID] = hasMAL; StatGroupHasMovieDB[grp.AnimeGroupID] = hasMovieDB; StatGroupHasMovieDBOrTvDB[grp.AnimeGroupID] = hasMovieDBOrTvDB; StatGroupSeriesCount[grp.AnimeGroupID] = seriesCount; StatGroupEpisodeCount[grp.AnimeGroupID] = epCount; StatGroupVideoQualityEpisodes[grp.AnimeGroupID] = videoQualityEpisodes; StatGroupAirDate_Min[grp.AnimeGroupID] = airDate_Min; StatGroupAirDate_Max[grp.AnimeGroupID] = airDate_Max; StatGroupEndDate[grp.AnimeGroupID] = endDate; StatGroupSeriesCreatedDate[grp.AnimeGroupID] = seriesCreatedDate; StatGroupUserVoteOverall[grp.AnimeGroupID] = grp.UserVote; StatGroupUserVotePermanent[grp.AnimeGroupID] = grp.UserVotePermanent; StatGroupUserVoteTemporary[grp.AnimeGroupID] = grp.UserVoteTemporary; StatGroupAniDBRating[grp.AnimeGroupID] = grp.AniDBRating; ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP - STEP 8 ({0}) in {1} ms", grp.GroupName, ts.TotalMilliseconds); start = DateTime.Now; string Stat_AudioLanguages = ""; foreach (string audioLan in audioLanguages) { if (Stat_AudioLanguages.Length > 0) Stat_AudioLanguages += ","; Stat_AudioLanguages += audioLan; } this.StatGroupAudioLanguages[grp.AnimeGroupID] = Stat_AudioLanguages; string Stat_SubtitleLanguages = ""; foreach (string subLan in subtitleLanguages) { if (Stat_SubtitleLanguages.Length > 0) Stat_SubtitleLanguages += ","; Stat_SubtitleLanguages += subLan; } this.StatGroupSubtitleLanguages[grp.AnimeGroupID] = Stat_SubtitleLanguages; ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP - STEP 9 ({0}) in {1} ms", grp.GroupName, ts.TotalMilliseconds); start = DateTime.Now; UpdateGroupFilterUsingGroup(grp.AnimeGroupID); UpdatePlexAnimeGroup(session, grp,grp.GetAllSeries()); ts = DateTime.Now - start; logger.Trace("Updating cached stats for GROUP - END ({0}) in {1} ms", grp.GroupName, ts.TotalMilliseconds); } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } }